for (k = 1 : numNeighbor)
   that = ind(k); 
   Ineighbor{k} = I{that}; 
end

bx =0; by = 0; cc = 3;
col = 6; row = floor(numNeighbor/col)+1;
Iout = zeros(row*sizex+(row-1)*(2*bx+cc), 2*col*sizey+(col-1)*(2*by+cc))+255; 
towrite = -commonTemplate; 
towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
Iout(1:sizex, 1:sizey) = towrite;
k = -1;  
for (r = 1 : row)
    for (c = 1 : col)
        k = k+1; 
        if ((k>=1)&&(k<=numNeighbor))
            towrite = Ineighbor{k}; 
            towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
            Iout((r-1)*sizex+1+(r-1)*(2*bx+cc): r*sizex+(r-1)*(2*bx+cc), (c-1)*2*sizey+1+(c-1)*(2*by+cc): (c-1)*2*sizey+sizey+(c-1)*(2*by+cc)) = towrite; 
            towrite =  -deformedTemplate{k}; 
            towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
            Iout((r-1)*sizex+1+(r-1)*(2*bx+cc) : r*sizex+(r-1)*(2*bx+cc), (c-1)*2*sizey+sizey+(c-1)*(2*by+cc)+1 : (c-1)*2*sizey+sizey+(c-1)*(2*by+cc)+sizey) =towrite;  
        end
    end
end
for (r = 1 : row-1)
  Iout(r*sizex+(r-1)*(2*bx+cc)+bx+1:r*sizex+(r-1)*(2*bx+cc)+bx+cc, :) = 0; 
end
for (c = 1 : col-1)
  Iout(:, c*(sizey*2)+(c-1)*(2*by+cc)+by+1:c*(sizey*2)+(c-1)*(2*by+cc)+by+cc) = 0; 
end

%outepsgif(Iout, ['ex9_1_' num2str(this)]); 
Outepsgif(Iout, ['ex9_1_' num2str(0)]); 

