OpenCV鼠标框选区域 --源程序

                          OpenCV鼠标框选区域 --源程序

鼠标框选区域 模板1: 


#include <imgproc/imgproc.hpp>
#include <highgui/highgui.hpp>
#include <core/core.hpp>
#include <photo/photo.hpp>

using namespace cv;

Point ptL, ptR; //鼠标画出矩形框的起点和终点
Mat imageSource, imageSourceCopy;
Mat ROI; //原图需要修复区域的ROI

void OnMouse(int event, int x, int y, int flag, void *ustg);   //鼠标回调函数

int main()

{

	imageSource = imread("Lena.jpg");

	if (!imageSource.data)

	{

		return -1;

	}

	imshow("原图", imageSource);

	setMouseCallback("原图", OnMouse);

	waitKey();

}

void OnMouse(int event, int x, int y, int flag, void *ustg)

{

	if (event == CV_EVENT_LBUTTONDOWN)   //可以根据鼠标的起点 和 终点来确定矩形

	{

		ptL = Point(x, y);   //起点

		ptR = Point(x, y);   //终点

	}

	if (flag == CV_EVENT_FLAG_LBUTTON)    //画矩形

	{

		ptR = Point(x, y);                //终点

		imageSourceCopy = imageSource.clone();

		rectangle(imageSourceCopy, ptL, ptR, Scalar(255, 0, 0));

		imshow("原图", imageSourceCopy);

	}

	if (event == CV_EVENT_LBUTTONUP)        //画矩形后,所做的事件

	{

		if (ptL != ptR)

		{

		ROI = imageSource(Rect(ptL, ptR));  //用户选择后的图片

		imshow("ROI", ROI);                 //显示 用户选择后的图片
 
			                                //提示 Rect(ptL, ptR) 为用户选择后,选区的大小

			waitKey();

		}

	}  

	//单击鼠标右键 ,触发的事件     
	
	if (event == CV_EVENT_RBUTTONDOWN)

	{
          //自定义事件
		
	}

}


举个例子: 鼠标圈定区域阈值处理+Mask膨胀处理


#include <imgproc/imgproc.hpp>

#include <highgui/highgui.hpp>

#include <core/core.hpp>

#include <photo/photo.hpp>

 

using namespace cv;

 

Point ptL, ptR; //鼠标画出矩形框的起点和终点

Mat imageSource, imageSourceCopy;

Mat ROI; //原图需要修复区域的ROI

 

//鼠标回调函数

void OnMouse(int event, int x, int y, int flag, void *ustg);

 

//鼠标圈定区域阈值处理+Mask膨胀处理

int main()

{

	imageSource = imread("Test.jpg");

	if (!imageSource.data)

	{

		return -1;

	}

	imshow("原图", imageSource);

	setMouseCallback("原图", OnMouse);

	waitKey();

}

void OnMouse(int event, int x, int y, int flag, void *ustg)

{

	if (event == CV_EVENT_LBUTTONDOWN)

	{

		ptL = Point(x, y);

		ptR = Point(x, y);

	}

	if (flag == CV_EVENT_FLAG_LBUTTON)

	{

		ptR = Point(x, y);

		imageSourceCopy = imageSource.clone();

		rectangle(imageSourceCopy, ptL, ptR, Scalar(255, 0, 0));

		imshow("原图", imageSourceCopy);

	}

	if (event == CV_EVENT_LBUTTONUP)

	{

		if (ptL != ptR)

		{

			ROI = imageSource(Rect(ptL, ptR));

			imshow("ROI", ROI);

			waitKey();

		}

	}

	//单击鼠标右键开始图像修复

	if (event == CV_EVENT_RBUTTONDOWN)

	{

		imageSourceCopy = ROI.clone();

		Mat imageGray;

		cvtColor(ROI, imageGray, CV_RGB2GRAY); //转换为灰度图

		Mat imageMask = Mat(ROI.size(), CV_8UC1, Scalar::all(0));

 

		//通过阈值处理生成Mask

		threshold(imageGray, imageMask, 235, 255, CV_THRESH_BINARY);

		Mat Kernel = getStructuringElement(MORPH_RECT, Size(3, 3));

		dilate(imageMask, imageMask, Kernel);  //对Mask膨胀处理

		inpaint(ROI, imageMask, ROI, 9, INPAINT_TELEA);  //图像修复

		imshow("Mask", imageMask);

		imshow("修复后", imageSource);

	}

}

 

希望对你有帮助。

猜你喜欢

转载自blog.csdn.net/qq_41204464/article/details/83793333