• 大小: 6KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: Matlab
  • 标签: matlab  poisson  matting  

资源简介

该代码为poisson抠图的matlab代码,可放心使用。 显示图像后,单击鼠标左键连出完全背景区域,双击左键结束; 图像改变后,再单击鼠标左键连出完全前景区域,双击左键结束。即可得到结果

资源截图

代码片段和文件信息

clc;
clear;
%   Global Poisson METHOD
%   By Guan Li May 2004
A=imread(‘cat.jpg‘);
imshow(A);
hold on;

Threshold = 1;
UniBack=[0
    0
   255];

% choose background object and B results into the Foreground object and the
% Boundary Region
% set(handles.text5 ‘String‘ ‘Please choose the outer line of the boundary region. Single 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) = immultiply(A(::2)~OuterBW);
Back(::3) = immultiply(A(::3)~OuterBW);
imshow(B);

% choose foreground object and C results into a Circle of Interest
% set(handles.text5 ‘String‘ ‘Please 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‘);
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);


hold off;


% set(handles.text5 ‘String‘ ‘Calculating Raw Fore & BackGround......................‘);
RawAlpha = (double(OuterBW)+double(InnerBW))/2;
%imshow(uint8(RawAlpha*255));

I = double(A);

% Method of “averaging“ to get raw fore and background colors
for i = 1:size(RawAlpha1)
     for j = 1:size(RawAlpha2)
         RawFore(ij1)=0;                     
         RawFore(ij2)=0;
         RawFore(ij3)=0;
         RawBack(ij1)=0;                     
         RawBack(ij2)=0;
         RawBack(ij3)=0;
         if RawAlpha(ij) == 1
             RawFore(ij:)=Fore(ij:);
         end;
         if RawAlpha(ij) == 0
             RawBack(ij:)=Back(ij:);
         end;
         if RawAlpha(ij) == 0.5
             r=1;
             while 1
                 TestFore = InnerBW(max(i-r1):min(i+rsize(A1))max(j-r1):min(j+rsize(A2)));
                 if size(find(TestFore)1)  
                     [i1 j1] = find(TestFore);
                     i2 = i1 + max(i-r1) -1;
                     j2 = j1 + max(j-r1) -1;
                     Rs = double(Fore(i2j2:));
                     Ds(:1)=diag(Rs(::1));
                     Ds(:2)=diag(Rs(::2));
                     Ds(:3)=diag(Rs(::3));                 
                     if size(Ds1) == 1
                         RawFore(ij:) =Ds;
                     else
                         RawFore(ij:) = sum(Ds) / size(Ds1);
                     end;
                     clear Ds;
                     break;
                 else
                     r=r+1;
                 end;
             end;
             r=1;
             while 1
      

评论

共有 条评论