使用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;
    }
相关推荐
居然JuRan17 分钟前
从零开始学大模型之预训练语言模型
人工智能
martinzh37 分钟前
向量化与嵌入模型:RAG系统背后的隐形英雄
人工智能
新智元1 小时前
学哲学没出路?不好意思,现在哲学就业碾压 CS!
人工智能·openai
AI码上来1 小时前
当小智 AI 遇上数字人,我用 WebRTC 打造实时音视频应用
人工智能·webrtc·实时音视频
黎燃1 小时前
智能库存管理的需求预测模型:从业务痛点到落地代码的完整实践
人工智能
机器之心1 小时前
DPad: 扩散大语言模型的中庸之道,杜克大学陈怡然团队免训推理加速61倍
人工智能·openai
一车小面包1 小时前
人工智能中的线性代数总结--简单篇
人工智能·numpy
晚云与城1 小时前
今日分享:C++ Stack和queue(栈与队列)
开发语言·c++
大模型真好玩1 小时前
深入浅出LangGraph AI Agent智能体开发教程(四)—LangGraph全生态开发工具使用与智能体部署
人工智能·python·mcp
算家计算1 小时前
OpenAI百亿美元造芯计划曝光,算力争夺战进入新阶段?
人工智能·openai·资讯