matlab 二值图像 求白色区域最小外接矩阵 长宽.docx_第1页
matlab 二值图像 求白色区域最小外接矩阵 长宽.docx_第2页
matlab 二值图像 求白色区域最小外接矩阵 长宽.docx_第3页
matlab 二值图像 求白色区域最小外接矩阵 长宽.docx_第4页
matlab 二值图像 求白色区域最小外接矩阵 长宽.docx_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Matlab关于二值图像的最小外接矩形问题借用minboundrect 现有函数。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.% See also: minboundcircle, minboundtri, minboundsphere% default for metricif (nargin3 edges = convhull(x,y); % 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 n1 % 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); returnend% 3 or more points. % will need a 2x2 rotation matrix through an angle thetaRmat = (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 hullnang = 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_imet % 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); endend%

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论