使用imrect - Matlab从边界坐标子集一个区域

时间:2017-10-18 13:34:33

标签: matlab

基于MultipartUploadOutputStream中的问题,我尝试使用imrect(虽然它限制为没有角度的矩形)。

然而,我的输出有问题,因为我没有得到任何x,y坐标。为什么呢?

脚本:

clc;
clear; 

I = imread('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Astroid_created_with_Elipses_with_a_plus_b_const.svg/330px-Astroid_created_with_Elipses_with_a_plus_b_const.svg.png'); 
I = rgb2gray(I);
I = imcomplement(I);
level = graythresh(I);
BW = im2bw(I,level);

BW_filled = imfill(BW,'holes');

boundaries = bwboundaries(BW_filled);  
figure,imshow(I) ;

hold on;

b = boundaries{1}; 
plot(b(:,2),b(:,1),'b','LineWidth',2);

xq = b(:,2);
yq = b(:,1);
h = imrect;

pos = getPosition(h);

X1 = round(pos(1));
Y1 = round(pos(2));
X2 = round(X1 + pos(3));
Y2 = round(Y1 + pos(4));

xv=[X1 X2];
yv=[Y1 Y2];

scatter(X1,Y1 ,'r' )  
scatter(X2,Y2 ,'r' )  

[in,on] = inpolygon(xq,yq,xv, yv) %xq and yq are inside or on the edge of the polygon area defined by xv and yv

1 个答案:

答案 0 :(得分:2)

inpoligon返回一个布尔值列表,以指示点xq和yq是否在多边形内。要获取相应点的坐标,可以使用逻辑索引;只需在代码末尾添加:

x_in = xq(in | on);
y_in = yq(in | on);
scatter(x_in,y_in ,'g.' )