opencv图像处理之鼠标事件(矩形roi绘图)

为满足图像处理要求,博主写此简易代码有如下作用:

1、根据感兴趣区域做深度学习标签;

2、提取感兴趣区域进行目标跟踪;

3、对感兴趣区域进行进一步操作。

 

具体代码如下:

 

//Author: samylee
//Contact email: [email protected]
#include   
#include   
#include   

using namespace cv;

Mat org, dst, img, tmp;

void on_mouse(int event, int x, int y, int flags, void *ustc)
{
	static Point pre_pt = (-1, -1);
	static Point cur_pt = (-1, -1);
	if (event == CV_EVENT_LBUTTONDOWN)
	{
		org.copyTo(img); 
		pre_pt = Point(x, y);
	}
	else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//摁下左键,flags为1 
	{
		img.copyTo(tmp);
		cur_pt = Point(x, y);
		rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
		imshow("img", tmp);
	}
	else if (event == CV_EVENT_LBUTTONUP) 
	{
		org.copyTo(img);
		cur_pt = Point(x, y);
		rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
		imshow("img", img);
		img.copyTo(tmp);
		int width = abs(pre_pt.x - cur_pt.x);
		int height = abs(pre_pt.y - cur_pt.y);
		if (width == 0 || height == 0)
		{
			return;
		}
		dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
		namedWindow("dst");
		imshow("dst", dst);
	}
}
void main()
{
	org = imread("desktop.jpg");
	org.copyTo(img);
	namedWindow("img");
	setMouseCallback("img", on_mouse, 0); 
	imshow("img", img);
	waitKey(0);
}

 

 

 

 

 

效果图如下:

处理前:

opencv图像处理之鼠标事件(矩形roi绘图)_第1张图片

处理后:

 

 

任何问题请加唯一QQ2258205918(名称samylee)!

唯一VX:samylee_csdn

 

你可能感兴趣的:(opencv,图像处理,鼠标,标签)