正文描述:《Matlab抠图代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、注:显示图像后,单击鼠标左键连出完全背景区域,双击左键结束; 图像改变后,再单击鼠标左键连出完全前景区域,双击左键结束。即可得到结果。A=imread('ppmm2.bmp');imshow(A);holdon;Threshold=1;UniBack=[0 0 255];%choosebackgroundobject,andBresultsintotheForegroundObjectandthe%BoundaryRegion%choosetheouterlineoftheboundaryregion.Singlel
2、eftclickthemousetospecifyvertice.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)=immul
3、tiply(A(:,:,2),~OuterBW);Back(:,:,3)=immultiply(A(:,:,3),~OuterBW);imshow(B);%chooseforegroundobject,andCresultsintoaCircleofInterest%choosetheinnerlineoftheboundaryregion.Singleleftclickthemousetospecifyvertice.Doubleleftclickorsinglerightclicktofinish;InnerBW=roipo
4、ly(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;Raw
5、Alpha=(double(OuterBW)+double(InnerBW))/2;I=double(A);%Methodof"averaging"togetrawforeandbackgroundcolorsfori=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;
6、 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,
7、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
8、(:,:,2)); Ds(:,3)=diag(Rs(:,:,3)); ifsize(Ds,1)==1 RawFore(i,j,:)=Ds; else
显示全部收起