-
Opencv绘制最小外接矩形、最小外接圆
2020-12-31 14:55:05Opencv中求点集的最小外结矩使用方法minAreaRect,求点集的最小外接圆使用方法minEnclosingCircle。 minAreaRect方法原型: RotatedRect minAreaRect( InputArray points ); 输入参数points是所要求最小外结矩的... -
python opencv minAreaRect 生成最小外接矩形
2017-08-03 11:05:331、方法: 使用python opencv返回点集cnt的最小外接矩形,所用...画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集cnt 存放的就是该四边形的4个顶点坐标(点集里面有4个点) cnt = np.array([[x1,y...方法
使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是点集数组或向量(里面存放的是点的坐标),并且这个点集中的元素不定个数。
举例说明
画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集 cnt 存放的就是该四边形的4个顶点坐标(点集里面有4个点)
cnt = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]]) # 必须是array数组的形式 rect = cv2.minAreaRect(cnt) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度) box = cv2.cv.BoxPoints(rect) # 获取最小外接矩形的4个顶点坐标(ps: cv2.boxPoints(rect) for OpenCV 3.x) box = np.int0(box) # 画出来 cv2.drawContours(img, [box], 0, (255, 0, 0), 1) cv2.imwrite('contours.png', img)
函数 cv2.minAreaRect() 返回一个Box2D结构rect:(最小外接矩形的中心(x,y),(宽度,高度),旋转角度),但是要绘制这个矩形,我们需要矩形的4个顶点坐标box, 通过函数 cv2.cv.BoxPoints() 获得,返回形式[ [x0,y0], [x1,y1], [x2,y2], [x3,y3] ]。得到的最小外接矩形的4个顶点顺序、中心坐标、宽度、高度、旋转角度(是度数形式,不是弧度数)的对应关系如下:
注意:
- 旋转角度θ是水平轴(x轴)逆时针旋转,直到碰到矩形的第一条边停住,此时该边与水平轴的夹角。并且这个边的边长是width,另一条边边长是height。也就是说,在这里,width与height不是按照长短来定义的。
- 在opencv中,坐标系原点在左上角,相对于x轴,逆时针旋转角度为负,顺时针旋转角度为正。所以,θ∈(-90度,0]。
-
最小外接矩形
2019-09-03 16:51:46使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是点集数组或向量(里面存放的是点的...举例说明:画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集 cnt 存放...https://blog.csdn.net/lanyuelvyun/article/details/76614872
使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是点集数组或向量(里面存放的是点的坐标),并且这个点集不定个数。
举例说明:画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集 cnt 存放的就是该四边形的4个顶点坐标(点集里面有4个点)
cnt = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]]) # 必须是array数组的形式 rect = cv2.minAreaRect(cnt) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度) box = cv2.cv.BoxPoints(rect) # cv2.boxPoints(rect) for OpenCV 3.x 获取最小外接矩形的4个顶点坐标 box = np.int0(box)
函数 cv2.minAreaRect() 返回一个Box2D结构rect:(最小外接矩形的中心(x,y),(宽度,高度),旋转角度),但是要绘制这个矩形,我们需要矩形的4个顶点坐标box, 通过函数 cv2.cv.BoxPoints() 获得,返回形式 [ [x0,y0], [x1,y1], [x2,y2], [x3,y3] ]。
得到的最小外接矩形的4个顶点顺序、中心坐标、宽度、高度、旋转角度(是度数形式,不是弧度数)的对应关系如下:
注意:
旋转角度θ是水平轴(x轴)逆时针旋转,与碰到的矩形的第一条边的夹角。
并且这个边的边长是width,另一条边边长是height。也就是说,在这里,width与height不是按照长短来定义的。
在opencv中,坐标系原点在左上角,相对于x轴,逆时针旋转角度为负,顺时针旋转角度为正。所以,θ∈(-90度,0]。
-
多边形最小外接矩形算法.ppt
2020-07-19 00:38:59多边形最小外接矩形算法 程鹏飞 题目 给出求一个多边形最小外接矩形的算法并证明求得的是最小外接矩形. 最小外接矩形英文简称为SMBR(smallest minimum bounding rectangle,它和MBR(minimum bounding rectangle)不... -
最小外接矩形思路以及实现
2019-02-25 08:23:04最小外接矩形 外接矩形计算 对一个凸多边形进行外接矩形计算,需要知道当前面的最大xy 和最小xy值,即可获得外接矩形 最小外接矩形计算 对凸多边形的每一条边都绘制一个外接矩形求最小面积。下图展示了计算流程 ...最小外接矩形
外接矩形计算
对一个凸多边形进行外接矩形计算,需要知道当前面的最大xy 和最小xy值,即可获得外接矩形
最小外接矩形计算
对凸多边形的每一条边都绘制一个外接矩形求最小面积。下图展示了计算流程
计算流程
-
旋转基础算法实现
- 旋转点基础
/** * 旋转点 * * @param point 被旋转的点 * @param center 旋转中心 * @param angle 角度 * @return 旋转后坐标 */ public static Coordinate get(Coordinate point, Coordinate center, double angle) { double cos = Math.cos(angle); double sin = Math.sin(angle); double x = point.x; double y = point.y; double centerX = center.x; double centerY = center.y; return new Coordinate(centerX + cos * (x - centerX) - sin * (y - centerY), centerY + sin * (x - centerX) + cos * (y - centerY)); }
-
凸包算法实现
Geometry hull = (new ConvexHull(geom)).getConvexHull();
-
获得结果
public static Polygon get(Geometry geom, GeometryFactory gf) { Geometry hull = (new ConvexHull(geom)).getConvexHull(); if (!(hull instanceof Polygon)) { return null; } Polygon convexHull = (Polygon) hull; System.out.println(convexHull); // 直接使用中心值 Coordinate c = geom.getCentroid().getCoordinate(); System.out.println("==============旋转基点=============="); System.out.println(new GeometryFactory().createPoint(c)); System.out.println("==============旋转基点=============="); Coordinate[] coords = convexHull.getExteriorRing().getCoordinates(); double minArea = Double.MAX_VALUE; double minAngle = 0; Polygon ssr = null; Coordinate ci = coords[0]; Coordinate cii; for (int i = 0; i < coords.length - 1; i++) { cii = coords[i + 1]; double angle = Math.atan2(cii.y - ci.y, cii.x - ci.x); Polygon rect = (Polygon) Rotation.get(convexHull, c, -1 * angle, gf).getEnvelope(); double area = rect.getArea(); // 此处可以将 rotationPolygon 放到list中求最小值 // Polygon rotationPolygon = Rotation.get(rect, c, angle, gf); // System.out.println(rotationPolygon); if (area < minArea) { minArea = area; ssr = rect; minAngle = angle; } ci = cii; } return Rotation.get(ssr, c, minAngle, gf); }
-
测试类
@Test public void test() throws Exception{ GeometryFactory gf = new GeometryFactory(); String wkt = "POLYGON ((87623.0828822501 73753.4143904365,87620.1073981973 73739.213216548,87629.1690996309 73730.4220136646,87641.882531493 73727.3112803367,87643.0997749692 73714.8683470248,87662.0346734872 73725.0120426595,87669.0676357939 73735.1557382941,87655.9484561064 73735.9672339449,87676.9120937514 73747.4634223308,87651.8909778525 73740.8362078495,87659.4649372597 73755.4431295634,87644.4522677204 73748.680665807,87645.5342619215 73760.7178512935,87635.2553170117 73750.9799034842,87630.5215923822 73760.3121034681,87623.0828822501 73753.4143904365))"; Polygon read = (Polygon) new WKTReader().read(wkt); Polygon polygon = MinimumBoundingRectangle.get(read, gf); // System.out.println(polygon); // System.out.println(polygon.getArea()); }
注
本文代码及可视化代码均放在 gitee 码云上 欢迎star & fork
-
-
matlab 提取一个图形对象的最小外接矩形 并计算出面积
2017-12-25 22:33:26function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) ...% 'a'是按面积算的最小矩形 [rectx,recty,area,perimeter] = minboundrect(c,r,'p'); imshow(bw);hold on line(rectx,recty);function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
% minboundrect: Compute the minimal bounding rectangle of points in the plane
% usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
%
% arguments: (input)
% x,y - vectors of points, describing points in the plane as
% (x,y) pairs. x and y must be the same lengths.
%
% metric - (OPTIONAL) - single letter character flag which
% denotes the use of minimal area or perimeter as the
% metric to be minimized. metric may be either 'a' or 'p',
% capitalization is ignored. Any other contraction of 'area'
% or 'perimeter' is also accepted.
%
% DEFAULT: 'a' ('area')
%
% arguments: (output)
% rectx,recty - 5x1 vectors of points that define the minimal
% bounding rectangle.
%
% area - (scalar) area of the minimal rect itself.
%
% perimeter - (scalar) perimeter of the minimal rect as found
%
%
% Note: For those individuals who would prefer the rect with minimum
% perimeter or area, careful testing convinces me that the minimum area
% rect was generally also the minimum perimeter rect on most problems
% (with one class of exceptions). This same testing appeared to verify my
% assumption that the minimum area rect must always contain at least
% one edge of the convex hull. The exception I refer to above is for
% problems when the convex hull is composed of only a few points,
% most likely exactly 3. Here one may see differences between the
% two metrics. My thanks to Roger Stafford for pointing out this
% class of counter-examples.
%
% Thanks are also due to Roger for pointing out a proof that the
% bounding rect must always contain an edge of the convex hull, in
% both the minimal perimeter and area cases.
%
%
% Example usage:
% x = rand(50000,1);
% y = rand(50000,1);
% tic,[rx,ry,area] = minboundrect(x,y);toc
%
% Elapsed time is 0.105754 seconds.
%
% [rx,ry]
% ans =
% 0.99994 -4.2515e-06
% 0.99998 0.99999
% 2.6441e-05 1
% -5.1673e-06 2.7356e-05
% 0.99994 -4.2515e-06
%
% area
% area =
% 0.99994
%
%
% See also: minboundcircle, minboundtri, minboundsphere
%
%
% Author: John D'Errico
% E-mail: woodchips@rochester.rr.com
% Release: 3.0
% Release date: 3/7/07% default for metric
if (nargin<3) || isempty(metric)
metric = 'a';
elseif ~ischar(metric)
error 'metric must be a character flag if it is supplied.'
else
% check for 'a' or 'p'
metric = lower(metric(:)');
ind = strmatch(metric,{'area','perimeter'});
if isempty(ind)
error 'metric does not match either ''area'' or ''perimeter'''
end
% just keep the first letter.
metric = metric(1);
end% preprocess data
x=x(:);
y=y(:);% not many error checks to worry about
n = length(x);
if n~=length(y)
error 'x and y must be the same sizes'
end% start out with the convex hull of the points to
% reduce the problem dramatically. Note that any
% points in the interior of the convex hull are
% never needed, so we drop them.
if n>3
edges = convhull(x,y);
%edges = convhull(x,y,{'Qt'}); % 'Pp' will silence the warnings% exclude those points inside the hull as not relevant
% also sorts the points into their convex hull as a
% closed polygon
x = x(edges);
y = y(edges);
% probably fewer points now, unless the points are fully convex
nedges = length(x) - 1;
elseif n>1
% n must be 2 or 3
nedges = n;
x(end+1) = x(1);
y(end+1) = y(1);
else
% n must be 0 or 1
nedges = n;
end% now we must find the bounding rectangle of those
% that remain.% special case small numbers of points. If we trip any
% of these cases, then we are done, so return.
switch nedges
case 0
% empty begets empty
rectx = [];
recty = [];
area = [];
perimeter = [];
return
case 1
% with one point, the rect is simple.
rectx = repmat(x,1,5);
recty = repmat(y,1,5);
area = 0;
perimeter = 0;
return
case 2
% only two points. also simple.
rectx = x([1 2 2 1 1]);
recty = y([1 2 2 1 1]);
area = 0;
perimeter = 2*sqrt(diff(x).^2 + diff(y).^2);
return
end
% 3 or more points.% will need a 2x2 rotation matrix through an angle theta
Rmat = @(theta) [cos(theta) sin(theta);-sin(theta) cos(theta)];% get the angle of each edge of the hull polygon.
ind = 1:(length(x)-1);
edgeangles = atan2(y(ind+1) - y(ind),x(ind+1) - x(ind));
% move the angle into the first quadrant.
edgeangles = unique(mod(edgeangles,pi/2));% now just check each edge of the hull
nang = length(edgeangles);
area = inf;
perimeter = inf;
met = inf;
xy = [x,y];
for i = 1:nang
% rotate the data through -theta
rot = Rmat(-edgeangles(i));
xyr = xy*rot;
xymin = min(xyr,[],1);
xymax = max(xyr,[],1);
% The area is simple, as is the perimeter
A_i = prod(xymax - xymin);
P_i = 2*sum(xymax-xymin);
if metric=='a'
M_i = A_i;
else
M_i = P_i;
end
% new metric value for the current interval. Is it better?
if M_i<met
% keep this one
met = M_i;
area = A_i;
perimeter = P_i;
rect = [xymin;[xymax(1),xymin(2)];xymax;[xymin(1),xymax(2)];xymin];
rect = rect*rot';
rectx = rect(:,1);
recty = rect(:,2);
end
end
% get the final rect% all doneend % mainline end%%上面是函数minboundrect 下面是操作步骤;!!!I=imread('3.jpg');
bw=im2bw(I);
[r c]=find(bw==1);
% 'a'是按面积算的最小矩形[rectx,recty,area,perimeter] = minboundrect(c,r,'p');
imshow(bw);hold on
line(rectx,recty); -
python opencv minAreaRect 生成最小外接矩形的方法
2020-12-26 00:42:28举例说明:画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集 cnt 存放的就是该四边形的4个顶点坐标(点集里面有4个点) cnt = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]]) # 必须是array数组的... -
python四边形转矩形_python opencv minAreaRect 生成最小外接矩形的方法
2020-12-08 18:45:37python opencv minAreaRect 生成最小外接矩形的方法使用python opencv返回点集...举例说明:画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集cnt 存放的就是该四边形的4个顶点坐标(点集里面有4个点)cnt ... -
求取SHP文件的最小外接矩形并裁剪图像
2019-09-03 21:29:12求取shp文件中每一个形状的最小外接矩形。 根据每一个形状的最小外接矩形裁剪图像。 已知数据: 一个shp文件,包含若干个形状。 2.shp文件对应的影像。 工具 ARCGIS10.4 python2.7(ARCGIS自带) 步骤 一、... -
求解最小外接矩形
2018-12-27 14:15:43最小矩形(rec1)的解题报告 作者:冯浩 时间: 2007.10.11 文档类型/出处:NOI专刊 题目简述: 給出一个平面点集S,求一个面积...简单地说,对于一个平面点集S,我们把完全包含该点集的最小的凸多边... -
Opencv 最小外接矩形合并拼接
2018-07-05 14:05:00扫描两次最小外接矩形,第一次扫描出的矩形是图一的小矩形,遍历vector指定一个合并最大距离(假设是80),达到指定距离使用画矩形函数将这两个矩形占据的组合区域染成实心矩形。 第二次扫描直... -
opencv for python (18) 边界矩形、最小外接圆、椭圆拟合、直线拟合
2018-01-25 16:56:43函数cv2.boundingRect返回四个参数(x,y)为矩形左上角的坐标,(w,h)是矩形的宽和高。 函数cv2.rectangle是绘制矩形函数 ...但是要绘制这个矩形需要矩形的 4 个角点,可以通过函数 cv2.boxPoints() 获得,最后 -
OpenCV-Python——第17.3章:轮廓形状拟合(边界矩形,最小外接圆...)及性质
2019-02-23 15:48:00边界矩形 最小外接圆 椭圆拟合 直线拟合 轮廓性质 综合举例 轮廓形状拟合 1 边界矩形 1.1 直边界矩形 一个直矩形(就是没有旋转的矩形)。它不会考虑对象是否旋转。 所以边界矩形的面积不是最小的。可以... -
使用opencv的minAreaRect方法生成检测对象的最小外接矩形
2020-09-08 09:08:56画一个任意四边形(任意多边形都可以)的最小外接矩形,那么点集cnt 存放的就是该四边形的4个顶点坐标(点集里面有4个点) cnt = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]]) # 必须是array数组的形式 rect = ... -
Halcon提取图像中的最小外接矩形 得出矩形的中心点和角度
2019-07-05 16:33:20由于这段时间在做一个视觉项目,里面由的产品类似于矩形, 使用模板匹配定位坐标系,边缘提取计算。可以做到精定位。 但是项目中,在相机视野内存在多个矩形产品(客户要求的定位精度要求:±0.5)比较松 所以我... -
(五)建筑物多边形化简系列——最小外接矩形的获取
2018-07-08 17:28:04最小外接矩形问题是在给出一个多边形(或一群点),求出面积最小且外接多边形的矩形的问题。这个问题看起来并不难,但是具体实现并不简单。除了调用现有的公开库之外,这里给出一种简单且易理解的方法。 算法的...
-
Unity ILRuntime框架设计
-
PHP深入理解-PHP架构布局
-
【硬核】一线Python程序员实战经验分享(1)
-
“Operation not permitted”报错
-
C52-源码
-
紫外区全角度光子晶体反射镜
-
系统设计:准备系统设计面试问题-源码
-
两个栈实现队列
-
精益开发治理的最佳实践,第3部分:角色和政策
-
Windows系统管理
-
2021年 系统架构设计师 系列课
-
ELF视频教程
-
斜光轴数字强度相关计量的像模糊容限
-
让界面更加清爽
-
Galera 高可用 MySQL 集群(PXC v5.6 + Ngin
-
FPYTDTlBRY
-
Glasterfs 分布式网络文件系统
-
深入理解分布式技术 - RocketMQ解析
-
Lambda 函数式接口Predicate(基础)(练习)
-
工程制图 AutoCAD 2012 从二维到三维