#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// global structure to remember some states changed
// by mouse action and move
struct initRoi
{
// on off
int init;
//initial coordination based on EVENT_LBUTTONDOWN
int initX;
int initY;
// actual coordination
int actualX;
int actualY;
//Selected Rect
Rect roiRect;
//Selected Mat roi
Mat takenRoi;
} SelectedRoi;
//event int is compared to determine action of the mouse EVENT_RBUTTONDOWN
// EVENT_LBUTTONDOWN, EVENT_LBUTTONUP, EVENT_MOUSEMOVE.
// If the event is evauated as happened the global structure SelectedRoi
// is updated.
static void CallBackF(int event, int x, int y, int flags, void* img)
{
//Mouse Right button down
if (event == EVENT_RBUTTONDOWN)
{
cout << "right button " << endl;
return;
}
//Mouse Left button down
if (event == EVENT_LBUTTONDOWN)
{
SelectedRoi.initX = x;
SelectedRoi.initY = y;
SelectedRoi.init = 1;
cout << "left button DOWN" << endl;
return;
}
//Mouse Left button up
if (event == EVENT_LBUTTONUP)
{
SelectedRoi.actualX = x;
SelectedRoi.actualX = y;
cout << "left button UP" << endl;
return;
}
//Mouse move coordinates update
if (event == EVENT_MOUSEMOVE)
{
cout << "event mouse move"<< endl;
SelectedRoi.actualX = x;
SelectedRoi.actualY = y;
SelectedRoi.roiRect = Rect(SelectedRoi.initX, SelectedRoi.initY,
SelectedRoi.actualX, SelectedRoi.actualY);
return;
}
}
int main()
{
// capture
VideoCapture cap(0);
// Set initial state for the SelectedRoi structure
//- The init is set = 1 by left button down action
//- This start to display image if (SelectedRoi.init != 0) in main for loop
SelectedRoi.init = 0;
for (;;)
{
if (!cap.isOpened())
{
cout << "Video Capture Fail" << endl;
break;
}
else
{
Mat img;
cap >> img;
namedWindow("Video", WINDOW_AUTOSIZE);
// mouse call back function, where CallBackF is function
// with parameters event type, x y coorfinates
setMouseCallback("Video", CallBackF, 0);
if (SelectedRoi.init != 0)
{
// draw the rectangle updated by mouse move
rectangle(img, Rect(SelectedRoi.initX, SelectedRoi.initY,
SelectedRoi.actualX- SelectedRoi.initX, SelectedRoi.actualY- SelectedRoi.initY),
Scalar(255, 255, 255), 1, 8, 0);
}
imshow("Video", img);
int key2 = waitKey(20);
}
}
}
Monday, 10 August 2020
How to draw a circle or rectangle in cpp, example2?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment