2010-03-01 75 views
3

例如,如果我已经得到描述图像上的一个矩形如何使用MATLAB内插图像上的矢量坐标?

xy=[165 88; 
    401 88; 
    401 278; 
    165 278]; 

的载体。

我如何获得使用MATLAB内置函数以下矢量

[165 88; % increase X -  hold y 
166 88; 
167 88; 
    ... ; 
399 88; 
400 88; 
401 88; % hold  x - increase y 
401 89; 
401 90; 
401 91; 
    ... ; 
401 276; 
401 277; 
401 278; % decrease X -  hold y 
400 278; 
399 278; 
398 278; 
    ... ; 
167 278; 
166 278; 
165 278; % hold  x - decrease y 
165 277; 
165 276; 
    ... ; 
165 87]; 

还是需要使用for循环写呢?

该算法必须适用于具有n点和xy坐标的通用向量。使用IND2SUB

+0

正确地说,xy不是矢量,而是2列矩阵。 – yuk 2010-03-01 19:19:04

回答

4

如果您有图像处理工具箱,你可以通过创建多边形,然后找到轮廓的图像做到这一点:

xy=[165 88; 401 88; 401 278; 165 278]; 
%# create the image - check the help for impolygon for how to make sure that 
%# your line is inside the pixel 
img = poly2mask(xy(:,1),xy(:,2),max(xy(:,1))+3,max(xy(:,2))+3); 
figure,imshow(img) %# show the image 

%# extract the perimeter. Note that you have to inverse x and y, and that I had to 
%# add 1 to hit the rectangle - this shows one has to be careful with rectangular 
%# polygons 
boundary = bwtraceboundary(logical(img),xy(1,[2,1])+1,'n',8,inf,'clockwise'); 

%# overlay extracted boundary 
hold on, plot(boundary(:,2),boundary(:,1),'.r') 

编辑来展示如何使用bwtraceboundary和警告像素相抵消矩形。

+0

非常好!唯一的问题是“out”的排序。 “out”的相邻元素必须是2d曲线的相邻点:'( – EnneKappa 2010-03-01 20:39:27

+0

然后您应该使用bwtraceboundary而不是bwperim – Jonas 2010-03-01 21:19:39

+0

谢谢!!! boundary = bwtraceboundary(img,[xy(1,2)+1 xy 1,1)+1],'N'); – EnneKappa 2010-03-01 22:31:30

0

一种解决方案:

xy=[165 88; 401 88; 401 278; 165 278]; 
xmin = min(xy(:,1))-1; 
xmax = max(xy(:,1)); 
ymin = min(xy(:,2))-1; 
ymax = max(xy(:,2)); 

ncol=xmax-xmin; 
nrow=ymax-ymin; 

[xn yn]=ind2sub([nrow ncol],1:nrow*ncol); 
xypairs = [xn'+xmin yn'+ymin]; 
+0

但我只想要周长和通用向量xy可以描述一个多边形,而不仅仅是一个矩形。 – EnneKappa 2010-03-01 19:26:13

+0

对不起,没有得到最后一句话。您需要循环遍历多边形的每条边(只有n个循环),并通过线性回归计算坐标。 – yuk 2010-03-01 19:48:29

0

的快速和肮脏的方式来绘制直道到一个离屏矩阵是通过评估式a*X+b*Y=c

令h和w是宽度和高度的缓冲液:

X = repmat([0:w-1], h, 1) 
Y = repmat([0:h-1]', 1, w) 

对于每对点(X1,Y1) - >(X2,Y2)的a,b和c是:

a = y2-y1 
b = x1-x2 
c = x1*y2-x2*y1 

现在计算straigt:

st = a*X+b*Y-c 
st(abs(st)>1) = 1 
st = 1 - abs(st) 

矩阵st由含有反走样直线的交接AW * H矩阵通过点(x1,y1)和(x2,y2)。现在让我们从直行到一行屏蔽掉不需要的部分:

[xs] = sort([x1 x2]) 
st = st .* [zeros(h, xs(1)) ones(h, xs(2)-xs(1)) zeros(h, w-xs(2))] 
[ys] = sort([y1 y2]) 
st = st .* [zeros(ys(1), w) ; ones(ys(2)-ys(1), w) ; zeros(h-ys(2), w)] 

我们刚才手动绘制,没有任何明确的循环单行。对代码的效率没有任何保证:-)

最后:为上面的每个公式添加另一个维度(作为读者的练习留下)。