• 最小矩形包围框，二值图像
• 对图像先进行灰度处理，接着进行二值化处理。。。。。。
• function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)%%ֵͼСӾif (nargin<3) || isempty(metric)metric = 'a';elseif ~ischar(metric)error 'metric must be a character flag if it is supplied...

function [rectx,recty,area,perimeter] = minboundrect(x,y,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
% if(x(1,1)==x(2,1)&&x(2,1)==x(3,1)&&x(3,1)== x(4,1))
if(x(1,1)==155&&x(2,1)==155&&x(3,1)== 155)
nedges = 0;
% elseif(y(1,1)==y(2,1)&&y(2,1)==y(3,1)&&y(3,1)== y(4,1))
% nedges = 0;
else
edges = convhull(x,y);
%edges = convhull(x,y,{'Qt'}); % '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;
end
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
一键复制
编辑
Web IDE
原始数据
按行查看
历史

展开全文
• minboundrect 最小外接矩形 ：可以求二值图像最小外接矩形（a:面积最小；p：周长最小） 下面是minboundrect.m 求白色部分的最小外接。 function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) %% 矩形是...
笔记
minboundrect 最小外接矩形 ：可以求二值图像最小外接矩形（a:面积最小；p：周长最小） 斜矩形
下面是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.
%  rectx,recty里面5个数，1-4表示从右上角开始顺时针的4个点的横纵坐标
%  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


这个是 minboxing.m 联合求斜矩形的长和宽
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));  %三角形勾股定理

% 长和宽 width height  斜矩形按大小复值
% wid = min(ds(1:2))
% hei = max(ds(1:2))
% 长和宽 width height  正矩形，平行于坐标轴的矩形，按此方法
wid = ds(2);
hei = ds(1);
end


可以求散点二值图像的最小外接main.m，可以画出单个目标的最小外接main2.m
主函数main.m minboundrect是求白色部分的最小外接，所以可能会用到二值图像取反。
I=imread('6.png');
[h,w] = size(I);
bw=im2bw(I);%二值化
bw =imcomplement(bw);%二值图像反相
[r,c]=find(bw==1); %返回行号和列号
% 'a'是按面积算的最小矩形，如果按边长用'p'
[rectx,recty,area,perimeter] = minboundrect(c,r,'p');
[wei, hei] = minboxing(rectx(1:end-1),recty(1:end-1)) %显示外接矩形长和宽
figure(2);imshow(bw);hold on
line(rectx,recty,'color','r');%画红色框

主函数main2.m
I=imread('7.png');
[h,w] = size(I);
bw=im2bw(I);%二值化
bw =imcomplement(bw);%二值图像反相
[labelpic,num]=bwlabel(bw,8); %num目标个数，labelpic目标索引

[r c]=find(labelpic==1);
% 'a'是按面积算的最小矩形，如果按边长用'p'
[rectx,recty,area,perimeter] = minboundrect(c,r,'p');
% [wei, hei] = minboxing(rectx(1:end-1),recty(1:end-1)) %显示外接矩形长和宽
figure(2);imshow(bw);hold on
line(rectx,recty,'color','r');

[r c]=find(labelpic==2);
% 'a'是按面积算的最小矩形，如果按边长用'p'
[rectx,recty,area,perimeter] = minboundrect(c,r,'p');
figure(2);imshow(bw);hold on
line(rectx,recty,'color','g');

[r c]=find(labelpic==3);
% 'a'是按面积算的最小矩形，如果按边长用'p'
[rectx,recty,area,perimeter] = minboundrect(c,r,'p');
figure(2);imshow(bw);hold on
line(rectx,recty,'color','b');

rectx = 330.2336 937.4036 848.9164 241.7464 330.2336 recty = 80.7667 326.9541 545.1893 299.0019 80.7667 wei =655.1822 hei =235.4923  最后一个目标的数据 rectx = 662 902 902 662 662 recty = 274 274 508 508 274 wei= 240 hei = 234  原本的目标是找到一个函数，可以求散点图最小外接矩形（平行于坐标轴），但这个函数不知道再哪里修改角度。所以另寻办法。 下面是效果，矩形是斜的  注意 matlab 的figure显示图像坐标原点在左上角 
展开全文
• [rectx,recty,area,perimeter] = minboundrect(c,r,‘a’) 其中a表示以面积最小、如果是p的话则是以边长最小 这里有个问题，用minboundrect函数求得的四个点顺序是什么？ 于是做了两张图验证了一下 放上结果： ...
最近在做课程大作业时看到opencv函数cv2.minAreaRect()， 但是想用matlab实现，于是查到了John D’Errico写的matlab实现求最小外接斜矩形函数。（代码贴在最后，仅供学习使用）
[rectx,recty,area,perimeter] = minboundrect(c,r,‘a’) 其中a表示以面积最小、如果是p的话则是以边长最小
这里有个问题，用minboundrect函数求得的四个点顺序是什么？ 于是做了两张图验证了一下
放上结果：

可以得到minboundrect函数得到的结果(rectx,recty)是从最上边的点开始，按照顺时针方向索引。
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.
%
%
% Example usage:
%  x = rand(50000,1);
%  y = rand(50000,1);
%  tic,[rx,ry,area] = minboundrect(x,y);toc
%
%  Elapsed time is 0.105754 seconds.
%
%  [rx,ry]
%  ans =
%      0.99994  -4.2515e-06
%      0.99998      0.99999
%   2.6441e-05            1
%  -5.1673e-06   2.7356e-05
%      0.99994  -4.2515e-06
%
%  area
%  area =
%      0.99994
%
%
%
%
% Author: John D'Errico
% E-mail: woodchips@rochester.rr.com
% Release: 3.0
% Release date: 3/7/07

% 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);
%edges = convhull(x,y,{'Qt'});  % '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
% get the final rect

% all done

end % mainline end

代码仅供学习
展开全文
• 本来目的是想将每个连通区域用一个斜的box框起来...minboundrect.m 函数function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) % minboundrect: Compute the minimal bounding rectangle of points in the
本来目的是想将每个连通区域用一个斜的box框起来，可是函数貌似针对整幅图片的。  正在改进  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.
%
%
%
%
% 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
使用实例1.
I=imread('E:\cutcut\whhhh.BMP'); %此处为图像所放路径和图片名字。
bw=im2bw(I,0.90);   %将图像二值化
bw = ~bw;%取反
bw = bwareaopen(bw,200,8);  %去除200以下的连通区域
img_reg= regionprops(bw);   %取连通区域，ima_reg默认有质心和box两个参数
bbboooxxx = cat(1,img_reg.BoundingBox); 数组保存所有box

for i=11
rect = [bbboooxxx(i,1),bbboooxxx(i,2),bbboooxxx(i,3),bbboooxxx(i,4)];
cutpic =imcrop(bw,rect);%每次裁一小块
cutpic1 = bwareaopen( cutpic,600,8);%去除附近的其他东西
[r c]=find(cutpic1==1);  %找出边界
[rectx,recty,area,perimeter] = minboundrect(c,r,'a');
% 'a'是按面积算的最小矩形，调用函数
figure
imshow(cutpic);
line(rectx(:),recty(:),'color','r');
end
使用实例2.  这是matlab论坛上一个大佬的，果然好用，虽然不是自己写的感觉乖乖的
function minb(x)
h=figure;
subplot(2, 2, 1); imshow(I);
title('原图');
I1 = rgb2gray(I);
subplot(2, 2, 2); imshow(I1);
title('灰度图');
I2 = medfilt2(I1);
bw1 = im2bw(I2, graythresh(I2));
%bw1=~bw1;
bw1= bwareaopen(bw1,50,4);
subplot(2, 2, 3); imshow(bw1);
B=bwboundaries(bw1);
title('二值图');
[L, n]=bwlabel(bw1, 4);
figure;
imshow(I);
stats = regionprops(L,'all');
Cen = cat(1, stats.Centroid);
hold on;
for i=1:1:n
boundary=B{i};
[rx,ry,area]=minboundrect(boundary(:,2),boundary(:,1));
hold on;
line(rx,ry ,'Color','r','LineWidth',2);
end
str = sprintf('共%d个', n);
title(str, 'Color', 'r');
end
展开全文
• matlab用于计算图片中图像的最小外接矩阵，用于图片识别
• 本资源包含求图片中最小矩形的函数（minboundrect.m）本函数已经流传多年，非常经典。还包括一个测试程序以及测试图片！本人已经测试过了，可以运行，希望对您有用！
• Matlab关于二值图像的最小外接矩形问题借用minboundrect 现有函数。function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)% minboundrect: Compute the minimal bounding rectangle of points in the ...
• 计算机视觉系统的搭建基于MATLAB，可以给大家一个参考。
• 执行额外的校准步骤来找到这些参数可以消除图片中的失真并得到更好的物体大小的近似值 参考 1.Measuring size of objects in an image with OpenCV 代码如下： coin_width minboundrect函数 function minboxing函数 ...
• ## 形状特征提取代码

千次阅读 2020-08-05 10:27:35
%外接矩阵中心点纵坐标 end end 上面Count_rectangle函数需要调用下面minboundrect函数 function [rectx,recty,area,perimeter] = minboundrect(x,y,metric) if (nargin) || isempty(metric) %narginp判断输入函数...
• Matlab 中并没有发现最小外接矩形的代码，为了方便下面提供最小外接矩形的代码：注：这个函数是源于网上找到的代码的改进版，原版不能检测水平线或者垂直线function [rectx,recty,area,perimeter] = minboundrect(x,...
• matlab计算目标最小外接矩形,主要利用minboundrect函数。
• 方法主要是利用面积最小实现。代码如下：function main() ...[rectx,recty,area,perimeter] = minboundrect(c,r,'a'); % 'a'是按面积算的最小矩形，如果按边长用'p' imshow(bw);hold on line(rectx...
• % 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, ...
• 这是一份用Minboundrect求最小外接矩形的代码详解，语言是Matlab
• 又是计算几何，我感觉最近对计算几何上瘾了。当然，工作上也会用一些，不过工作上一般直接调用boost的geometry库。上次写过最小包围圆，这次是最小包围矩形，要比最小包围圆复杂些。最小包围矩形可不一定是个直立的...
• 宁收宁马的费呢！老子自己做的，免费提供该大家，卖资料的垃圾，能收几个钱 拍一幅含多个人体目标的图像，利用外接盒、围盒和凸包对每个人体目标区域进行表达，并提取其骨架 main clc clear close all ...
• Matlab中的函数mod(a,m)的作用： 　取余数 　例如： 　mod(25,5)=0; 　mod(25,10)=5; 仅此。 转载于:https://www.cnblogs.com/TheTinkerJ/p/5764893.html
• Opencv绘制最小外接矩形、最小外接圆 原创 2016年08月20日 12:43:45 ...标签：最小外接矩形 /最小外接圆 /minAreaRect /minEnclosingCircle ...Opencv中求点集的最小外结矩使用方法minAreaRect，求点集的最小...
• regionprops是用来度量图像中不同区域属性的函数。 ... ...表示形式是：stats=regionprops(L,properties) ...测量标注矩阵L中每一个标注区域的一系列属性。L中不同的正整数元素对应不同的区域，例如：L中等于整数1的...
• clc;clear all;close all src=imread('C:\Users\think\Desktop\12.jpg'); subplot(1,2,1) imshow(src); I = mat2gray(src); J = imrotate(I,-208); subplot(1,2,2) imshow(J); map0=J(290:949,446:940)......
• ## MATLAB报错：未定义函数或变量

万次阅读 多人点赞 2017-03-16 23:45:12
出现这种错误一般是因为定义的函数和当前的工作环境不在一块导致。 一般情况MATLAB工作环境默认为C盘，但我们有时候会将自己编写的函数放在其他盘。当我们调用这个函数是就会出现 未定义函数或变量：‘’xxx‘’。...
• To complete the installation, Open Matlab, then choose "File"->"Set Path"->"Add with Subfolders" and select the root path of your installation. As a result, all subfolders will be added to Matlab's pa