Correction TP Edition d’images

  • Correction pour le TP Edition d’images
  • ////////////////////////////////////////////////////////////////////
    // Function COLOR_POISSON 
    // Take two color images u and v and a binary mask. The image //v is assumed to be smaller than u. First a subimage of u 
    // with the size of v is extracted at (x,y). Then, we copy the // part of v corresponding to the mask to this subimage of u, // and then u is reformed. 
    // Author : Julie Delon, 2015. 
    ////////////////////////////////////////////////////////////////////
    
    // Input parameters 
    // - image u (large image)
    // - image v (small image with portion to be copied in u)
    // - mask (binary mask)
    // - (x,y) integers, position in u where a portion of v must 
    // be copied
    
    // Output :
    // - image out containing u and a portion of v cloned by 
    // Poisson Editing
    
    function out = color_poisson(u, v, mask, x, y, niter)
     [nr,nc] = size(v(:,:,1));
     ucrop = u(x:x+nr-1,y:y+nc-1,:); 
    
     for i = 1:3
     tmp(:,:,i) = poisson(ucrop(:,:,i),v(:,:,i),mask,niter);
     end
    
     out = u;
     out(x:x+nr-1,y:y+nc-1,:) = tmp;
     out = max(min(out,255),0);
    
    endfunction
    
    // Poisson editing for gray level images
    function out = poisson(u, v, mask, niter)
    
     f = u.*(1-mask) + v.*mask;
     K=[0,1,0;1,0,1;0,1,0];
     lapv = laplacien(v);
     
     for k = 1:niter
     Kf = conv2(f,K,'same');
     f(mask) = 1/4*(Kf(mask) + lapv(mask));
     end
    
     out = f;
    endfunction
    
    // Compute the laplacian of an image u
    function lap = laplacien(u)
    
     [ud,ub,ug,uh] = imdiff(u);
     lap = ud + ub + ug + uh;
    
    endfunction
    
    
    function [ud,ub,ug,uh] = imdiff(u)
    
     Kd = [ 0,1, -1 ];
     Kb = [ 0;1; -1 ];
     Kg = [ -1,1, 0 ];
     Kh = [ -1;1; 0 ];
    
     ud = conv2(u,Kd,'same');
     ub = conv2(u,Kb,'same');
     ug = conv2(u,Kg,'same');
     uh = conv2(u,Kh,'same');
    
    endfunction