OpenCV:鼠标绘制+提取感兴趣区域

1.鼠标绘制

详情:Opencv函数setMouseCallback鼠标事件响应

1.设置MouseCallback函数,函数名可随意,但是参数要与MouseCallback的一致。

2.setMouseCallback()

2.以圆形为例,其中有矩形的方法

可以先提取出矩形的区域,再利用mask提取圆形的区域。

#include<opencv2/opencv.hpp>
#include<iostream>
#include<imgproc.hpp>
#include<cmath>
using namespace cv;
using namespace std;

Point startp(-1, -1);
Point endp(-1, -1);
//绘制圆,需要中心坐标和半径
Point centerp(-1, -1);
int radius = 0;
Mat temp;
void MouseDraw(int event,int x,int y,int flag,void* usedata ) {
	Mat image = *((Mat*)usedata);
	Mat roi;
	if (event == EVENT_LBUTTONDOWN) {//左键按下
		centerp.x = x;
		centerp.y = y;
		cout << "center point: " << centerp << endl;
	}
	else if (event == EVENT_MOUSEMOVE) {//鼠标移动
		if (centerp.x > 0 and centerp.y > 0) {
			//得有这个判断条件,不然一开始 不按下鼠标左键时就已经有起始点了
			endp.x = x;
			endp.y = y;
			int w = endp.x - centerp.x;
			int h = endp.y - centerp.y;
			radius = (int)sqrt(pow(w, 2) + pow(h, 2));

			temp.copyTo(image);
            //矩形可利用rectangle来实现
			circle(image, centerp, radius, Scalar(0, 0, 255), 2);
			imshow("mousedrawing", image);
		}
	}
	else if (event == EVENT_LBUTTONUP) {//左键抬起
		endp.x = x;
		endp.y = y;
		int w = endp.x - centerp.x;
		int h = endp.y - centerp.y;
		radius = (int)sqrt(pow(w, 2) + pow(h, 2));

		circle(image, centerp, radius, Scalar(0, 0, 255), 2);
		imshow("mousedrawing", image);

		//截取该部分图像
		//先截取矩形的部分,再用模取出圆形
		Rect rec;
		rec.x = centerp.x - radius;
		rec.y = centerp.y - radius;
		rec.width = 2 * radius;
		rec.height = 2 * radius;
		Mat Rec_roi = image(rec);//矩形的ROI区域

		Mat roi = Mat::zeros(Rec_roi.size(), Rec_roi.type());
		roi = Scalar(255, 255, 255);//将目标ROI背景置白

		//利用mask提取圆形的ROI
		Mat mask = Mat::zeros(Rec_roi.size(), Rec_roi.type());
		circle(mask, Point(radius,radius), radius, Scalar(255, 255, 255), -1);
		cvtColor(mask, mask, COLOR_BGR2GRAY);
		imshow("mask", mask);

		Rec_roi.copyTo(roi, mask);
		imshow("ROI", roi);
		
		//清空中心坐标,这样才会停止掉这一次的绘制
		centerp.x = -1;
		centerp.y = -1;
	}
}


int main(int argc, char** argv) {
	Mat image = imread("C:/Users/YY/Pictures/Saved Pictures/2.jpg");
	namedWindow("mousedrawing");
	setMouseCallback("mousedrawing", MouseDraw, &image);
	imshow("mousedrawing", image);
	temp=image.clone();

	waitKey(0);
	destroyAllWindows();
	return 0;
}

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python OpenCV鼠标框选区域检测的示例代码: ```python import cv2 def draw_rectangle(event, x, y, flags, param): global x_init, y_init, drawing, top_left_pt, bottom_right_pt if event == cv2.EVENT_LBUTTONDOWN: drawing = True x_init, y_init = x, y elif event == cv2.EVENT_MOUSEMOVE: if drawing: top_left_pt = (min(x_init, x), min(y_init, y)) bottom_right_pt = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] cv2.rectangle(img, top_left_pt, bottom_right_pt, (0, 255, 0), 2) elif event == cv2.EVENT_LBUTTONUP: drawing = False top_left_pt = (min(x_init, x), min(y_init, y)) bottom_right_pt = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] cv2.rectangle(img, top_left_pt, bottom_right_pt, (0, 255, 0), 2) roi = img[top_left_pt[1]:bottom_right_pt[1], top_left_pt[0]:bottom_right_pt[0]] detect(roi) def detect(roi): gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150) cv2.imshow("Edges", edges) drawing = False x_init, y_init = 0, 0 top_left_pt, bottom_right_pt = (-1, -1), (-1, -1) img = cv2.imread("image.jpg") cv2.namedWindow("Select ROI") cv2.setMouseCallback("Select ROI", draw_rectangle) while True: cv2.imshow("Select ROI", img) c = cv2.waitKey(1) if c == 27: break cv2.destroyAllWindows() ``` 该示例代码实现了在图像鼠标框选区域,并在选定的区域中进行边缘检测。在鼠标左键按下时开始绘制矩形,鼠标移动时更新矩形,鼠标左键松开时完成矩形绘制。同时,根据选定区域提取出ROI(感兴趣区域),并在ROI中进行边缘检测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值