Matlab抠图代码.docx

上传人:小飞机 文档编号:3161291 上传时间:2023-03-11 格式:DOCX 页数:5 大小:37.46KB
返回 下载 相关 举报
Matlab抠图代码.docx_第1页
第1页 / 共5页
Matlab抠图代码.docx_第2页
第2页 / 共5页
Matlab抠图代码.docx_第3页
第3页 / 共5页
Matlab抠图代码.docx_第4页
第4页 / 共5页
Matlab抠图代码.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《Matlab抠图代码.docx》由会员分享,可在线阅读,更多相关《Matlab抠图代码.docx(5页珍藏版)》请在三一办公上搜索。

1、Matlab抠图代码注:显示图像后,单击鼠标左键连出完全背景区域,双击左键结束; 图像改变后,再单击鼠标左键连出完全前景区域,双击左键结束。即可得到结果。 A=imread(ppmm2.bmp); imshow(A); hold on; Threshold = 1; UniBack=0 0 255; % choose background object, and B results into the Foreground Object and the % Boundary Region % choose the outer line of the boundary region. Single

2、 left click the mouse to specify vertice.Double left click or single right click to finish; 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); % choose foreground object, and C results into a Circle of Interest % choose the inner line of the boundary region. Single left click the mouse to specify vertice.Double left click or single right click to finish; InnerB

4、W = 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); hold off; RawAlpha

5、 = (double(OuterBW)+double(InnerBW)/2; I = double(A); % Method of averaging to get raw fore and background colors for i = 1:size(RawAlpha,1) for j = 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; if RawAlpha(i,j) = 1 Raw

6、Fore(i,j,:)=Fore(i,j,:); end; if RawAlpha(i,j) = 0 RawBack(i,j,:)=Back(i,j,:); end; if RawAlpha(i,j) = 0.5 r=1; while 1 TestFore = InnerBW(max(i-r,1):min(i+r,size(A,1),max(j-r,1):min(j+r,size(A,2); if size(find(TestFore),1) i1, j1 = find(TestFore); i2 = i1 + max(i-r,1) -1; j2 = j1 + max(j-r,1) -1; R

7、s = double(Fore(i2,j2,:); Ds(:,1)=diag(Rs(:,:,1); Ds(:,2)=diag(Rs(:,:,2); Ds(:,3)=diag(Rs(:,:,3); if size(Ds,1) = 1 RawFore(i,j,:) =Ds; else RawFore(i,j,:) = sum(Ds) / size(Ds,1); end; clear Ds; break; else r=r+1; end; end; r=1; while 1 TestBack = OuterBW(max(i-r,1):min(i+r,size(A,1),max(j-r,1):min(

8、j+r,size(A,2); if size(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); if size(Ds,1) = 1 RawBack(i,j,:) =Ds; else RawBack(i,j,:) = sum(Ds) / size(Ds,1); end

9、; clear Ds; break; else r=r+1; end; end; end; end; end; %此处没有用高斯滤波 Denorm = RawFore - RawBack; %red channel I1 = I(:,:,1); Denorm1 = Denorm(:,:,1); for i=1:size(Denorm1,1) for j= 1: size(Denorm1,2) if Denorm1(i,j)=0 Denorm1(i,j)=1; end; end; end; OldAlpha = RawAlpha; NewAlpha = RawAlpha; h1=0; while

10、 1 for i=1:size(OldAlpha,1) for j=1:size(OldAlpha,2) NewAlpha(i,j) = OldAlpha(i,j); if RawAlpha(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) * De

11、norm1(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; if NewAlpha(i,j)1 NewAlpha(i,j)=1; end; end; end; end; % imshow(uint8(NewAlpha*255);

12、DifferenceAlpha = abs(NewAlpha - OldAlpha); OldAlpha = NewAlpha; if sum(sum(DifferenceAlpha) Threshold break; end; h1=h1+1; end; for i=1:size(A,1) for j=1:size(A,2) if OldAlpha(i,j)=0 NewImage(i,j,:)=UniBack; else NewImage(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);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号