原图:
目标:
好吧,第一步我们应该去除边框。
先转换为二值图:
代码:
bw=im2bw(f);
figure,imshow(bw);
新知识,因为要用到去边框函数。所以要进行切割,贴合白边切割:
利用find()函数:
代码:
[r,c]=find(bw);
bw=bw(min(r):max(r),min(c):max(c));
figure,imshow(bw);
去边框函数:
imclearborder();
注意,你操作的东西不能和边框粘连在一起!!!
ok。
代码:
bw=imclearborder(bw);
figure,imshow(bw);
成功去掉:
为了去掉图中的小点点。我们使用去除小对象:
bw_ao=bwareaopen(bw,150);
figure,imshow(bw_ao);
得到:
因为,本例子是:川。我采用的是利用线条:line来进行腐蚀:
bw_erode=imerode(bw_ao,strel('line',10,0));
figure,imshow(bw_erode);
得到:
然后,再进行修复操作:
bw_re=imreconstruct(bw_erode,bw);
figure,imshow(bw_re);
最后,修复过之后:
恢复了原来胖胖的样子:
然后,原二值图减去上面这个胖胖的图:
chuan=bw_ao-bw_re;
figure,imshow(chuan);
得到:
全部代码:
clear,clc,close all;
f=imread('cl.jpg');
imshow(f);
bw=im2bw(f);
figure,imshow(bw);
[r,c]=find(bw);
bw=bw(min(r):max(r),min(c):max(c));
figure,imshow(bw);
bw=imclearborder(bw);
figure,imshow(bw);
bw_ao=bwareaopen(bw,150);
figure,imshow(bw_ao);
bw_erode=imerode(bw_ao,strel('line',10,0));
figure,imshow(bw_erode);
bw_re=imreconstruct(bw_erode,bw);
figure,imshow(bw_re);
chuan=bw_ao-bw_re;
figure,imshow(chuan);