精华内容
下载资源
问答
  • matlab最小外接矩形
    2021-05-06 07:05:01

    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

    % 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));

    更多相关内容
  • % 'a'是按面积算的最小矩形,如果按边长用'p' figure imshow(bw); line(rectx(:),recty(:),'color','r'); %画直线 x0=rectx(1:4); % 去掉一个重复的值 y0=recty(1:4); % 去掉一个重复的值 xcenter=mean(x0); %去...

    保存为后缀名xx.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

    % 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
     

    调用方法:

    I=imread('xxx.bmp');
    bw=im2bw(I,0.5);  %二值化
    [r c]=find(bw==1);
    [rectx,recty,area,perimeter] = minboundrect(c,r,'a'); % 'a'是按面积算的最小矩形,如果按边长用'p'
    figure
    imshow(bw);
    line(rectx(:),recty(:),'color','r'); %画直线
    x0=rectx(1:4); % 去掉一个重复的值
    y0=recty(1:4); % 去掉一个重复的值
    xcenter=mean(x0);  %去中心值
    ycenter=mean(y0);
    hold  on
    plot(xcenter ,ycenter, '*')  %显示*号

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

    2014-06-13 13:28:42
    求多个目标的最小外接矩形,有说明,matlab
  • matlab计算目标最小外接矩形,主要利用minboundrect函数。
  • 最小外接矩形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)
  • matlab实现画最小外接矩形

    热门讨论 2011-07-12 21:31:18
    matlab实现的,画最小外接矩形。其中包括求出最小外接矩形的四个顶点坐标,周长,面积等参数。
  • 资源中包含了两个m文件。...minboundrect.m用于绘制运动目标的最小外接矩形框,可任意设置最小外接矩形框的角度。 本人做的是视频中运动车辆的检测,读者可自行更换检测算法以及参数调整以匹配不同的运动场景。
  • MATLAB求二值图像的最小外接矩形

    千次阅读 2021-05-03 21:36:46
    求二值图像的最小外接矩形算法描述步骤流程源代码结论 算法描述 计算最小外接矩形(MER)的一种方法是,将物体的边界以每次以一定的角度增量(例如3°)在90°范围内旋转。每旋转一次记录一次其坐标系方向上的外接...

    算法描述

    计算最小外接矩形(MER)的一种方法是,将物体的边界以每次以一定的角度增量(例如3°)在90°范围内旋转。每旋转一次记录一次其坐标系方向上的外接矩形边界点的最大和最小x、y值。旋转到某一个角度后,外接矩形的面积(或周长)达到最小。取面积最小的外接矩形的参数为主轴意义下的长度和宽度。
    旋转物体使外接矩形最小

    步骤流程

    1. 输入图像
      源图像

    2. 输出图像
      旋转角度从0°~90°,步长为10°,求得的MER,如下图;
      步长为10°
      旋转角度从0°~90°,步长为1°,求得的MER,如下图。
      步长为1°

    源代码

    %%%寻找图像最小外接矩形
    
    %%%先用迭代式阈值求法将图像变为二值图
    img = imread('test0.png');
    img = rgb2gray(img);
    Th = mean(img( : ));            %将灰度均值设为初始阈值
    newTh = 0;
    i = 1;
    while (Th - newTh) > 1
        pic1 = img;
        pic1(pic1 > Th) = 0;            %大于阈值置零方便求均值
        miu1 = mean(pic1(:));        %小于阈值的像素的灰度均值
        pic2 = img;
        pic2(pic2 < Th) = 0;            %小于阈值置零方便求均值 
        miu2 = mean(pic2( : ));      %大于阈值的像素的灰度均值
        if i ~= 1
            Th = newTh; 
        end
        i = i + 1;
        newTh = (miu1 + miu2) / 2;
    end
    imgTh = img;
    imgTh(imgTh < Th) = 1;
    imgTh(imgTh > Th) = 256;
    imgTh = 256 - imgTh;
    %%%先用迭代式阈值求法将图像变为二值图
    %%%旋转图像求其MER
    for angle = 0 : 1 : 90
        imgRotated = double(imrotate(imgTh,angle,'bicubic','loose'));  %求旋转后的图像
        %imshow(uint8(imgRotated));
        [row, col] = size(imgRotated);
    
        %%%判断最小外接矩形的边界
        for i = 1 : row
            if sum(imgRotated(i, :)) > col
                break;
            end
        end
        yMinTest = i;
    
        for i = row : -1 : 1
            if sum(imgRotated(i, :)) > col
                break;
            end
        end
        yMaxTest = i;
    
        for i = 1 : col
            if sum(imgRotated(:, i)) > row
                break;
            end
        end
        xMinTest = i;
    
        for i = col : -1 : 1
            if sum(imgRotated(:, i)) > row
                break;
            end
        end
        xMaxTest = i;
        %%%判断最小外接矩形的边界
        
        %%%计算面积
        XLU = xMinTest * cosd(angle) - yMinTest * sind(angle);
        YLU = xMinTest * sind(angle) + yMinTest * cosd(angle);
    
        XLD = xMinTest * cosd(angle) - yMaxTest * sind(angle);
        YLD = xMinTest * sind(angle) + yMaxTest * cosd(angle);
    
        XRU = xMaxTest * cosd(angle) - yMinTest * sind(angle);
        YRU = xMaxTest * sind(angle) + yMinTest * cosd(angle);
    
        XRD = xMaxTest * cosd(angle) - yMaxTest * sind(angle);
        YRD = xMaxTest * sind(angle) + yMaxTest * cosd(angle);
            
        l1 = sqrt((XLU - XRU) ^ 2 + (YLU - YRU) ^ 2);
        l2 = sqrt((XLU - XLD) ^ 2 + (YLU - YLD) ^ 2);
    
    
        nowSize = l1 * l2;
        %%%保存当前求得的MER
        if angle == 0 || nowSize < typicalSize
            xMin = xMinTest;
            yMin = yMinTest;
            xMax = xMaxTest;
            yMax = yMaxTest;
            typicalSize = nowSize;
            typicalAngle = angle;
            typicalImg = imgRotated;
        end
        %%%保存当前求得的MER
        lastSize = nowSize;
    end
    
    %%%重现
    XLU = xMin * cosd(typicalAngle) - yMin * sind(typicalAngle);
    YLU = xMin * sind(typicalAngle) + yMin * cosd(typicalAngle);
    
    XLD = xMin * cosd(typicalAngle) - yMax * sind(typicalAngle);
    YLD = xMin * sind(typicalAngle) + yMax * cosd(typicalAngle);
    
    XRU = xMax * cosd(typicalAngle) - yMin * sind(typicalAngle);
    YRU = xMax * sind(typicalAngle) + yMin * cosd(typicalAngle);
    
    XRD = xMax * cosd(typicalAngle) - yMax * sind(typicalAngle);
    YRD = xMax * sind(typicalAngle) + yMax * cosd(typicalAngle);
    %%%重现
    
    subplot(2,1,1);
    imshow(uint8(imgTh));
    title('原图像');
    
    out1 = imrotate(imgTh,typicalAngle,'bicubic','loose');
    rectx = [xMin, xMax, xMax, xMin, xMin];
    recty = [yMax, yMax, yMin, yMin, yMax];
    subplot(2,1,2);
    imshow(out1);
    title(['旋转角度为',num2str(typicalAngle),'°']);
    line(rectx(:),recty(:),'color','r');
    
    % out2 = imrotate(Figure1,-typicalAngle,'bicubic','loose');
    % imshow(out2);
    % rectx = [XLU, XLD, XRD, XRU, XLU];
    % recty = [YLU, YLD, YRD, YRU, YLU];
    % imshow(uint8(imgTh));
    % line(rectx(:),recty(:),'color','r');
    %%%旋转图像求其MER
    
    

    结论

    从输入输出图像中可以看到,通过旋转图像法求最小外接矩形,实验效果较好。通过改变旋转角度的步长可以使MER的计算变得更为精确,但也会使得程序运行时间增加。此外通过这一方法求MER,阈值分割的准确性也很重要,本次实验中选区的图像背景较为简洁,因此阈值分割效果也比较好。但在实际应用中,背景通常没有这么间接,因此在进行阈值分割时应当进行形态学的腐蚀与膨胀,以确保阈值分割的效果。一旦阈值分割出现一点点小瑕疵,整个程序都会出问题。

    原始图像在旋转55°之后的坐标系内,有主轴意义下的MER。

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

    下面是我循环操作后的一个效果。

    31d9b8a05a0394785b631aee9ac7c19b.png

    展开全文
  • 全部來自ilovematlab中文論壇,其中matlab較新的版本不再支持其中的有些函數,...首先是尋找最小外接矩形的代碼:function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)% minboundrect: Compute the...
  • 在此之前先介绍一个画矩形的函数:rectangle('Position',[0.59,0.35,3.75,1.37],'Curvature',[0,0],'LineWidth',2,'LineStyle','--')rectangle('Position',[x坐标,y坐标,宽,高],'Curvature',[曲率纵向,曲率横向],'...
  • MATLAB中有关于计算二值区域最小外接矩形的函数——regionprops。但是此函数只能计算平行于坐标轴的矩形,这样求得的外接矩形有时并非真正意义上的‘最小’。 我个人对于这个问题的解决方法的灵感来源于以下两篇...
  • 检测图像的最小外接矩形 matlab代码
  • MATLAB中对目标进行最小外接矩形处理

    万次阅读 多人点赞 2018-05-31 19:49:35
    MATLAB中常常会用到对识别分割出来的物体进行最小外接矩形的处理,进而在原图中显示分割结果 这里常用的函数minboundrect.m function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) % ...
  • 目的:图像预处理之后,计算所有连通域的最小外接矩形的长和宽,根据长宽比以及面积作为阈值,筛选出符合条件的连通域,并画框。 问题:用函数 minboundrect 找出所有连通域的外接最小外接矩形的时候,报错! 报错...
  • 该代码可用于求取一个多边形的最小外接矩形
  • 但是想用matlab实现,于是查到了John D’Errico写的matlab实现求最小外接矩形函数。(代码贴在最后,仅供学习使用) [rectx,recty,area,perimeter] = minboundrect(c,r,‘a’) 其中a表示以面积最小、如果是p的话则...
  • 通过目标的对角点,可以框出目标的最小外接矩形
  • 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...
  • 凸包最小外接矩形

    千次阅读 2019-06-30 17:07:14
    获取点的凸包之后,可以实现以下获取凸包的最小外接矩形,获取思路: 1、以其中两点作为矩形的一条边 2、以该边作为x轴基坐标,并做y轴基坐标 3、将所有点以该基坐标进行旋转,找到以该边为基准的所有点的x坐标的...
  • 简单多边形的最小外接矩形算法,适用于玻璃排样等。
  • matlab求二值图像最小外接矩形

    万次阅读 多人点赞 2016-07-18 16:44:45
    原文参照:...下载这个函数,用法如下 http://www.mathworks.com/matlabcentral/fileexchange/13624-minimal-bounding-rectangle url='http://www.ilovematlab.cn/atta
  • 最小外接矩形及长宽的求法liuqingjie2@163.com#include "cv.h"#include "highgui.h"#include #include #include "otsu.h"int main(int argc,char** argv){IplImage *src,*gray,*bw,*dst;CvMemStorage* storage=...
  • 基于凸壳边界的最小外接矩形拟合MATLAB程序。
  • 最小外接矩形法.PPT

    2021-04-24 12:05:17
    最小外接矩形法3 纹理分析 3.3 基于纹理分割 将图像分割成一些具有某种一致性的区域是图像分析的重要手段,一致性包括亮度、颜色或纹理等衡量标准。在机器视觉系统中,通常图像可以根据亮度标准进行分割,但在复杂的...
  • 文章目录一、基于图像内容最小外接矩形边框矫正二、基于霍夫变换的图像倾斜矫正 一、基于图像内容最小外接矩形边框矫正 前提: 图像有明显的轮廓,需要对图像进行降噪处理 特点: 对于表格矫正效果很好 缺点: 当...
  • 最小外接矩形的求取 ,求二值图像的最小外接矩形MATLAB程序,以及实验步骤
  • 实现二值图像最小外接矩形(正) 用于目标识别 散点图画框 % 原文链接:https://blog.csdn.net/rosfreshman/article/details/116380981 原文效果如下 旋转角度固定为0° 可以得到平行于坐标轴的矩形 %% 寻找图像...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 381
精华内容 152
关键字:

matlab最小外接矩形

matlab 订阅