使用opencv鼠标回调函数选择ROI区域

使用opencv绘制矩形ROI,点击鼠标左键开始绘制,鼠标右键退出绘制并返回矩形左上角和右下角坐标。可绘制多个ROI区域(图中红色区域)

cpp 复制代码
/****************************************
函数名称:
		MouseCallbackDrawRect()
函数功能:
		绘制矩形回调函数
***************************************/
bool drawing = false;
bool bExitDrawing = false;
Point startPoint, endPoint;
Mat tempImg;
vector<pair<Point, Point>> vecPairLeftUpRightDownPoint;//记录绘制的起点和终点

void MouseCallbackDrawRect(int event, int x, int y, int flags, void* userdata)
{
	const Mat& image = *(Mat*)userdata;
	if (event == EVENT_LBUTTONDOWN) { // 鼠标左键按下
		drawing = true;
		startPoint = Point(x, y); // 记录起始点
	}
	else if (event == EVENT_MOUSEMOVE) { // 鼠标移动
		if (drawing)
		{
			endPoint = Point(x, y); // 更新结束点
			tempImg = image.clone(); // 复制原始图像
			rectangle(tempImg, startPoint, endPoint, Scalar(0, 0, 255), 2); // 绘制矩形
			for (vector<pair<Point, Point>>::const_iterator it = vecPairLeftUpRightDownPoint.cbegin(); it != vecPairLeftUpRightDownPoint.cend(); it++)
			{
				rectangle(tempImg, Rect(it->first,it->second), Scalar(0, 0, 255), 2); // 绘制矩形
			}
			imshow("Image", tempImg); // 显示实时矩形
		}
	}
	else if (event == EVENT_LBUTTONUP) { // 鼠标左键释放
		drawing = false;
		endPoint = Point(x, y); // 记录结束点
		pair<Point, Point> pairPointTemp;
		pairPointTemp.first = startPoint;
		pairPointTemp.second = endPoint;
		vecPairLeftUpRightDownPoint.push_back(pairPointTemp);
		rectangle(image, startPoint, endPoint, Scalar(0, 0, 255), 2); // 在原始图像上绘制矩形
		imshow("Image", image); // 显示最终结果
	}
	if (event == EVENT_RBUTTONDOWN)//鼠标右键退出
	{
		bExitDrawing = true;
		return;
	}
}

/****************************************
函数名称:
		LogLURDPoint()
函数功能:
		记录矩形左上角和右下角坐标
***************************************/
vector<pair<Point, Point>> LogLURDPoint(Mat img, string WindowName)
{
	namedWindow(WindowName);
	setMouseCallback(WindowName, MouseCallbackDrawRect, &img);

	vector<pair<Point, Point>> vecpairStartEndPoint;

	// 显示图像
	imshow(WindowName, img);

	// 等待按键退出
	while (true)
	{
		if ((waitKey(1) == 27)|| bExitDrawing) { // 按下ESC键或者鼠标右键退出
			vecpairStartEndPoint= vecPairLeftUpRightDownPoint;
			vecPairLeftUpRightDownPoint.clear();
			bExitDrawing = false;
			break;
		}
	}
	return vecpairStartEndPoint;
}

/*************************主函数***************************/
int main()
{
    Mat matSrcColor = imread("1.jpg",IMREAD_COLOR);
    if (matSrcColor.empty())
    {
        cout << "源图像不存在,请确认图像路径。";
        return -1;
    }
    else
    {
        Mat matSrcColorClone = matSrcColor.clone();
        vector<pair<Point, Point>> pairLURDPoints;
        pairLURDPoints = LogLURDPoint(matSrcColorClone, "Image");
        return;
    }
相关推荐
AKAMAI27 分钟前
Akamai Cloud客户案例 | CloudMinister借助Akamai实现多云转型
人工智能·云计算
小a杰.2 小时前
Flutter 与 AI 深度集成指南:从基础实现到高级应用
人工智能·flutter
橘子真甜~2 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
colorknight2 小时前
数据编织-异构数据存储的自动化治理
数据仓库·人工智能·数据治理·数据湖·数据科学·数据编织·自动化治理
Lun3866buzha3 小时前
篮球场景目标检测与定位_YOLO11-RFPN实现详解
人工智能·目标检测·计算机视觉
janefir3 小时前
LangChain框架下DirectoryLoader使用报错zipfile.BadZipFile
人工智能·langchain
齐齐大魔王3 小时前
COCO 数据集
人工智能·机器学习
asiwxy4 小时前
OpenGL 材质
c++
阿华hhh4 小时前
Linux系统编程(标准io)
linux·开发语言·c++
AI营销实验室4 小时前
原圈科技AI CRM系统赋能销售新未来,行业应用与创新点评
人工智能·科技