opencv-鼠标框选矩形区域并输出尺寸

Posted 嵌入式爱好者-超

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv-鼠标框选矩形区域并输出尺寸相关的知识,希望对你有一定的参考价值。

全部代码

#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 = (0, 0);
	static Point cur_pt = (0, 0);
	if (event == EVENT_LBUTTONDOWN)
	
		pre_pt = Point(x, y);
	
	else if (event == 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);
		
		imshow("img", tmp);//画的时候显示框
	
	else if (event == 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));
		cout << "x=" << pre_pt.x << "y=" << pre_pt.y << "width=" << width << "height=" << height << endl;
		namedWindow("dst");
		imshow("dst", dst);

	



int main() 
	org = imread("D:/images/111.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);

以上是关于opencv-鼠标框选矩形区域并输出尺寸的主要内容,如果未能解决你的问题,请参考以下文章

opencv-鼠标框选矩形区域并输出尺寸

opencv 实现 ROI 框选功能

opencv进阶-检测使用鼠标框选的自定义区域

Altium AD20的PCB板框绘制定义板子形状重新设置原点放置尺寸标注

unity实现框选效果

如何使用opencv从矩阵向量构造图像