Matlab最小面积包围四边形

更新时间:2023-07-04 11:04:00 阅读: 评论:0

Matlab最⼩⾯积包围四边形
对于存在透视变换的物体,提取时最⼩⾯积包围矩形不能满⾜要求,google到⼀个求最⼩⾯积包围四边形的算法,虽然速度较慢。以提取书本为例,实验结果和代码如下。
booktest.m
I = imread('book1.jpg');
figure(1);
imshow(I);
Ir = I(:,:,1);
Ismall = imresize(Ir,0.25);
bw =~imbinarize(Ismall);
imshow(bw);
bw2 = bwareaopen(bw,1000);
imshow(bw2);
stats = regionprops(bw2,'BoundingBox','ConvexHull');
hold on;
for i=1:numel(stats)
wood是什么意思
%凸包
h1=impoly(gca,stats(i).ConvexHull);
api = iptgetapi(h1);
api.tColor('red');
%包围矩形
h2=imrect(gca,stats(i).BoundingBox);
api = iptgetapi(h2);
api.tColor('blue');
%包围盒(最⼩⾯积包围矩形)
point = stats(i).ConvexHull;
c = minBoundingBox(point');
cdfs是什么意思plot(c(1,[1:end 1]),c(2,[1:end 1]),'g')
%最⼩⾯积包围四边形
[qx,qy] = minboundquad(point(:,1),point(:,2));
h3=impoly(gca,[qx',qy']);
api = iptgetapi(h3);
api.tColor('yellow');
end
hold off;
%整理四个点位置
%记最左点为1,同最左的话,靠下为1
%      2
%1
%            3
%    4
quadVetices = [qx(1:4)',qy(1:4)'];
quadVetices  = quadVetices .* 4;
[quadVeticesSort,index] = sortrows(quadVetices);
if quadVeticesSort(1,1) == quadVeticesSort(2,1)
if quadVeticesSort(1,2) < quadVeticesSort(2,2)
k = index(1);
el
k = index(2);
end
el
k = index(1);
end
i = mod((k-1):(k+2),4)+1;
rRect = quadVetices(i,:);malaysian
%计算边长
len = zeros(4,1);
for i = 1:3
len(i) = sqrt(power(rRect(i,1)-rRect(i+1,1),2)+power(rRect(i,2)-rRect(i+1,2),2));
end
len(4) = sqrt(power(rRect(4,1)-rRect(1,1),2)+power(rRect(4,2)-rRect(1,2),2));
dsth = (len(1)+len(3))/2;
dstw = (len(2)+len(4))/2;
dstRect  = [0,dsth;0,0;dstw,0;dstw,dsth;];
%纠正透视变换
tform = estimateGeometricTransform(rRect, dstRect,...
'projective');
日语等级考试报名imgBp = imwarp(I, tform,...
'OutputView',imref2d([floor(dsth),floor(dstw)]));
imshow(imgBp);
现在总是报警告,暂时没时间细看了
In minboundquad (line 181)
In booktest (line 27)
警告: 矩阵为奇异⼯作精度。
minboundquad.m
我稍微改了下,
在第125⾏增加nedges = size(edges,1);
function[qx,qy,quadarea] = minboundquad(x,y)
% minboundquad: Compute the minimum area bounding quadrilateral of points in the plane % usage: [qx,qy] = minboundquad(x,y)
%
% arguments: (input)
%  x,y - vectors of points, describing points in the plane as
%        (x,y) pairs. x and y must be the same size.
%
% arguments: (output)
%  qx,qy - 5x1 vectors of points that define the minimum
%        area bounding quadrilateral.
%
% WARNING: Most 2-d convex hulls are fairly small.
% However, this code will be O(N^4), where N is the
% number of distinct edges in the convex hull of
% your data. So finding the bounding quadrilateral
% of 1000 points around the perimeter of a circle
% will take much time.
%
% Example usage:
%  x = randn(50,1);
%  y = randn(50,1);
%  [qx,qy] = minboundquad(x,y);
%  plot(x,y,'ro',qx,qy,'b-')
%
%
% See also: minboundcircle, minboundrect, minboundtri
%
%
% Author: John D'Errico
% E-mail:
% Relea: 1.0
% Relea date: 3/14/07
% preprocess data
x=x(:);
y=y(:);
% not many error checks to worry about
n = length(x);
if n~=length(y)
error('MINBOUNDQUAD:size','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.
grenoblexy = [x,y];
如何经营服装店
if n>3
打电话英文翻译edges = convhull(x,y);
% convhull returns a list of points around
% the perimeter. I prefer the convhulln form,
% where I have an explicit list of edges.
网站翻译
% where I have an explicit list of edges.
% Make it so.
edges = [edges(1:(end-1)),edges(2:end)];
elif n==3
% it is a triangle. I don't care how we
% traver it. Replicate a vertex into
% a quadrilateral.
qx = xy([12331],1);
qy = xy([12331],2);
quadarea = polyarea(qx,qy);
return
百特英语培训学校elif n==2
% a single edge
qx = xy([12221],1);
qy = xy([12221],2);
% no area to be found
quadarea = 0;
美国2016大选return
elif n==1
% a single point
qx = xy([11111],1);
qy = xy([11111],2);
quadarea = 0;
return
el
% empty begets empty
qx = [];
qy = [];
quadarea = 0;
return
end
nedges = size(edges,1);
% now we must find the bounding quadrilateral of tho % that remain.
% special ca small numbers of points. If we trip any
% of the cas, then we are done, so return.
if nedges == 3
qx = xy([1231],1);
qy = xy([1231],2);
return
elif nedges == 4
qx = xy([12341],1);
qy = xy([12341],2);
return
end
% more than 4 points.
% get the angle of each edge of the hull polygon edgeangles = atan2(xy(edges(:,2),2) - xy(edges(:,1),2), ...  xy(edges(:,2),1) - xy(edges(:,1),1));
% (The next two steps are probably superfluous.)
% work with all positive angles
k = edgeangles < 0;
edgeangles(k) = edgeangles(k) + 2*pi;
% sort the edges into increasing order of angle [edgeangles,tags] = sort(edgeangles);
edges = edges(tags,:);
% Look for concutive edges that have the same
% angles. This test will generally only trip if
% the data t has multiple collinear points
% around the perimeter.
angletol = eps*100;

本文发布于:2023-07-04 11:04:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/166716.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:包围   变换   透视   警告   时间   矩形   实验   提取
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图