鼠标操作
opencv中使用鼠标进行交互。
鼠标操作与滑动条的消息映射方式很类似,都是通过一个中介函数配合一个回调函数来实现的。
#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);
int main()
{
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);
namedWindow(WINDOW_NAME);
setMouseCallback(WINDOW_NAME, on_MouseHandle, (void*)&srcImage);
while (1)
{
srcImage.copyTo(tempImage);
if (g_bDrawingBox)
DrawRectangle(tempImage, g_rectangle);
imshow(WINDOW_NAME, tempImage);
if (waitKey(10) == 27) break;
}
return 0;
}
void on_MouseHandle(int event, int x, int y, int flags, void * param)//鼠标回调函数
{
Mat & image = *(cv::Mat*) param;
switch (event)
{
case EVENT_MOUSEMOVE:
{
//鼠标移动消息
if (g_bDrawingBox)//是否进行绘制
{
g_rectangle.width = x - g_rectangle.x;
g_rectangle.height = y - g_rectangle.y;
}
}
break;
//左键按下消息
case EVENT_LBUTTONDOWN:
{
g_bDrawingBox = true;
g_rectangle = Rect(x, y, 0, 0);
}
break;
//左键抬起消息
case EVENT_LBUTTONUP:
{
g_bDrawingBox = false;
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;
}
}
}
void DrawRectangle(cv::Mat& img, cv::Rect box)//自定义矩形绘制函数
{
rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));//随机颜色
}