交互式选择多边形区域

clc
clear
[filename,pathname,q]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.png'});
if q>0
path=strcat(pathname,filename);
I = imread(path);
figure
imshow(I);
end
p=size(I,3);
if p==3
r=I(:,:,1);
g=I(:,:,2);
b=I(:,:,3);
I1=rgb2gray(I);
else
    I1=I;
end
hold on
[x,y,c]=ginput(1);
m(1)=x;
n(1)=y;
plot(x,y,'r');
k=2;
while(c==1)
    [x1,y1,c1]=ginput(1);
    if c1==1
    m(k)=x1;
    n(k)=y1;
    plot(x,y,'r');
    line([m(k-1) m(k)],[n(k-1) n(k)]);
    k=k+1;
    c=c1;
    else
        break
    end
end
line([m(k-1) m(1)],[n(k-1) n(1)]);
BW = roipoly(I1,m,n); 
if p==3
    r1=double(r).*double(BW);
    g1=double(g).*double(BW);
    b1=double(b).*double(BW);
    i1=cat(3,r1,g1,b1);
    figure
    imshow(mat2gray(i1));
else 
    i2=double(I1).*double(BW);
    figure
    imshow(mat2gray(i2));
end
交互式选择多边形区域_第1张图片 交互式选择多边形区域_第2张图片

你可能感兴趣的:(交互式选择多边形区域)