-
Notifications
You must be signed in to change notification settings - Fork 25
/
mouse_test.cpp
87 lines (67 loc) · 2.47 KB
/
mouse_test.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
String winName = "Mouse Events Test";
String MouseEventNames[] =
{
"EVENT_MOUSEMOVE",
"EVENT_LBUTTONDOWN",
"EVENT_RBUTTONDOWN",
"EVENT_MBUTTONDOWN",
"EVENT_LBUTTONUP",
"EVENT_RBUTTONUP",
"EVENT_MBUTTONUP",
"EVENT_LBUTTONDBLCLK",
"EVENT_RBUTTONDBLCLK",
"EVENT_MBUTTONDBLCLK",
"EVENT_MOUSEWHEEL",
"EVENT_MOUSEHWHEEL"
};
String MouseEventFlagNames[] =
{
"EVENT_FLAG_LBUTTON",
"EVENT_FLAG_RBUTTON",
"EVENT_FLAG_MBUTTON",
"EVENT_FLAG_CTRLKEY",
"EVENT_FLAG_SHIFTKEY",
"EVENT_FLAG_ALTKEY"
};
void onMouse( int event, int x, int y, int flags, void* userdata )
{
int i;
Scalar color;
Mat img = *((Mat *)userdata);
Vec3b pixel = img.at<Vec3b>(y,x);
if( ( event > 0 ) || ( y < 300 ) ) // disable EVENT_MOUSEMOVE event on bottom part of the window.
{
img.setTo( Scalar( 220, 220, 220 ) );
color = Scalar( pixel[0], pixel[1],pixel[2] );
putText( img, "Color of this text will change according pixel value of the mouse pointer", Point( 40,580 ) , FONT_HERSHEY_SIMPLEX, .4, color, 1 );
color = Scalar( 255, 0, 0 );
line( img, Point( 0, 300 ), Point( 600, 300 ), color, 2 );
putText( img, "EVENT_MOUSEMOVE event is disabled", Point( 300,320 ) , FONT_HERSHEY_SIMPLEX, .5, color, 1 );
putText( img, "on bottom part of the window", Point( 300,340 ) , FONT_HERSHEY_SIMPLEX, .5, color, 1 );
for( i=0 ; i<12 ; i++ )
{
color = i==event ? Scalar( 0, 0, 255 ) : Scalar( 150, 150, 150 );
putText( img, MouseEventNames[i], Point( 40, 30*i+20 ) , FONT_HERSHEY_SIMPLEX, .7, color, 2 );
}
for( i=0 ; i<6 ; i++ )
{
color = (int)pow( 2, i ) & flags ? Scalar( 0, 0, 255 ) : Scalar( 150, 150, 150 );
putText( img, MouseEventFlagNames[i], Point( 40, 30*i+400 ) , FONT_HERSHEY_SIMPLEX, .7, color, 2 );
}
putText( img, format( "x : %d", x ), Point( 350,30*event+20 ) , FONT_HERSHEY_SIMPLEX, .7, Scalar( 0, 0, 255 ), 2 );
putText( img, format( "y : %d", y ), Point( 500,30*event+20 ) , FONT_HERSHEY_SIMPLEX, .7, Scalar( 0, 0, 255 ), 2 );
imshow( winName, img );
}
}
int main( int argc, char**argv )
{
Mat image( 600, 600, CV_8UC3 );
namedWindow( winName, WINDOW_AUTOSIZE );
setMouseCallback( winName, onMouse, &image );
imshow( winName, image );
waitKey();
return 0;
}