-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha18_UseMouse.cpp
145 lines (120 loc) · 4.98 KB
/
a18_UseMouse.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <ros/ros.h>
#include <opencv2/opencv.hpp>
using namespace cv;
//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
#define WINDOW_NAME "【程序窗口】" //为窗口标题定义的宏
//-----------------------------------【全局函数声明部分】------------------------------------
// 描述:全局函数的声明
//------------------------------------------------------------------------------------------------
void on_MouseHandle(int event, int x, int y, int flags, void* param);//鼠标回调函数
void DrawRectangle( cv::Mat& img, cv::Rect box );//自定义的矩形绘制函数
void ShowHelpText( );
//-----------------------------------【全局变量声明部分】-----------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
Rect g_rectangle;
bool g_bDrawingBox = false;//是否进行绘制
RNG g_rng(12345);
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-------------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
ros::init(argc, argv, "a18_UseMouse");
//【0】改变console字体颜色
system("color 9F");
//【0】显示欢迎和帮助文字
ShowHelpText();
//【1】准备参数
g_rectangle = Rect(-1,-1,0,0);
Mat srcImage(600, 800,CV_8UC3), tempImage;
srcImage.copyTo(tempImage);
g_rectangle = Rect(-1,-1,0,0);
srcImage = Scalar::all(0);
//【2】设置鼠标操作回调函数
namedWindow( WINDOW_NAME );
setMouseCallback(WINDOW_NAME,on_MouseHandle,(void*)&srcImage);
//【3】程序主循环,当进行绘制的标识符为真时,进行绘制
while(1)
{
srcImage.copyTo(tempImage);//拷贝源图到临时变量
if( g_bDrawingBox ) DrawRectangle( tempImage, g_rectangle );//当进行绘制的标识符为真,则进行绘制
imshow( WINDOW_NAME, tempImage );
if( waitKey( 10 ) == 27 ) break;//按下ESC键,程序退出
}
return 0;
}
//--------------------------------【on_MouseHandle( )函数】-----------------------------
// 描述:鼠标回调函数,根据不同的鼠标事件进行不同的操作
//-----------------------------------------------------------------------------------------------
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
ROS_INFO("on_MouseHandle()");
//std::cout << "on_MouseHandle()" << event << std::endl;
Mat& image = *(cv::Mat*) param;
switch( event)
{
//鼠标移动消息
case EVENT_MOUSEMOVE:
{
if( g_bDrawingBox )//如果是否进行绘制的标识符为真,则记录下长和宽到RECT型变量中
{
g_rectangle.width = x-g_rectangle.x;
g_rectangle.height = y-g_rectangle.y;
}
}
break;
//左键按下消息
case EVENT_LBUTTONDOWN:
{
std::cout << "EVENT_LBUTTONDOWN" << std::endl;
g_bDrawingBox = true;
g_rectangle =Rect( x, y, 0, 0 );//记录起始点
}
break;
//左键抬起消息
case EVENT_LBUTTONUP:
{
std::cout << "EVENT_LBUTTONUP" << std::endl;
g_bDrawingBox = false;//置标识符为false
//对宽和高小于0的处理
if( g_rectangle.width < 0 )
{
g_rectangle.x += g_rectangle.width;
g_rectangle.width *= -1;
}
if( g_rectangle.height < 0 )
{
g_rectangle.y += g_rectangle.height;
g_rectangle.height *= -1;
}
//调用函数进行绘制
DrawRectangle( image, g_rectangle );
}
break;
}
}
//-----------------------------------【DrawRectangle( )函数】------------------------------
// 描述:自定义的矩形绘制函数
//-----------------------------------------------------------------------------------------------
void DrawRectangle( cv::Mat& img, cv::Rect box )
{
std::cout << "draw base point " << box.tl() << " and " << box.br() << std::endl;
cv::rectangle(img,box.tl(),box.br(),cv::Scalar(g_rng.uniform(0, 255), g_rng.uniform(0,255), g_rng.uniform(0,255)));//随机颜色
}
//-----------------------------------【ShowHelpText( )函数】-----------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
//输出欢迎信息和OpenCV版本
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第18个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION );
printf("\n\n ----------------------------------------------------------------------------\n");
//输出一些帮助信息
printf("\n\n\n\t欢迎来到【鼠标交互演示】示例程序\n");
printf("\n\n\t请在窗口中点击鼠标左键并拖动以绘制矩形\n");
}