function Image_fill = piece_fill(k3, l, c, N)

% On appelle les fonctions handles

ed = @edginess;
cons = @byaverage;
n = @norme;
moy = @moyenne;

% Voila la premiere boucle pour trouver
% les edges et la matrice qui va avec

B = [];
B(l,:) = N(l,:);
B(:,c) = N(:,c);
for i = 1:l-1
    for j = 1:c-1
        e(i,j) = feval( ed, N, i, j);
        d(i,j) = feval( cons, N ,i, j);
        R(i,j) = e(i,j)*d(i,j);
        if R(i,j) > 0.00025*k3
            B(i,j) = 1;
        else B(i,j) = feval(moy, N, i,j);
        end
    end     
end

% on va creer une matrice temoin
M = [];
B(l,:) = 1;
B(:,c) = 1;
B(1,:) = 1;
B(:,1) = 1;
M = zeros([l c]);
% Ensuite on va fill les holes
couple =[[-1 0];[1 0];[0 -1];[0 1];];
for i = 1:l
    for j = 1:c
        if B(i,j) == 1
            M(i,j) = 1;
        else 
            if M(i,j) ~= 1
                M(i,j) = 1;
                x = i;
                y = j;
                for r = 1:4
                    while B(x + couple(r,1), y + couple(r,2)) ~= 1
                        B(x + couple(r,1),y + couple(r,2)) = B(i,j);
                        M(x + couple(r,1),y + couple(r,2))= 1;
                        x = x + couple(r,1);
                        y = y + couple(r,2);
                        if x == l-1, break, end;
                        if x == 2, break, end;           
                        if y == c-1, break, end;
                        if y == 2, break, end;                                       
                    end
                end
            end
        end
    end
end
            
I_matrice_temoin = M;
figure, Image_piece_random = imshow(I_matrice_temoin);

I_matrice_piece_fill = B;
figure, Image_fill = imshow(I_matrice_piece_fill);
