精华内容
下载资源
问答
  • Matlab 最小外接矩形

    万次阅读 热门讨论 2016-05-12 15:07:27
    Matlab 中并没有发现最小外接矩形的代码,为了方便 下面提供最小外接矩形的代码: function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) % minboundrect: Compute the minimal bounding rectangle of ...

    Matlab 中并没有发现最小外接矩形的代码,为了方便

    下面提供最小外接矩形的代码: 

    注:这个函数是源于网上找到的代码的改进版,原版不能检测水平线或者垂直线


    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 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
    
    
    
    % if var(x)==0
        
    % 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 
        
        %%%%%%%%%%%%%%%%%%%%%%%%%
        if (var(x)== 0|| var(y)==0)
            if var(x)== 0
                x = [x-1;x(1); x+1 ];
                y = [y ;y(1);y];
                flag = 1;
            else
                y = [y-1;y(1); y+1 ];
                x = [x ;x(1);x];
                flag = 1;
            end
            
        else
            flag = 0;
         %%%%%%%%%%%%%%%%%%%%%%
        edges = convhull(x,y);  % 'Pp' will silence the warnings
      
        end
    
      % exclude those points inside the hull as not relevant
      % also sorts the points into their convex hull as a
      % closed polygon
      
      %%%%%%%%%%%%%%%%%%%%
      if flag == 0 
      %%%%%%%%%%%%%%%%%%%%    
          
      x = x(edges);
      y = y(edges);
      %%%%%%%%%%%%%%%%%%
      end
      %%%%%%%%%%%%%
      % 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




    当然这段代码并没有获取到外接矩形的长和宽,下面我在写一个函数,就可以获得对应外接矩形的长和宽

    function [ wid hei ] = minboxing( d_x , d_y )
    %minboxing Summary of this function goes here
    %   Detailed explanation goes here
    dd = [d_x, d_y];
    dd1 = dd([4 1 2 3],:);
    
    ds = sqrt(sum((dd-dd1).^2,2));
    wid = min(ds(1:2));
    hei = max(ds(1:2));
    
    end
    

    这里默认为较短的距离为宽,较长的距离为长。


    调用代码如下:注(dataX, dataY为需要计算最小外接矩形的数据。)

    [recty,rectx,area,perimeter] =  minboundrect(dataX, dataY);
     [wei hei] = minboxing(rectx(1:end-1),recty(1:end-1));





    展开全文
  • matlab最小外接矩形

    2014-06-13 13:28:42
    求多个目标的最小外接矩形,有说明,matlab
  • matlab实现画最小外接矩形

    热门讨论 2011-07-12 21:31:18
    matlab实现的,画最小外接矩形。其中包括求出最小外接矩形的四个顶点坐标,周长,面积等参数。
  • matlab计算目标最小外接矩形,主要利用minboundrect函数。
  • 检测图像的最小外接矩形 matlab代码
  • 最小外接矩形matlab

    2015-12-21 19:31:17
    minboundrect: Compute the minimal bounding rectangle of points in the plane % usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
  • 资源中包含了两个m文件。...minboundrect.m用于绘制运动目标的最小外接矩形框,可任意设置最小外接矩形框的角度。 本人做的是视频中运动车辆的检测,读者可自行更换检测算法以及参数调整以匹配不同的运动场景。
  • 最小外接矩形

    2018-06-01 19:54:47
    这是一份用Minboundrect求最小外接矩形的代码详解,语言是Matlab
  • MATLAB中常常会用到对识别分割出来的物体进行最小外接矩形的处理,进而在原图中显示分割结果 这里常用的函数minboundrect.m function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) % ...

    说明:

    在MATLAB中常常会用到对识别分割出来的物体进行最小外接矩形的处理,进而在原图中显示分割结果
    这里常用的函数minboundrect.m

    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 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'); 
    展开全文
  • 2018a版本MatLab利用regionprops函数获取图片中物体最小外接矩形 本次内容,用于介绍利用matlab中的regionprops函数来获取图像区域中的物体的最小外接矩形信息(位置、长、宽)。 1.regionprops函数 regionprops函数...

    2018a版本MatLab利用regionprops函数获取图片中物体最小外接矩形

    本次内容,用于介绍利用matlab中的regionprops函数来获取图像区域中的物体的最小外接矩形信息(位置(x,y)、宽(△x)、高(△y))。

    1.regionprops函数

    regionprops函数是matlab中一个用来度量图像区域属性的函数,常用于编辑区域面积分布,显示区域总数。其具体信息可在官方帮助文档中查看:
    https://ww2.mathworks.cn/help/images/ref/regionprops.html

    它的语法如下:
    STATS = regionprops(L,properties)
    描述:

    1、测量标注矩阵 L中每一个标注区域的一系列属性。L 中不同的正整数元素对应不同的区域, 例如:L 中等于整数1的元素对应区域1;L 中等于整数2的元素对应区域2;以此类推。L数据类型支持多种,逻辑、数值皆可。
    2、返回值STATS 是一个长度为 max(L(:))的结构数组,结构数组的相应域定义了每一个区域相应属性下的度量。 properties 可以是由逗号分割的字符串列表,包含字符串的单元数组,单个字符串 ‘all’ 或者 ‘basic’。如果 properties 等于字符串 ‘all’,则所有下述字串列表中的度量数据都将被计算,如果properties 没有指定或者等于 ‘basic’,则属性 ‘Area’、‘Centroid’ 和’BoundingBox’ 将被计算。

    具体属性如下:

    'Area’图像各个区域中像素总个数
    ‘BoundingBox’ 包含相应区域的最小矩形
    ‘Centroid’ 每个区域的质心(重心)
    ‘MajorAxisLength’ 与区域具有相同标准二阶中心矩的椭圆的长轴长度(像素意义下)
    ‘MinorAxisLength’ 与区域具有相同标准二阶中心矩的椭圆的短轴长度(像素意义下)
    ‘Eccentricity’ 与区域具有相同标准二阶中心矩的椭圆的离心率(可作为特征)
    ‘Orientation’ 与区域具有相同标准二阶中心矩的椭圆的长轴与x轴的交角(度)
    ‘Image’ 与某区域具有相同大小的逻辑矩阵
    ‘FilledImage’ 与某区域具有相同大小的填充逻辑矩阵
    ‘FilledArea’ 填充区域图像中的on像素个数
    ‘ConvexHull’ 包含某区域的最小凸多边形
    ‘ConvexImage’ 画出上述区域最小凸多边形
    ‘ConvexArea’ 填充区域凸多边形图像中的on像素个数
    ‘EulerNumber’ 几何拓扑中的一个拓扑不变量——欧拉数
    ‘Extrema’ 八方向区域极值点
    ‘EquivDiameter’ 与区域具有相同面积的圆的直径
    ‘Solidity’ 同时在区域和其最小凸多边形中的像素比例
    ‘Extent’ 同时在区域和其最小边界矩形中的像素比例
    ‘PixelIdxList’ 存储区域像素的索引下标
    ‘PixelList’ 存储上述索引对应的像素坐标
    ‘Perimeter’ 图像各个区域边界地区的周长

    参考:
    百度百科:https://baike.baidu.com/item/regionprops/3208753?fr=aladdin
    matlab帮助文档:https://ww2.mathworks.cn/help/images/ref/regionprops.html

    2.regionprops函数获取最小外接矩形信息

    通过前面对regionprops函数的简单介绍,我们可以了解到该函数可用于获取图像中各个区域中像素总个数、相应区域的最小矩形、每个区域的质心、估计图像中圆形对象的中心和半径、甚至填充…
    所以这次用了regionprops函数来获取二值图片中物体最小外接矩形,下面直接放代码:
    // An highlighted block
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %                       regionprops函数 求得多张图片中区域的最小外接矩形
    %1%      该regionprops函数直接用于需要的图片上就可得到图片中所有连通区域的最小外接矩形位置、长、宽信息
    %  我下面函数,只是因为我现在有filenums张图片,图片中为人体轮廓,现在需要求得每一张图片中人体轮廓的
    %  最小外接矩形,因而用到一些循环等语句。
    %2%      regionprops函数的返回值为结构体;如果用其 regionprops(imbiEdgepic(:,:,i),'BoundingBox')
    %imbiEdgepic(:,:,i)(imbiEdgepic二值图图集中的第i张图)的'BoundingBox'(最小外接矩形);
    %  那么它的返回值stats为结构体,该结构体中包含第i张图片中  ****所有连通区域  最小外接矩形的位置、长宽信息 ****
    % (如有2个连通区域,则返回的stats结构体具有两最小外接矩形的信息)
    %  其矩形信息调用方法为:a = stats.BoundingBox; % stats.BoundingBox便是位置、长、宽信息
    %                                             % [x,y,wight(△x),heigh(△y)]
    %
    % % % % 始终注意一点:
    %          该函数是对图片中所有连通区域求最小外接矩形!!!
    %          如果图片中连通区域为多个,返回的结构体也是一次性的多维结构体!!! 
    % https://blog.csdn.net/BinHeon
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ... 
    ...
    ... 
    figure('NumberTitle','off','Name','步态周期检测-最小外接矩形'); % figure显示名字
    %%%% %%%% 获得每个轮廓的最小外接矩形
    for i=1:filenums  
        imbiEdgepic(:,:,i) = imbinarize(Morphology(:,:,i)); % 将图集中第i张图片二值化处理
        %%%% regionprops函数用于返回图片的属性
        BoundBs(1,i) = regionprops(imbiEdgepic(:,:,i),'BoundingBox'); % 利用函数度量图像区域属性,这里度量最小外接矩形
                                                                 % 返回结构数据,其内是有关最小外接矩形的位置
                                                                  % 长、宽信息,调用格式:s.BoundingBox
                                                                   % s.BoundingBox = [x,y,wight(△x),heigh(△y)]
                                                                    % 始终注意,该函数是对图片中所有连通区域求最小外接矩形!!!
                                                                     % 如果图片中连通区域为多个,返回的结构体也是一次性的多维结构体!!!
        %%%% 利用画矩形函数rectangle函数,将求得的第i张图中物体最小外接矩形 画在 第i张图片上
        figure;imshow(Morphology(:,:,i));title(['No.4-',num2str(i)]); % 首先显示第i张图片
        hold on % 画图保持
        rectangle('position',BoundBs(1,i).BoundingBox,'edgecolor','r'); % 把矩形位置、长宽信息传入rectangle函数,画出最小外接矩形
        hold off
    
    end
    
    
    下面是我循环操作后的一个效果。

    在这里插入图片描述

    展开全文
  • 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...
         方法主要是利用面积最小实现。代码如下:
    function main()
    I=imread('4.jpg');
    bw=im2bw(I);
    [r c]=find(bw==1);
    [rectx,recty,area,perimeter] = minboundrect(c,r,'a'); % 'a'是按面积算的最小矩形,如果按边长用'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.
    %
    %
    % 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);  % '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
    

    结果如下:


    侵删。

    展开全文
  • 但是想用matlab实现,于是查到了John D’Errico写的matlab实现求最小外接矩形函数。(代码贴在最后,仅供学习使用) [rectx,recty,area,perimeter] = minboundrect(c,r,‘a’) 其中a表示以面积最小、如果是p的话则...
  • matlab求二值图像最小外接矩形

    万次阅读 2016-07-18 16:44:45
    原文参照:...下载这个函数,用法如下 http://www.mathworks.com/matlabcentral/fileexchange/13624-minimal-bounding-rectangle url='http://www.ilovematlab.cn/atta
  • matlab 任意轮廓外接矩形代码及其实现; clc;clear;close all; I=imread('00121.bmp'); I=rgb2gray(I); subplot(3,3,1);imshow(I); % M=im2double(I);%将unit8转成double型 subplot(3,3,2),imhist(I); M1=...
  • function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) ...% 'a'是按面积算的最小矩形 [rectx,recty,area,perimeter] = minboundrect(c,r,'p'); imshow(bw);hold on line(rectx,recty);
  • 我将一幅图经过二值化后将相连的部分求得了它们的最小外接矩形,接着我想将这些外接矩形进行判断是否相交或者包含,若成立则将这两个区域合并![图片说明]...
  • 检测图像的最小外接矩形 matlab 包括a-z ,0-9
  • MATLAB分割小实验

    2016-12-09 17:19:45
    对辣椒图像的分割程序,由于辣椒的弯曲程度不同,直接用外接矩形分割会存在重叠的情况,吧叶柄去掉再外接矩形分割比较好(还有用最小外接凸边形);记下流程,以后做图像分割自己可以借鉴。在图像分割中,最小外接...
  • matlab求凸包

    千次阅读 2016-05-24 15:05:25
    然后用代码求出其凸包及最小外接矩形,代码如下(注意:代码可直接使用,但具体怎么调用的我还不太明白,纯当记录使用,参考了csdn一位博主的代码,他的代码里I和bw用混了)。 figure; imshow(pure); stats
  • 做完二值化处理后,我们想进行分割,然后把每一块长宽调整到位,用最小外接矩形框起来,那么就有一个这样的问题 我可以把每一块叫做im_1,im_2,如果你不嫌麻烦,可以写8次重复的代码,所以我们希望通过8次循环,那么...
  • 目标跟踪的方法有很多,背景差分法, 差分帧法,mean-shift法等等, 这里主要说说最简单的...我们使用一个最小外接矩形将I圈出,并标出“形心”(实际上是矩形的中心,而非I的重心),就能获知完成对于目标的跟踪了。
  • 虚线点云相关

    2018-10-30 15:46:00
    Road Markings Extraction里面的工作是在陈启超写...虚线点云的最小外接矩形角点坐标(matlab): fd=fopen('C:\Users\Qin\Desktop\1112.txt','w');for i=1:size(breakLines,2) for k=1:size(breakLines{1,1},2) ...
  • 代码存放路径: 研一下学期课程/现代测量 matlab :实现视频图像的预处理,二值化之类 ,...python代码:利用minAreaRect()最小外接矩形,实现旋转角度的测量。(仅实现了图片,没有对摄像头或者视频试验) ...
  • matlan图像处理(一)

    2015-07-14 15:41:22
    师兄给了个任务,让我用matlab处理图片。...简单说是把box框出的物体用最小外接正方形抠出来输出单个图片。一种格式是原始图片,一种格式是图片中含有box框框。另外在统计box长宽的数目做出直方图。  任务不

空空如也

空空如也

1 2
收藏数 26
精华内容 10
关键字:

matlab最小外接矩形

matlab 订阅