使用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;
    }
相关推荐
山烛几秒前
KNN 算法中的各种距离:从原理到应用
人工智能·python·算法·机器学习·knn·k近邻算法·距离公式
盲盒Q11 分钟前
《频率之光:归途之光》
人工智能·硬件架构·量子计算
guozhetao13 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
墨染点香19 分钟前
第七章 Pytorch构建模型详解【构建CIFAR10模型结构】
人工智能·pytorch·python
go546315846520 分钟前
基于分组规则的Excel数据分组优化系统设计与实现
人工智能·学习·生成对抗网络·数学建模·语音识别
茫茫人海一粒沙26 分钟前
vLLM 的“投机取巧”:Speculative Decoding 如何加速大语言模型推理
人工智能·语言模型·自然语言处理
诗酒当趁年华28 分钟前
【NLP实践】二、自训练数据实现中文文本分类并提供RestfulAPI服务
人工智能·自然语言处理·分类
静心问道1 小时前
Idefics3:构建和更好地理解视觉-语言模型:洞察与未来方向
人工智能·多模态·ai技术应用
sheep88881 小时前
AI与区块链Web3技术融合:重塑数字经济的未来格局
人工智能·区块链
奋进的孤狼1 小时前
【Spring AI】阿里云DashScope灵积模型
人工智能·spring·阿里云·ai·云计算