使用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;
    }
相关推荐
TGITCIC1 小时前
AI Search进化论:从RAG到DeepSearch的智能体演变全过程
人工智能·ai大模型·ai智能体·ai搜索·大模型ai·deepsearch·ai search
lucky_lyovo4 小时前
自然语言处理NLP---预训练模型与 BERT
人工智能·自然语言处理·bert
fantasy_arch4 小时前
pytorch例子计算两张图相似度
人工智能·pytorch·python
No0d1es6 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
AndrewHZ6 小时前
【3D重建技术】如何基于遥感图像和DEM等数据进行城市级高精度三维重建?
图像处理·人工智能·深度学习·3d·dem·遥感图像·3d重建
飞哥数智坊6 小时前
Coze实战第18讲:Coze+计划任务,我终于实现了企微资讯简报的定时推送
人工智能·coze·trae
Code_流苏7 小时前
AI热点周报(8.10~8.16):AI界“冰火两重天“,GPT-5陷入热议,DeepSeek R2模型训练受阻?
人工智能·gpt·gpt5·deepseek r2·ai热点·本周周报
赴3357 小时前
矿物分类案列 (一)六种方法对数据的填充
人工智能·python·机器学习·分类·数据挖掘·sklearn·矿物分类
大模型真好玩7 小时前
一文深度解析OpenAI近期发布系列大模型:意欲一统大模型江湖?
人工智能·python·mcp
双翌视觉7 小时前
工业视觉检测中的常见的四种打光方式
人工智能·计算机视觉·视觉检测