-
2019-03-26 18:47:18
minAreaRect函数
函数作用:主要求得包含点集最小面积的矩形,这个矩形是可以有偏转角度的,可以与图像的边界不平行
RotatedRect minAreaRect(InputArray points) ; InputArray points:表示输入的点集
RotatedRect
它的返回值是矩形的四个顶点,中心点坐标,矩形角度
通过轮廓点找到轮廓的最小外接矩形,同时返回上面的关于矩形的信息
下面给出一个例子:#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> //opencv申明 #include <opencv2\imgproc\imgproc.hpp> #include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; Mat caculate(Mat img) { int sum = 0; Mat dst,dst1,src,grayimg,imag,dst_image; int u1 = img.rows; int u2 = img.cols; for (int i = 0; i < img.rows; i++) { for (int j = 0; j < img.cols; j++) { sum = sum + img.at<uchar>(i, j); } } int u = u1*u2; int average = (int)sum/u*2; threshold(img, src, average, 255, CV_THRESH_BINARY); Canny(src,dst_image,50,50); return dst_image; } int main() { vector<vector<Point>> contours; vector<vector<Point>> contours1; Mat image = imread("C:\\Users\\Administrator\\Desktop\\tp_01.bmp");//读入模板图 Mat image1 = imread("C:\\Users\\Administrator\\Desktop\\tp_03.bmp"); Mat temp_img = caculate(image); findContours(temp_img, contours, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0)); drawContours(image, contours, -1, Scalar(0, 255,0), 1.5, 8); RotatedRect temp_r= minAreaRect(contours[0]); int temp_angle = temp_r.angle; int temp_x = temp_r.center.x; int temp_y = temp_r.center.y; Size2i temp_size = temp_r.size; Point2f fourPoint2f[4]; //将rectPoint变量中存储的坐标值放到 fourPoint的数组中 temp_r.points(fourPoint2f); //根据得到的四个点的坐标 绘制矩形 for (int i = 0; i < 3; i++) { line(image, fourPoint2f[i], fourPoint2f[i + 1], Scalar(55, 100, 195), 2, CV_AA); } line(image, fourPoint2f[0], fourPoint2f[3], Scalar(55, 100, 195), 2, CV_AA); Mat dst_img = caculate(image1); findContours(dst_img, contours1, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0)); drawContours(image1, contours1, -1, Scalar(0, 255,0), 1.5, 8); RotatedRect dst_r= minAreaRect(contours1[0]); int dst_angle = dst_r.angle; int dst_x = dst_r.center.x; int dst_y = dst_r.center.y; Size2i dst_size = dst_r.size; Point2f fourPoint2f1[4]; //将rectPoint变量中存储的坐标值放到 fourPoint的数组中 dst_r.points(fourPoint2f1); //根据得到的四个点的坐标 绘制矩形 for (int i = 0; i < 3; i++) { line(image1, fourPoint2f1[i], fourPoint2f1[i + 1], Scalar(55, 100, 195), 2, CV_AA); } line(image1, fourPoint2f1[0], fourPoint2f1[3], Scalar(55, 100, 195), 2, CV_AA); cout<<"旋转角度:"<<dst_angle-temp_angle<<endl; cout<<"中心x偏移量:"<<dst_x-temp_x<<endl; cout<<"中心y偏移量:"<<dst_y-temp_y<<endl; imshow("模板图",image); waitKey(0); imshow("旋转图",image1); waitKey(0); }
运行结果如下:
还在学习中,代码中有不妥之处或有好的想法,还希望大神们不吝赐教更多相关内容 -
matlab计算最小外界矩形
2013-04-22 09:56:08这个是使用matlab来计算二值图像中的前景区域的最小外界矩形,图像中只能够有一个前景区域,用于单目标跟踪最好 -
Opencv——minAreaRect()计算最小外界矩形
2022-02-18 10:56:05minAreaRect()函数计算并返回指定点集的最小区域边界斜矩形 RotatedRect minAreaRect(InputArray points) point:输入信息,可为包含点的容器vector或mat RotatedRect:返回一个轮廓的外接矩形,包覆输入信息的...minAreaRect()函数计算并返回指定点集的最小区域边界斜矩形
RotatedRect minAreaRect(InputArray points)
-
point:输入信息,可为包含点的容器vector或mat
-
RotatedRect:返回一个轮廓的外接矩形,包覆输入信息的最小斜矩形,是一个Box2D结构rect:(最小外接矩形的中心(x,y),(宽度,高度),旋转角度)。
-
旋转角度θ是水平轴(x轴)逆时针旋转,与碰到的矩形的第一条边的夹角。并且这个边的边长是width,另一条边边长是height。也就是说,在这里,width与height不是按照长短来定义的。
-
在opencv中,坐标系原点在左上角,相对于x轴,逆时针旋转角度为负,顺时针旋转角度为正。所以,θ∈(-90度,0]。
求矩形顶点:
函数:cvBoxPoints(CvBox2D box, CvPoint2D32f pt[4])
- box:输入矩形数据,minAreaRect返回的RotatedRect 类就是一个Box2D结构的矩形
- pt 返回的顶点数组
opencv源码:
void RotatedRect::points(Point2f pt[]) const { double _angle = angle*CV_PI/180.; float b = (float)cos(_angle)*0.5f; float a = (float)sin(_angle)*0.5f; pt[0].x = center.x - a*size.height - b*size.width; pt[0].y = center.y + b*size.height - a*size.width; pt[1].x = center.x + a*size.height - b*size.width; pt[1].y = center.y - b*size.height - a*size.width; pt[2].x = 2*center.x - pt[0].x; pt[2].y = 2*center.y - pt[0].y; pt[3].x = 2*center.x - pt[1].x; pt[3].y = 2*center.y - pt[1].y; } CV_IMPL void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] ) { if( !pt ) CV_Error( CV_StsNullPtr, "NULL vertex array pointer" ); cv::RotatedRect(box).points((cv::Point2f*)pt); }
eg:
double rz=0; std::vector<cv::Point2f>points; for(unsigned int i=0;i<current_cluster->points.size();i++){ cv::Point2f pt; pt.x = current_cluster->points[i].x; pt.y = current_cluster->points[i].y; points.push_back(pt); } std::vector<cv::Point2f> hull; cv::convexHull(points,hull); polygon_.header = in_ros_header; for(size_t i=0; i< hull.size();i++){ geometry_msgs::Point32 point; point.x = hull[i%hull.size()].x point.y = hull[i%hull.size()].y point.z = min_point.z; polygon_.polygon.points.push_back(point); } if(in_estimate_pose){ cv::RotateRect box = minAreaRect(hull); rz = box.angle *3.14/100; //jsk_recognition_msgs::BoundingBox bounding_box_; bounding_box.pose.position.x = box.center.x; bounding_box.pose.position.y = box.center.y; bounding_box.dimension.x = box.size.width; bounding_box.dimension.y = box.size.height; } //设置bounding box的方向 tf::Quaternion quat = tf::createQuaternionFromRPY(0.0,0.0,rz) tf::quaternionTFToMsg(quat, bounding_box_.pose.oriention) current_cluster->width = current_cluster->points.size(); current_cluster->height = 1; current_cluster->is_dense = true;
-
-
python利用四个坐标点对图片目标区域最小外接矩形进行裁剪
2020-12-21 20:44:02在图像裁剪操作中,opencv和pillow两个库都具有相应的函数,但是这两个库中的函数仅仅能对与图片平行的矩形进行裁剪操作,如果想要对目标的最小外接矩形进行裁剪该如何操作呢?如下所示: 具体处理该问题的思路如下... -
简单多边形的最小外接矩形算法
2014-06-25 13:59:20简单多边形的最小外接矩形算法,适用于玻璃排样等。 -
matlab计算目标最小外接矩形
2013-12-26 21:38:09matlab计算目标最小外接矩形,主要利用minboundrect函数。 -
Esri geometry api java 学习 实战文档 (7) 最小外界矩形 (Envelope)
2019-01-28 15:35:00Esri geometry api java 学习文档 (5) 最小外界矩形 (Envelope) Envelope是最小外界矩形,在几何集合中有很重要的作用。 Envelope相当于geometry的存在域。当几何集合进行判断时,将先判断几何体的Envelope...Esri geometry api java 学习文档 (5) 最小外界矩形 (Envelope)
Envelope是最小外界矩形,在几何集合中有很重要的作用。
Envelope相当于geometry的存在域。当几何集合进行判断时,将先判断几何体的Envelope是否在需要判断的Envelope中。可以作为索引在分幅、四叉树存储与计算等方面有重要作用,大大加快运算速度。
Polygon 的常用方法:
queryEnvelope(Envelope e)//为
geometry创建Envelope
void centerAt(Point c, double w, double h) //用中心点+长宽创建envelope(长宽是总长总宽,跟buffer不一样)
void centerAt(Point c) //设置中心点
void reaspect(double arWidth, double arHeight) //修改长宽double calculateArea2D() //计算面积
double calculateLength2D() //计算长度
boolean contains(Envelope env) //判断Envelope1是否包含Envelope2true!
boolean contains(Point p) //判断Envelope是否包Point(跟上面差不多)
boolean equals(Object _other) //判断相等
boolean intersect(Envelope other) //判断相交
boolean isIntersecting(Envelope other) //判断相交true!
void merge(Envelope other) //合并Envelope
void merge(Point point) //合并point合体!
void inflate(double dx, double dy)//膨胀
void offset(double dx, double dy)//平移
Point getCenter()//得到中心点
double getCenterX()
Point2D getCenterXY()
double getCenterY()
double getWidth()//得到宽
double getHeight()
Point getLowerLeft()//得到左下点
Point getLowerRight()
Point getUpperLeft()
Point getUpperRight()
double getXMax()//得到右边
double getXMin()
double getYMax()
double getYMin()
void setXMax(double x)
void setXMin(double x)
void setYMax(double y)
void setYMin(double y)
void queryCoordinates(Point2D[] dst)
void queryCorner(int index, Point2D ptDst)
void queryCornerByVal(int index, Point ptDst)
void queryEnvelope(Envelope env)
Geometry getBoundary() //得到边界线
int getDimension()//维度
Geometry.Type getType()//类别
void setEmpty()
boolean isEmpty()
String toString()
void copyTo(Geometry dst)//复制到 dst
Geometry createInstance()//创建新的空的Envelope
applyTransformation(Transformation2D transform) //二维仿射变换参考: http://esri.github.io/geometry-api-java/doc/Envelope.html http://esri.github.io/geometry-api-java/javadoc/com/esri/core/geometry/Envelope.html
-
最小外接矩形
2021-12-08 21:40:44使用opencv返回点集cnt的最小外接矩形,所用函数为minAreaRect(cnt) ,cnt是所要求最小外接矩形的点集数组或向量,这个点集不定个数。 这个矩形是可以有偏转角度的,可以与图像的边界不平行。 调用形式:...使用opencv返回点集cnt的最小外接矩形,所用函数为minAreaRect(cnt) ,cnt是所要求最小外接矩形的点集数组或向量,这个点集不定个数。
这个矩形是可以有偏转角度的,可以与图像的边界不平行。
调用形式:RotatedRect minAreaRect(InputArray points)
InputArray points:表示输入的点集
输出是矩形的四个点坐标。
例如: box[i] = minAreaRect(Mat(contours[i]));
box[i].points(rect); //把最小外接矩形四个端点复制给rect数组
最小外接矩形的4个顶点顺序、中心坐标、宽度、高度、旋转角度(是度数形式,不是弧度数)的对应关系如下:
注意:旋转角度
是水平轴(x轴)逆时针旋转,与碰到的矩形的第一条边的夹角。并且这个边的边长是width,另一条边边长是height。也就是说,在这里,width与height不是按照长短来定义的。
猜测,没有验证:距离X轴最远的应该是Rect[0],然后顺时针旋转分别为Rect[1]、Rect[2]、Rect[3]。
在opencv中,坐标系原点在左上角,相对于x轴,逆时针旋转角度为负,顺时针旋转角度为正。在这里,
∈(-90度,0]。
参考文献:https://blog.csdn.net/lanyuelvyun/article/details/76614872
-
matlab实现画最小外接矩形
2011-07-12 21:31:18matlab实现的,画最小外接矩形。其中包括求出最小外接矩形的四个顶点坐标,周长,面积等参数。 -
opencv轮廓检测+最小外接矩形
2020-12-30 17:49:01img) im2, contours, hierarchy = cv2.findContours(tresh_img, cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: # 最小外界矩形的宽度和高度 width, height = cv2.minAreaRect(cnt)[1] if ... -
提取前景目标的最小外接圆和最小外界矩形
2021-08-07 10:47:452) # 椭圆拟合 ellipse = cv2.fitEllipse(cnt) cv2.ellipse(image2, ellipse, (255, 255, 0), 2) io.imsave(r'C:\Users\Dell\Desktop\test.png',image2) 根据个人需要画指定的最小外接圆和最小外接矩形。... -
三维空间点集的最小外接矩形
2021-08-26 09:50:49前段时间毕设卡在求一系列点的最小外接矩形, 卡了好久在导师的帮助下终于解决了. 来分享下思路 说明 本篇所有的坐标系都是建立在右手坐标系下的情况. 虽然为了符合常识认知, 我会把这个坐标系做一个旋转, 但本质上它... -
基于arcgis的最小外接矩形获取方法
2021-07-06 10:09:52ArcToolbox => Data Management Tools => Features => Minimum Bounding Geometry -
opencv 最小外接矩形
2019-11-26 21:27:261. RotatedRect minRect = minAreaRect(contours[i]) (1)输入点集,如由findContours(image, contours, hiera, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE)查找得到的轮廓点集... (2)输出:RotatedRect 类矩形... -
MATLAB求二值图像的最小外接矩形
2021-05-03 21:36:46求二值图像的最小外接矩形算法描述步骤流程源代码结论 算法描述 计算最小外接矩形(MER)的一种方法是,将物体的边界以每次以一定的角度增量(例如3°)在90°范围内旋转。每旋转一次记录一次其坐标系方向上的外接... -
OpenCV找出最小外接矩形
2017-10-22 14:54:38提取轮廓找出最小外接矩形 -
使用opencv画最小外接矩形与最小外接圆
2015-09-27 16:26:20使用opencv画出图形的最小外接矩形与最小外接圆,首先求出图形的轮廓,设有滚动条可以选择最佳阈值,然后画出图形的最小外接圆与最小外接矩形,算法的效果很好! -
PCL1.8+Win10+VS2017+intel realsense获取平面点云的最小外接矩形
2020-09-04 17:14:08如上图所示,计算某个区域的平面点云的最小外接矩形。 需要注意的是这里是针对平面点云而言的。 具体步骤: 方法1:利用PCL自带的AABB或OBB最小外接矩形的api即可(但貌似并不是严密贴合的最小外接矩形) 方法2... -
C++ OpenCV4绘制轮廓最小外接矩形
2020-04-13 18:59:01C++ OpenCV4绘制轮廓最小外接矩形使用方法发生了一些变化: OpenCV2/3的代码: findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); cout<<"num="<<contours.size... -
java 求最小外接矩形框
2020-01-15 14:18:03https://zhuanlan.zhihu.com/p/97855964 https://blog.csdn.net/staHuri/article/details/87910716 https://github.com/huifer/planar_algorithm import ... import lombok.B... -
多边形最小外接矩形 旋转卡壳
2019-08-10 19:06:14给出4n个点,求最小面积的外接矩形。 解析: 因为可以分析出外接矩形一定有一条边与多边形重合,使用可以用旋转卡壳优化时间。 枚举每一条边,求出对面最远的点upupup,以及最两边的点l,rl,rl,r。 upupup的正确性与... -
Opencv视觉学习--画出最小外接矩形
2019-12-20 16:06:06opencv画出最小外接矩形,需要定义minRects类型的类,然后获取四个角点,并通过line函数将四个角点连接起来。minRects类型,可以返回四个角点的信息,我们可以通过各种计算方式,为自己提供更多的可用参数,从而更好... -
2018a版本MatLab利用regionprops函数获取图片中物体轮廓最小外接矩形
2019-06-06 20:43:032018a版本MatLab利用regionprops函数获取图片中物体最小外接矩形 本次内容,用于介绍利用matlab中的regionprops函数来获取图像区域中的物体的最小外接矩形信息(位置、长、宽)。 1.regionprops函数 regionprops函数... -
带JTS的最小边界矩形
2020-03-03 19:09:21需求:从整个集合中计算最小的边界矩形 Geometry类有一个'getEnvelopeInternal()'返回铭文信封,但'getEnvelope()'只返回另一个Geometry。 看看javadoc,看来返回的Geometry对象是: 与空的Geometry对象匹配的空... -
【OpenCV3图像处理】提取轮廓的凸包、外包矩形、最小外包矩形、最小外包圆
2017-06-15 20:47:09主要求得包含点集最小面积的矩形,这个矩形是可以有偏转角度的,可以与图像的边界不平行 函数调用形式: RotatedRect minAreaRect(InputArray points) 输入:二维点集,点的序列或向量 (Mat) 返回:... -
GIS中最小外包矩形(MBR) | 学步园
2020-12-24 19:22:15最小外包矩形就是包围图元,并且平行于X轴和Y轴的最小外界矩形。到底这个矩形有什么用,设想一下,一个几何体有很多顶点,我们要判断一个图形是否包含另一个图形,就要一个个点点判断,这样为大大延长处理的时间。那...