-
2021-01-17 13:30:03
#include "cv.h"
#include "highgui.h"
#include
#include
//#include "otsu.h"
int main(int argc,char** argv)
{
IplImage *src,*gray,*bw,*dst;
CvMemStorage* storage=cvCreateMemStorage(0);
CvSeq* contour=0;
char* filename=argc==2?argv[1]:"5.jpg";
if(!filename)
printf("can't open the file:%d\n",filename);
src=cvLoadImage("D:\\xsz\\Debug\\图片\\3.jpg",1);
cvNamedWindow("image",1);
cvShowImage("image",src);
gray=cvCreateImage(cvSize(src->width,src->height),src->depth,1);
cvCvtColor(src,gray,CV_BGR2GRAY);
int hei,wid;
hei=gray->height;//注意此处是gray,otsu中要用到hei,wid,已在otsu.h中全局定义;
wid=gray->width;
printf("图像的高为:%d,宽为:%d\n\n",hei,wid);
cvNamedWindow("image2",1);
cvShowImage("image2",gray);
bw=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvThreshold(gray,bw,128,255,CV_THRESH_BINARY_INV);
cvNamedWindow("image4",1);
cvShowImage("image4",bw);
//wb=cvCloneImage(bw);
// cvNot(bw,wb); 只有当目标区域为黑色背景时候,才对其取反。
dst=cvCloneImage(src);
cvFindContours(bw,storage,&contour,sizeof(CvContour),CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);
for(;contour!=0;contour=contour->h_next)
{CvBox2D rect=cvMinAreaRect2(contour,storage);
CvPoint2D32f rect_pts0[4];
cvBoxPoints(rect, rect_pts0);
//因为cvPolyLine要求点集的输入类型是CvPoint**
//所以要把 CvPoint2D32f 型的 rect_pts0 转换为 CvPoint 型的 rect_pts
//并赋予一个对应的指针 *pt
int npts = 4,k=0;
int aaa=0,bbb=0;
CvPoint rect_pts[4], *pt = rect_pts;
printf("连通区域最小外接矩形顶点坐标分别为:\n");
for (int i=0; i<4; i++)
{
rect_pts[i]= cvPointFrom32f(rect_pts0[i]);
printf("%d %d\n",rect_pts[i].x,rect_pts[i].y);
aaa=(int)sqrt((pow((rect_pts[0].x-rect_pts[1].x),2)+pow((rect_pts[0].y-rect_pts[1].y),2)));
bbb=(int)sqrt((pow((rect_pts[0].x-rect_pts[3].x),2)+pow((rect_pts[0].y-rect_pts[3].y),2)));
if(aaa
{
k=aaa;
aaa=bbb;
bbb=k;
}
}
printf("最小外接矩形的长为:%d,宽为:%d。\n\n",aaa,bbb);
//chang=rect_pts[0]-rect_pts[3];
//kuan=rect_pts[0]-rect_pts[1];
//printf("最小外接矩形的长为:%d,宽为:%d\n",chang,kuan);
//画出Box
cvPolyLine(dst, &pt, &npts, 1, 1, CV_RGB(255,0,0), 1);
}
cvNamedWindow("image5",1);
cvShowImage("image5",dst);
cvWaitKey(0);//注意此句放的位置,放的不对则。。。
cvDestroyWindow("image");
cvDestroyWindow("image2");
cvDestroyWindow("image4");
cvDestroyWindow("image5");
cvReleaseImage(&src);
cvReleaseImage(&gray);
cvReleaseImage(&bw);
cvReleaseImage(&dst);
return 0;
}
更多相关内容 -
【测量篇】 最小外接矩形算法
2022-02-11 10:37:44今天我将带领大家从【测量篇】开始学习,要学习到的算法就是——最小外接矩形! 机器视觉定位方法很多如: 基于特征点匹配的、基于形状的、基于外截圆、外截矩形的等等。其中基于最小外接矩形的定位方法是我们常见...Hi 又见面啦!
不知道小伙伴们最近有没有好好学习软件呢
今天我将带领大家从【测量篇】开始学习,要学习到的算法就是——最小外接矩形!
机器视觉定位方法很多如:
基于特征点匹配的、基于形状的、基于外截圆、外截矩形的等等。其中基于最小外接矩形的定位方法是我们常见的一种定位方法。
其定位机理可以概述为:通过查找目标特征区域的最小外接矩形,依据矩形的位置及方向来定位目标物体的位置与姿态。再通过与模板图像的比对,从而计算出目标物体的偏移量与旋转角度,从而引导机械手进行相应的作业。
✔算法介绍
本算法用于统计在设定的 ROI 区域(蓝色框)内统计符合灰度要求的像素点组成的最小单位的矩形,设定区域面积的【合格范围】,从而判断产品OK/NG.
✔参数学习
【灰度低阈值】:设置值应<=检测位置边缘灰度值,确定检测位置需要被检测到的像素点灰度的最小值;
【灰度高阈值】:设置值应>=检测位置边缘灰度值,确定检测位置需要被检测到的像素点灰度的最大值;
【精度放大倍数】:此值越高计算出的结果值越精确,但同时会增加检测时间,
【最小阈值】:设置值应<=测试结果值,确定合格产品范围的最小值;
【最大阈值】:设置值应>=测试结果值,确定合格产品范围的最大值;
【收缩检测区域】:此功能可一键自动调整 ROI 区域大小范围(可多次调整),其中“膨胀尺寸”和“向外扩展”可设置单次向外扩展的大小
最小外接矩形算法实际应用场景 :检测物体(如玻璃瓶的瓶口高度)是否有半圆缺失等...
我们先打开SGVision软件,按【F4】快捷键进入算法页面,【导入需要检测的图片】—【选择测量栏目】—【选中最小外接矩形】。
假设我们要寻找这张图片
也就是白色区域的最小外
他的【RGB】是255,所以灰度最高最低阈值只要在这个范围就可以检测出来!
,时长00:21
检测一下
它就自动寻找出最小的外接矩形了
这个时候我们测出来的参数有长和宽,有最小宽度和最大宽度,那我们根据客户的要求看他是想要提取哪个参数。
我们再来换一个不同形状的来测试一下!
圆形的最小外接矩形
测试一下也很快就找到了最小的外接矩形
,时长00:09
那假设我们想要找的是最外面这个圆的最小外接矩形,那我们要怎么把里面的圆过滤掉从而精准的找到呢,答案就是调整参数。
我们把【灰度低阈值】与【灰度高阈值】调整为:
80—150
测试一下就找到了!
,时长00:13
所以大家在寻找最小外接矩形的时候要先确认好阈值范围,以免碰到多层图形的时候找错图形。
最小外接矩形呢比较适合不规则图形去寻找它的最小的长和宽,如果是圆形这样的图形精度就没那么准确了,总的来说这个算法相对简单,也没有那么多复杂的参数,非常好上手的!
完整视频教学:
https://mp.weixin.qq.com/s/TMGQs5uOgzca0yIjv9Re4w
https://mp.weixin.qq.com/s/TMGQs5uOgzca0yIjv9Re4w
-
一种获取图像区域最小外接矩形的算法及实现.pdf
2021-01-17 13:30:01nbsp图形图像一种获取图像区域最小外接矩形的算法及实现.pdf3页本文档一共被下载:次,您可全文免费在线阅读后下载本文档。 下载提示1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔...您所在位置:网站首页 > 海量文档
 > 计算机 > 图形图像
一种获取图像区域最小外接矩形的算法及实现.pdf3页
本文档一共被下载:
次,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
第30卷 第12期 计 算 机 工 程 2004年6月
Vol.30 № 12 Computer Engineering June 2004
·人工智能及识别技术 · 文章编号:1000—3428(2004)12 —0124—02 文献标识码:A 中图分类号: TP317.4
一种获取图像区域最小外接矩形的算法及实现
1,2 1 1
吴晓光 ,王涤琼,盛 慧
华东师范大学信息科学与技术学院,上海 ; 山西广播电视大学理工系,太原
(1. 200062 2 . 030027)
摘 要:给出了二值数字图像区域外接矩形的一种获取算法。对于图像方形点阵中的 近邻的情形,建立了标定区域边界的基本图 通过自动
8 ,
标定区域边界取得其像素点集,最后给出了获取图像区域最小外接矩形的实现方法。
关键词:图像区域;区域标定;最小外接矩形
An Algorithm and Implementaion for Obtaining Minimum Exterior
Rectangle of Image Region
1,2 1 1
WU Xiaoguang ,WANG Diqiong ,SHENG hui
(1.College of Information Science and Tchnology,East China Normal University,Shanghai 200062;
2.Dept. of Science and Engineering,Shanxi Radio & TV University, Taiyuan 030027)
【Abstract 】
In this paper,a new kind of algorithm for obtaining minimum exterior rectangle of digital image region is presented. It sets up the basic graphs
of image labeling for 8-site neighborhood digital images, based on basic graphs of image labeling, people can gain pixel pipointset of region boundary
directly by automatic labeling. Finally it provides the implementaion of obtaining minimum exterior rectangle of digital image region.
【Key words 】 ; ;
Image region Region labeling Minimum exterior rectangle
二值图像在图像分析中应用非常广泛,二值图像就是指 p p p p
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名:
验证码:
匿名?
发表评论
-
找最小外接矩形
2018-05-09 09:39:35利用opencv找图像中形状的最小外接矩形,进而可获得最小外接矩形的信息等 -
最小外接矩形matlab
2015-12-21 19:31:17minboundrect: Compute the minimal bounding rectangle of points in the plane % usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric) -
MATLAB中对目标进行最小外接矩形处理
2018-05-31 19:49:35在MATLAB中常常会用到对识别分割出来的物体进行最小外接矩形的处理,进而在原图中显示分割结果 这里常用的函数minboundrect.m function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) % ...说明:
在MATLAB中常常会用到对识别分割出来的物体进行最小外接矩形的处理,进而在原图中显示分割结果
这里常用的函数minboundrect.mfunction [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. % % % See also: minboundcircle, minboundtri, minboundsphere % % % 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,{'Qt'}); % 'Pp' will silence the warnings edges = convhull(x,y); % 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 done end % mainline end
测试文件:
close all; clc; I=imread('2.jpg'); bw=im2bw(I); [labelpic,num]=bwlabel(bw,8); [r c]=find(labelpic==1); [rectx,recty,area,perimeter]=minboundrect(c,r,'p'); %%'a'是按最小面积算,如果按边长算,用'p' imshow(bw); hold on line(rectx(:),recty(:),'color','g'); [r c]=find(labelpic==2); [rectx,recty,area,perimeter]=minboundrect(c,r,'p'); line(rectx(:),recty(:),'color','r'); [r c]=find(labelpic==3); [rectx,recty,area,perimeter]=minboundrect(c,r,'p'); line(rectx(:),recty(:),'color','r');
-
OpenCV中minAreaRect()最小外接矩形详解
2020-04-30 09:49:55使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是点集数组或向量(里面存放的是点的坐标),并且这个点集中的元素不定个数。 2、举例说明: 画一个任意四边形(任意多边形都... -
检测图像的最小外接矩形 matlab
2017-01-08 11:59:56检测图像的最小外接矩形 matlab代码 -
最小外接矩形思路以及实现
2019-02-25 08:23:04最小外接矩形 外接矩形计算 对一个凸多边形进行外接矩形计算,需要知道当前面的最大xy 和最小xy值,即可获得外接矩形 最小外接矩形计算 对凸多边形的每一条边都绘制一个外接矩形求最小面积。下图展示了计算流程 ... -
最小外接矩形
2018-07-26 16:22:192.求最小外接矩形 对于多边形 P 的一个外接矩形存在一条边与原多边形的边共线。 不仅不必去检测所有可能的方向, 而且只需要检测与多边形边数相等数量的矩形。 图示上述结论: 四条切线(红色), ... -
求解最小外接矩形
2018-12-27 14:15:43最小矩形(rec1)的解题报告 作者:冯浩 时间: 2007.10.11 文档类型/出处:NOI专刊 题目简述: 給出一个平面点集S,求一个面积最小的矩形使其包含S所有的点。 预备知识: 在求解这道题... -
minboundrect()关于最小外接矩形
2021-08-23 12:31:39minboundrect 最小外接矩形 :可以求二值图像最小外接矩形(a:面积最小;p:周长最小) 下面是minboundrect.m 求白色部分的最小外接。 function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) %% 矩形是... -
最小外接矩形(MBR)
2014-07-27 10:36:34最小外接矩形(MBR)可分为最小面积外接矩形(Minimum Area Bounding Rectangle,简称 MABR)和最小周长外接矩形(Minimum Perimter Bounding Rectangle, 简称MPBR)。通常情况下MABR与MPBR差异不大。 图像中物体... -
旋转卡壳算法求最小外接矩形代码
2021-08-28 21:52:19* @brief :获取由凸包传来的点集,计算最小外接矩形,采用旋转卡壳算法 * @param[in] :凸包传来的点集,逆时针顺序的点集 * @param[out] : * @return :返回最小外接矩形的四个点集。顺时针方向 */ CUBECOM_... -
matlab求最小外接斜矩形minboundrect方法
2020-12-05 23:11:13但是想用matlab实现,于是查到了John D’Errico写的matlab实现求最小外接斜矩形函数。(代码贴在最后,仅供学习使用) [rectx,recty,area,perimeter] = minboundrect(c,r,‘a’) 其中a表示以面积最小、如果是p的话则... -
求最小外接矩形的面积(C++实现)
2021-06-13 15:37:28求最小外接矩形的面积(C++实现)求最小外接矩形的原理C++实现代码 求最小外接矩形的原理 最近在做课程设计的过程中遇到了一个问题,需要求出一幅图像中物体的矩形度,但是求解矩形度需要用到该物体的最小外接矩形的... -
opencv之求各连通区域内目标的最小外接矩形及其长、宽
2021-01-17 13:30:0210、最小外接矩形及长宽的求法liuqingjie2@http://www.doczj.com/doc/96e75a41a8956bec0975e327.html#include "cv.h"#include "highgui.h"#include#include#include "otsu.h"int main(int argc,char** argv){... -
凸包最小外接矩形
2019-06-30 17:07:14获取点的凸包之后,可以实现以下获取凸包的最小外接矩形,获取思路: 1、以其中两点作为矩形的一条边 2、以该边作为x轴基坐标,并做y轴基坐标 3、将所有点以该基坐标进行旋转,找到以该边为基准的所有点的x坐标的... -
二值图像分析—轮廓最小外接矩形
2019-10-30 17:11:27OpenCV中最小外接矩形 说明 brief Finds a rotated rectangle of the minimum area enclosing the input 2D point set. 查找包含输入二维点集的最小区域的旋转矩形。 该函数计算并返回指定点集的最小区域边界矩形... -
基于MATLAB的多边形最小外接矩形计算
2022-01-07 16:13:09MATLAB中有关于计算二值区域最小外接矩形的函数——regionprops。但是此函数只能计算平行于坐标轴的矩形,这样求得的外接矩形有时并非真正意义上的‘最小’。 我个人对于这个问题的解决方法的灵感来源于以下两篇... -
轮廓的最小外接矩形、最小外接圆、三角形、椭圆等
2019-01-28 19:11:59的最小外接矩形(up-right boundidng:正的,没有旋转的矩形) Rect cv::boundingRect ( InputArray array ) 输入灰度图 或者2D 一组点, 存在std::vector 或者 Mat. 找到 包含一组点 的... -
MATLAB实现绘制连通域最小外接矩形
2018-05-16 19:10:32方法主要是利用面积最小实现。代码如下:function main() I=imread('4.jpg'); bw=im2bw(I); [r c]=find(bw==1);... % 'a'是按面积算的最小矩形,如果按边长用'p' imshow(bw);hold on line(rectx... -
opencv求最小外接矩阵
2018-11-08 19:46:40求最小外接矩阵的基本原理: ...旋转之后,求出新的minx,maxx,miny,maxy,计算此时的面积,直到面积达到最小,对应的即为最小外接矩形。 关于图像旋转参考:https://blog.csdn.net/zhouxuguang236/article/de... -
Python图片查找轮廓、多边形拟合、最小外接矩形操作实例
2019-07-29 12:11:32# 计算最小外接矩形顶点 h = int ( rect [ 1 ] [ 0 ] ) w = int ( rect [ 1 ] [ 1 ] ) if min ( h , w ) == 0 : ration = 0 else : ration = max ( h , w ) / min ( h , w ) #... -
Opencv——minAreaRect()计算最小外界矩形
2022-02-18 10:56:05RotatedRect:返回一个轮廓的外接矩形,包覆输入信息的最小斜矩形,是一个Box2D结构rect:(最小外接矩形的中心(x,y),(宽度,高度),旋转角度)。 旋转角度θ是水平轴(x轴)逆时针旋转,与碰到的矩形的第... -
python opencv minAreaRect 生成最小外接矩形的方法
2020-12-08 18:45:37python opencv minAreaRect 生成最小外接矩形的方法使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是点集数组或向量(里面存放的是点的坐标),并且这个点集不定个数。... -
halcon18算子:最小包围矩形smallest_rectangle2()
2020-08-12 09:20:37halcon的最小包围矩形smallest_rectangle2()算子: 输入: region 输出: row:最小包围矩形的中心点的行坐标 col:最小包围矩形的中心的列坐标 Phi:最小包围矩形的长边与图像坐标系x轴的夹角,范围为-1.... -
关于Matlab中函数minboundrect的报错解决,以及画出最小外接矩形的方法
2022-04-24 22:17:36目的:图像预处理之后,计算所有连通域的最小外接矩形的长和宽,根据长宽比以及面积作为阈值,筛选出符合条件的连通域,并画框。 问题:用函数 minboundrect 找出所有连通域的外接最小外接矩形的时候,报错! 报错...