function [Image_piece_brut_lignes, Image_piece_brut_colonnes] =piece_brut(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

% Voila la bonne matrice et
% l'inverse qui va avec
I_matrice1 = B;
M = [];
for i = 1:l
    for j = 1:c
        M(i,j) = I_matrice1(i,c-(j-1));
    end
end

% On commence ligne par ligne
D = B;
E = M;
for i = 1:l-1
    for j = 1:c-1   
        C = D(i:i+1,j:j+1);
        W = E(i:i+1,j:j+1);
        o = 0;
        u = 0;
        for k = 1:4
            if C(k) == 1
                o = o + 1;
            elseif W(k) == 1;
                u = u + 1;                
            end
        end
        if o == 0
            D(i:i+1,j:j+1) = D(i,j);
        elseif o == 1
            if D(i,j) == 1
                D(i+1,j:j+1) = D(i,j+1);
            elseif D(i+1,j) == 1
                D(i,j:j+1) = D(i,j);
            elseif D(i,j+1) == 1
                D(i+1,j) = D(i,j);
            elseif D(i+1,j+1) == 1
                D(i,j+1) = D(i,j);
            end
        elseif o == 2
            if D(i:i+1,j+1) == 1
                D(i:i+1,j) == D(i,j);
            elseif D(i:i+1,j) == 1
                D(i:i+1,j+1) == D(i,j);
            end
        end
        if u == 0
            E(i:i+1,j:j+1) = E(i,j);
        elseif u == 1
            if E(i,j) == 1
                E(i+1,j:j+1) = E(i,j+1);
            elseif E(i+1,j) == 1
                E(i,j:j+1) = E(i,j);
            elseif E(i,j+1) == 1
                E(i+1,j) = E(i,j);
            end
        elseif u == 2
            if E(i:i+1,j+1) == 1
                E(i:i+1,j) == E(i,j);
            elseif E(i:i+1,j) == 1
                E(i:i+1,j+1) == E(i,j);
            end
        end
    end
end

% ON REINVERSE POUR QUE CA SOIT JOLI A L AFFICHAGE
for i = 1:l
    for j = 1:c
        I_matrice2_inverse(i,j) = E(i,c-(j-1));
    end
end

% On affiche le resultat ligne par ligne
    
I_matrice2 = D;
figure, Image_piece_brut_lignes= imshow(I_matrice2);
%figure, Image_piece_brut_lignes_inv= imshow(I_matrice2_inverse);         


% On veut la meme chose colonne par colonne
M = [];
for i = 1:l
    for j = 1:c
        M(i,j) = I_matrice1(l-(i-1),j);
    end
end

% Et c'est reparti colonne par colonne
D = B;
E = M;
for j = 1:c-1
    for i = 1:l-1       
        C = D(i:i+1,j:j+1);
        W = E(i:i+1,j:j+1);
        o = 0;
        u = 0;
        for k = 1:4
            if C(k) == 1
                o = o + 1;
            elseif W(k) == 1;
                u = u + 1;                
            end
        end
        if o == 0
            D(i:i+1,j:j+1) = D(i,j);
        elseif o == 1
            if D(i,j) == 1
                D(i+1,j:j+1) = D(i,j+1);
            elseif D(i+1,j) == 1
                D(i,j:j+1) = D(i,j);
            elseif D(i,j+1) == 1
                D(i+1,j) = D(i,j);
            end
        elseif o == 2
            if D(i:i+1,j+1) == 1
                D(i:i+1,j) == D(i,j);
            elseif D(i:i+1,j) == 1
                D(i:i+1,j+1) == D(i,j);
            end
        end
        if u == 0
            E(i:i+1,j:j+1) = E(i,j);
        elseif u == 1
            if E(i,j) == 1
                E(i+1,j:j+1) = E(i,j+1);
            elseif E(i+1,j) == 1
                E(i,j:j+1) = E(i,j);
            elseif E(i,j+1) == 1
                E(i+1,j) = E(i,j);
            end
        elseif u == 2
            if E(i:i+1,j+1) == 1
                E(i:i+1,j) == E(i,j);
            elseif E(i:i+1,j) == 1
                E(i:i+1,j+1) == E(i,j);
            end
        end
    end
end
% ON REINVERSE POUR QUE CA SOIT JOLI A L AFFICHAGE
for i = 1:l
    for j = 1:c
        I_matrice3_inverse(i,j) = E(l-(i-1),j);
    end
end
      
I_matrice3 = D;
%figure, Image_piece_brut_colonnes= imshow(I_matrice3);
%figure, Image_piece_brut_colonnes_inv= imshow(I_matrice3_inverse); 

A = (I_matrice3 + I_matrice3_inverse + I_matrice2 + I_matrice2_inverse)/4;
%figure, imshow(A);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% DIFFERENTS PARAMETRES POUR LES PLOTS
%[ld, cd] = size(D);
%dim_image = (ld-1)*(cd-1);
%D2 = D(:);
%D3 = sort(D2);
%x = zeros([dim_image 1]);
%k = 1:dim_image;
%x(k) = k;
%f = D3(dim_image-(x-1));

% les differents plots:
%figure, plot(x,f,'b');
%figure, semilogx(x,f,'r');


% ESSAI POUR MOYENNER SUR CHAQUE LIGNE ENTRE DES EDGES

% On initialise
%D = B;
%jk = 0;
%for i = 1:l-1
   % for j = 1:c-1
    %    if j > jk,   
        %    if D(i,j) ~= 1
         %       k=0;
         %       s = 0;
            %    while D(i,j+k) ~= 1
              %      s = s + D(i,j+k);
               %     av = s/(k+1);
                    %D(i,j+k) = D(i,j);
                %    k=k+1;
                 %   if (j+k) == l, break, end                
                 %   end
              %  D(i,j:j+k) = av;
              %    end
        %    jk = j + k;         
        %   end
        % end
        %end
%Matrice_piece_brut_inter = D;
%figure, Image_piece_brut_inter= imshow(Matrice_piece_brut_inter);