分割树叶(数字图像处理)
(1)提出问题:
(2)分析问题 :
(3)解决问题 :
问题:
在一幅图像中,若含有一片树叶, 那么,如何将图像中的树叶分割出来?
分析:
从上到下,从左到右,依次扫描图像,将上下左右出现的第一个黑色像素点记录下来,然后进行分割:
实现:
I=imread('shuye.bmp');//读入一副图象
bw=im2bw(I);
[y,x]=size(bw);
left=zeros(1,y);从左开始扫描
for j=1:y
i=1;
while((i<=x)&&(bw(j,i)==1))
i=i+1;
end
if(i<x)
left(j)=i;
else
left(j)=x;
end
end
leftlocation=min(left);
right=zeros(1,y);//从右开始扫描
for j=1:y
i=x;
while((i>=1)&&(bw(j,i)==1))
i=i-1;
end
if(i>=1)
right(j)=i;
end
end
rightlocation=max(right);
top=zeros(1,x);//从上开始扫描
for i=1:x
j=1;
while((j<=y)&&(bw(j,i)==1))
j=j+1;
end
if(j<y)
top(i)=j;
else
top(i)=y;
end
end
toplocation=min(top);
bottom=zeros(1,x);//从下开始扫描
for i=1:x
j=y;
while((j>=1)&&(bw(j,i)==1))
j=j-1;
end
if(j>=1)
bottom(i)=j;
end
end
bottomlocation=max(bottom);
clip=imcrop(bw,[leftlocation toplocation rightlocation-leftlocation bottomlocation-toplocation]);//分割
imshow(I);
figure;
imshow(clip);//显示
还没有评论,来说两句吧...