Matlab抠图代码.docx
- 文档编号:231255
- 上传时间:2022-10-07
- 格式:DOCX
- 页数:5
- 大小:10.74KB
Matlab抠图代码.docx
《Matlab抠图代码.docx》由会员分享,可在线阅读,更多相关《Matlab抠图代码.docx(5页珍藏版)》请在冰豆网上搜索。
注:
显示图像后,单击鼠标左键连出完全背景区域,双击左键结束;
图像改变后,再单击鼠标左键连出完全前景区域,双击左键结束。
即可得到结果。
A=imread('ppmm2.bmp');
imshow(A);
holdon;
Threshold=1;
UniBack=[0
0
255];
%choosebackgroundobject,andBresultsintotheForegroundObjectandthe
%BoundaryRegion
%choosetheouterlineoftheboundaryregion.Singleleftclickthemousetospecifyvertice.Doubleleftclickorsinglerightclicktofinish;
OuterBW=roipoly(A);
B(:
:
1)=immultiply(A(:
:
1),OuterBW);
B(:
:
2)=immultiply(A(:
:
2),OuterBW);
B(:
:
3)=immultiply(A(:
:
3),OuterBW);
Back(:
:
1)=immultiply(A(:
:
1),~OuterBW);
Back(:
:
2)=immultiply(A(:
:
2),~OuterBW);
Back(:
:
3)=immultiply(A(:
:
3),~OuterBW);
imshow(B);
%chooseforegroundobject,andCresultsintoaCircleofInterest
%choosetheinnerlineoftheboundaryregion.Singleleftclickthemousetospecifyvertice.Doubleleftclickorsinglerightclicktofinish;
InnerBW=roipoly(B);
C(:
:
1)=immultiply(B(:
:
1),~InnerBW);
C(:
:
2)=immultiply(B(:
:
2),~InnerBW);
C(:
:
3)=immultiply(B(:
:
3),~InnerBW);
Fore(:
:
1)=immultiply(A(:
:
1),InnerBW);
Fore(:
:
2)=immultiply(A(:
:
2),InnerBW);
Fore(:
:
3)=immultiply(A(:
:
3),InnerBW);
imshow(C);
holdoff;
RawAlpha=(double(OuterBW)+double(InnerBW))/2;
I=double(A);
%Methodof"averaging"togetrawforeandbackgroundcolors
fori=1:
size(RawAlpha,1)
forj=1:
size(RawAlpha,2)
RawFore(i,j,1)=0;
RawFore(i,j,2)=0;
RawFore(i,j,3)=0;
RawBack(i,j,1)=0;
RawBack(i,j,2)=0;
RawBack(i,j,3)=0;
ifRawAlpha(i,j)==1
RawFore(i,j,:
)=Fore(i,j,:
);
end;
ifRawAlpha(i,j)==0
RawBack(i,j,:
)=Back(i,j,:
);
end;
ifRawAlpha(i,j)==0.5
r=1;
while1
TestFore=InnerBW(max(i-r,1):
min(i+r,size(A,1)),max(j-r,1):
min(j+r,size(A,2)));
ifsize(find(TestFore),1)
[i1,j1]=find(TestFore);
i2=i1+max(i-r,1)-1;
j2=j1+max(j-r,1)-1;
Rs=double(Fore(i2,j2,:
));
Ds(:
1)=diag(Rs(:
:
1));
Ds(:
2)=diag(Rs(:
:
2));
Ds(:
3)=diag(Rs(:
:
3));
ifsize(Ds,1)==1
RawFore(i,j,:
)=Ds;
else
RawFore(i,j,:
)=sum(Ds)/size(Ds,1);
end;
clearDs;
break;
else
r=r+1;
end;
end;
r=1;
while1
TestBack=~OuterBW(max(i-r,1):
min(i+r,size(A,1)),max(j-r,1):
min(j+r,size(A,2)));
ifsize(find(TestBack),1)
[i1,j1]=find(TestBack);
i2=i1+max(i-r,1)-1;
j2=j1+max(j-r,1)-1;
Rs=double(Back(i2,j2,:
));
Ds(:
1)=diag(Rs(:
:
1));
Ds(:
2)=diag(Rs(:
:
2));
Ds(:
3)=diag(Rs(:
:
3));
ifsize(Ds,1)==1
RawBack(i,j,:
)=Ds;
else
RawBack(i,j,:
)=sum(Ds)/size(Ds,1);
end;
clearDs;
break;
else
r=r+1;
end;
end;
end;
end;
end;
%此处没有用高斯滤波
Denorm=RawFore-RawBack;
%redchannel
I1=I(:
:
1);
Denorm1=Denorm(:
:
1);
fori=1:
size(Denorm1,1)
forj=1:
size(Denorm1,2)
ifDenorm1(i,j)==0
Denorm1(i,j)=1;
end;
end;
end;
OldAlpha=RawAlpha;
NewAlpha=RawAlpha;
h1=0;
while1
fori=1:
size(OldAlpha,1)
forj=1:
size(OldAlpha,2)
NewAlpha(i,j)=OldAlpha(i,j);
ifRawAlpha(i,j)==0.5
Roui=((I1(i+1,j)+I1(i-1,j)-2*I1(i,j))*Denorm1(i,j)-(I1(i+1,j)-I1(i,j))*(Denorm1(i+1,j)-Denorm1(i,j)))/(Denorm1(i,j)*Denorm1(i,j));
Rouj=((I1(i,j+1)+I1(i,j-1)-2*I1(i,j))*Denorm1(i,j)-(I1(i,j+1)-I1(i,j))*(Denorm1(i,j+1)-Denorm1(i,j)))/(Denorm1(i,j)*Denorm1(i,j));
Rou=Roui+Rouj;
NewAlpha(i,j)=(OldAlpha(i+1,j)+NewAlpha(i-1,j)+OldAlpha(i,j+1)+NewAlpha(i,j-1)-Rou)/4;
ifNewAlpha(i,j)<0
NewAlpha(i,j)=0;
end;
ifNewAlpha(i,j)>1
NewAlpha(i,j)=1;
end;
end;
end;
end;
%imshow(uint8(NewAlpha*255));
DifferenceAlpha=abs(NewAlpha-OldAlpha);
OldAlpha=NewAlpha;
ifsum(sum(DifferenceAlpha
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Matlab 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)