Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <opencv2/opencv.hpp>
- #include <opencv2/features2d.hpp>
- #include <opencv2/xfeatures2d.hpp>
- using namespace std;
- using namespace cv;
- Point P1(0, 0);
- Point P2(0, 0);
- bool clicked = false;
- Mat img0;
- Mat captured;
- bool saved = false;
- void CallBackFunc(int event, int x, int y, int flags, void* userdata)
- {
- if (event == EVENT_LBUTTONDOWN && clicked == false)
- {
- cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
- P1.x = x;
- P1.y = y;
- P2.x = x;
- P2.y = y;
- Rect(P1, P2);
- clicked = true;
- }
- if (event == EVENT_MOUSEMOVE && clicked == true)
- {
- P2.x = x;
- P2.y = y;
- }
- if (event == EVENT_LBUTTONUP)
- {
- cout << "Left button of the mouse is released - position (" << x << ", " << y << ")" << endl;
- P2.x = x;
- P2.y = y;
- saved = false;
- clicked = false;
- }
- if (event == EVENT_RBUTTONUP)
- {
- P1.x = 0;
- P1.y = 0;
- P2.x = 0;
- P2.y = 0;
- clicked = false;
- }
- }
- int main() {
- FileStorage fs("lifecam.yaml", FileStorage::READ);
- if (!fs.isOpened()) {
- cerr << "failed to open camera.yml" << endl;
- return -1;
- }
- cv::VideoCapture videoCapture(0);
- if (!videoCapture.isOpened())// check if we succeeded
- return -1;
- videoCapture.set(CV_CAP_PROP_FRAME_WIDTH, 1200);
- videoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
- for (;;)
- {
- Mat frame;
- videoCapture >> frame; // get a new frame from camera
- cvtColor(frame, img0, CV_RGBA2RGB); // Farbmodus
- namedWindow("ImageDisplay", 1);
- setMouseCallback("ImageDisplay", CallBackFunc, NULL);
- captured = img0(Rect(P1, P2)).clone();
- rectangle(img0, P1, P2, Scalar(0, 0, 255), 1, 8);
- imshow("ImageDisplay", img0);
- if (captured.rows > 0 && captured.cols > 0 and saved == false ) {
- imshow("Captured", captured);
- }
- if (waitKey(30) == 99) break;
- if (waitKey(30) == 115 && saved == false)
- {
- imwrite("captured.jpg", captured);
- Mat img = imread("captured.jpg");
- cvDestroyWindow("Captured");
- imshow("SavePicture", img);
- saved = true;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement