• ## 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.
%
%
%
%
% 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
• matlab计算目标最小外接矩形,主要利用minboundrect函数。
• minboundrect: Compute the minimal bounding rectangle of points in the plane % usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
• % '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.
%
%
%
%
% 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

调用方法：

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, '*')  %显示*号

展开全文
• 资源中包含了两个m文件。...minboundrect.m用于绘制运动目标的最小外接矩形框，可任意设置最小外接矩形框的角度。 本人做的是视频中运动车辆的检测，读者可自行更换检测算法以及参数调整以匹配不同的运动场景。
• 检测图像的最小外接矩形 matlab代码
• 2018a版本MatLab利用regionprops函数获取图片中物体最小外接矩形 本次内容，用于介绍利用matlab中的regionprops函数来获取图像区域中的物体的最小外接矩形信息（位置、长、宽）。 1.regionprops函数 regionprops函数...

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

### 1.regionprops函数

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

###### 描述：

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’ 图像各个区域边界地区的周长

### 2.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

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

展开全文
• 求二值图像的最小外接矩形算法描述步骤流程源代码结论 算法描述 计算最小外接矩形（MER）的一种方法是，将物体的边界以每次以一定的角度增量（例如3°）在90°范围内旋转。每旋转一次记录一次其坐标系方向上的外接...

# 算法描述

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

# 步骤流程

1. 输入图像

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

旋转角度从0°~90°，步长为1°，求得的MER，如下图。

# 源代码

%%%寻找图像最小外接矩形

%%%先用迭代式阈值求法将图像变为二值图
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。

展开全文
• MATLAB中常常会用到对识别分割出来的物体进行最小外接矩形的处理，进而在原图中显示分割结果 这里常用的函数minboundrect.m function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) % ...
• 方法主要是利用面积最小实现。代码如下：function main() I=imread('4.jpg'); bw=im2bw(I); [r c]=find(bw==1);... % 'a'是按面积算的最小矩形，如果按边长用'p' imshow(bw);hold on line(rectx...
• 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实现的，画最小外接矩形。其中包括求出最小外接矩形的四个顶点坐标，周长，面积等参数。
• 但是想用matlab实现，于是查到了John D’Errico写的matlab实现求最小外接矩形函数。（代码贴在最后，仅供学习使用） [rectx,recty,area,perimeter] = minboundrect(c,r,‘a’) 其中a表示以面积最小、如果是p的话则...
• 原文参照：...下载这个函数，用法如下 http://www.mathworks.com/matlabcentral/fileexchange/13624-minimal-bounding-rectangle url='http://www.ilovematlab.cn/atta
• 最小外接矩形 外接矩形计算 对一个凸多边形进行外接矩形计算，需要知道当前面的最大xy 和最小xy值，即可获得外接矩形 最小外接矩形计算 对凸多边形的每一条边都绘制一个外接矩形求最小面积。下图展示了计算流程 ...
• 利用MATLAB将一张图片中的多个连同区域用矩形框圈起来，实现单一图片中多目标的最小包围矩形的效果。
• 这个是使用matlab来计算二值图像中的前景区域的最小外界矩形，图像中只能够有一个前景区域，用于单目标跟踪最好
• 想学习OpenCV进行图像处理的可以参考下，若需要里面的头文件联系：liuqingjie2@163.com
• 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=...
• 'BoundingBox'：是1行ndims(L)*2列的向量，即包含相应区域的最小矩形。BoundingBox 形式为 [ul_corner width]，这里 ul_corner 以 [x y z ...] 的坐标形式给出边界盒子的左上角、boxwidth 以 [x_width y_width ...] ...
• 给定点集组成任意多边形，使用matlab求出包含所有点的最小外接圆。
• 实现二值图像最小外接矩形（正） 用于目标识别 散点图画框 % 原文链接：https://blog.csdn.net/rosfreshman/article/details/116380981 原文效果如下 旋转角度固定为0° 可以得到平行于坐标轴的矩形 %% 寻找图像...
• minboundrect 最小外接矩形 ：可以求二值图像最小外接矩形（a:面积最小；p：周长最小） 下面是minboundrect.m 求白色部分的最小外接。 function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) %% 矩形是...
• 这是一份用Minboundrect求最小外接矩形的代码详解，语言是Matlab
• 这个是matlab计算最小包围矩形，可以二值图像中的多个前景区域来求得其最小包围矩形，非常有用的
• 求点集的最小面积外接矩形 void Rotate(float& x, float& y, float angle) { float a = x * cos(angle) - y * sin(angle); float b = x * sin(angle) + y * cos(angle); x = a; y = b; } void Rect...
• 最小外接矩形 https://bitbucket.org/william_rusnack/minimumboundingbox/src/master/ 最大内接矩形 ...
• 最小外接矩形的求取 ，求二值图像的最小外接矩形MATLAB程序，以及实验步骤

...

matlab 订阅