【opencv3的鼠标事件选取ROI区域操作】

  1. 选取图像中的ROI区域:
#include<opencv2\opencv.hpp>
#include <stdio.h>  
using namespace cv;
using namespace std;
Mat org, dst, img, tmp;
	void on_mouse(int event, int x, int y, int flags, void *)
	{
		static Point pre_pt = (-1, -1);
		static Point cur_pt = (-1, -1);
		if (event == CV_EVENT_LBUTTONDOWN)
		{
			pre_pt = Point(x, y);
            cout<<pre_pt<<endl;
		}
		else if (event == CV_EVENT_MOUSEMOVE && flags)//摁下左键,flags为1 

		{
			org.copyTo(tmp);
			cur_pt = Point(x, y);
			circle(tmp,cur_pt,4,Scalar(0, 255, 0, 0),2,8);
			rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
            cout<<cur_pt<<endl;
			imshow("img", tmp);//画的时候显示框
		}
		else if (event == CV_EVENT_LBUTTONUP)
		{
			org.copyTo(img);
			rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
			imshow("img", img);//画完后显示框
			int width = abs(pre_pt.x - cur_pt.x);
			int height = abs(pre_pt.y - cur_pt.y);
			dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
			namedWindow("dst");
			imshow("dst", dst);

		}

	}



	void main(){
		org = imread("98.jpg");
		org.copyTo(img);
		namedWindow("img");
		imshow("img", img);
		setMouseCallback("img", on_mouse, 0);
	/*	waitKey();
		cvtColor(dst, dst, CV_BGR2GRAY);
		imshow("gray", dst);*/
		waitKey(0);
	}

 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CVer儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值