0%

OpenCV:CPP

鼠标事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2\opencv.hpp"
using namespace cv;

#include <iostream>
#pragma comment(lib,"opencv_world455d.lib")
using namespace std;
#define WINDOW "原图"
Mat g_srcImg, g_dstImg;
Point previousPoint;
bool P = false;
void on_mouse(int event, int x, int y, int flags, void*);
int main()
{
g_srcImg = imread("test.jpg",1);
imshow(WINDOW, g_srcImg);
setMouseCallback(WINDOW, on_mouse, 0);
waitKey(0);
destroyAllWindows();
imwrite("result.jpg", g_srcImg);
return 0;
}
void on_mouse(int event, int x, int y, int flags, void*) {
if (event == EVENT_LBUTTONDOWN) {
previousPoint = Point(x, y);
}
else if(event==EVENT_MOUSEMOVE&&(flags&EVENT_FLAG_LBUTTON))
{
Point pt(x, y);
line(g_srcImg, previousPoint, pt, Scalar(0, 0, 255), 2, 5, 0);
previousPoint = pt;
imshow(WINDOW, g_srcImg);

}
}

键盘事件

1