Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/features2d.hpp>
  4. #include <opencv2/xfeatures2d.hpp>
  5.  
  6.  
  7. using namespace std;
  8. using namespace cv;
  9.  
  10. Point P1(0, 0);
  11. Point P2(0, 0);
  12. bool clicked = false;
  13. Mat img0;
  14. Mat captured;
  15. bool saved = false;
  16.  
  17.  
  18. void CallBackFunc(int event, int x, int y, int flags, void* userdata)
  19. {
  20.  
  21. if (event == EVENT_LBUTTONDOWN && clicked == false)
  22. {
  23. cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
  24. P1.x = x;
  25. P1.y = y;
  26. P2.x = x;
  27. P2.y = y;
  28. Rect(P1, P2);
  29. clicked = true;
  30. }
  31. if (event == EVENT_MOUSEMOVE && clicked == true)
  32. {
  33. P2.x = x;
  34. P2.y = y;
  35. }
  36. if (event == EVENT_LBUTTONUP)
  37. {
  38. cout << "Left button of the mouse is released - position (" << x << ", " << y << ")" << endl;
  39. P2.x = x;
  40. P2.y = y;
  41. saved = false;
  42. clicked = false;
  43. }
  44. if (event == EVENT_RBUTTONUP)
  45. {
  46. P1.x = 0;
  47. P1.y = 0;
  48. P2.x = 0;
  49. P2.y = 0;
  50.  
  51. clicked = false;
  52.  
  53. }
  54. }
  55.  
  56.  
  57.  
  58. int main() {
  59.  
  60.  
  61. FileStorage fs("lifecam.yaml", FileStorage::READ);
  62. if (!fs.isOpened()) {
  63. cerr << "failed to open camera.yml" << endl;
  64. return -1;
  65. }
  66.  
  67.  
  68. cv::VideoCapture videoCapture(0);
  69. if (!videoCapture.isOpened())// check if we succeeded
  70. return -1;
  71. videoCapture.set(CV_CAP_PROP_FRAME_WIDTH, 1200);
  72. videoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
  73.  
  74.  
  75.  
  76.  
  77.  
  78. for (;;)
  79. {
  80. Mat frame;
  81. videoCapture >> frame; // get a new frame from camera
  82. cvtColor(frame, img0, CV_RGBA2RGB); // Farbmodus
  83. namedWindow("ImageDisplay", 1);
  84. setMouseCallback("ImageDisplay", CallBackFunc, NULL);
  85. captured = img0(Rect(P1, P2)).clone();
  86. rectangle(img0, P1, P2, Scalar(0, 0, 255), 1, 8);
  87. imshow("ImageDisplay", img0);
  88. if (captured.rows > 0 && captured.cols > 0 and saved == false ) {
  89. imshow("Captured", captured);
  90. }
  91. if (waitKey(30) == 99) break;
  92.  
  93. if (waitKey(30) == 115 && saved == false)
  94. {
  95. imwrite("captured.jpg", captured);
  96. Mat img = imread("captured.jpg");
  97. cvDestroyWindow("Captured");
  98. imshow("SavePicture", img);
  99. saved = true;
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement