您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 图形图像 > Matlab抠图代码
注:显示图像后,单击鼠标左键连出完全背景区域,双击左键结束;图像改变后,再单击鼠标左键连出完全前景区域,双击左键结束。即可得到结果。A=imread('ppmm2.bmp');imshow(A);holdon;Threshold=1;UniBack=[00255];%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);%Methodofaveragingtogetrawforeandbackgroundcolorsfori=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)==1RawFore(i,j,:)=Fore(i,j,:);end;ifRawAlpha(i,j)==0RawBack(i,j,:)=Back(i,j,:);end;ifRawAlpha(i,j)==0.5r=1;while1TestFore=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)==1RawFore(i,j,:)=Ds;elseRawFore(i,j,:)=sum(Ds)/size(Ds,1);end;clearDs;break;elser=r+1;end;end;r=1;while1TestBack=~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)==1RawBack(i,j,:)=Ds;elseRawBack(i,j,:)=sum(Ds)/size(Ds,1);end;clearDs;break;elser=r+1;end;end;end;end;end;%此处没有用高斯滤波Denorm=RawFore-RawBack;%redchannelI1=I(:,:,1);Denorm1=Denorm(:,:,1);fori=1:size(Denorm1,1)forj=1:size(Denorm1,2)ifDenorm1(i,j)==0Denorm1(i,j)=1;end;end;end;OldAlpha=RawAlpha;NewAlpha=RawAlpha;h1=0;while1fori=1:size(OldAlpha,1)forj=1:size(OldAlpha,2)NewAlpha(i,j)=OldAlpha(i,j);ifRawAlpha(i,j)==0.5Roui=((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)0NewAlpha(i,j)=0;end;ifNewAlpha(i,j)1NewAlpha(i,j)=1;end;end;end;end;%imshow(uint8(NewAlpha*255));DifferenceAlpha=abs(NewAlpha-OldAlpha);OldAlpha=NewAlpha;ifsum(sum(DifferenceAlpha))Thresholdbreak;end;h1=h1+1;end;fori=1:size(A,1)forj=1:size(A,2)ifOldAlpha(i,j)==0NewImage(i,j,:)=UniBack';elseNewImage(i,j,1)=UniBack(1)*(1-OldAlpha(i,j))+RawFore(i,j,1)*OldAlpha(i,j);NewImage(i,j,2)=UniBack(2)*(1-OldAlpha(i,j))+RawFore(i,j,2)*OldAlpha(i,j);NewImage(i,j,3)=UniBack(3)*(1-OldAlpha(i,j))+RawFore(i,j,3)*OldAlpha(i,j);end;end;end;figure,imshow(NewAlpha);figure,imshow(uint8(NewImage));figure,imshow(uint8(RawFore));figure,imshow(uint8(RawBack));
本文标题:Matlab抠图代码
链接地址:https://www.777doc.com/doc-5720693 .html