Bonjour à tous,

Dans le but de faire de la détection de contour, j'ai écrit un programme qui me permet d'extraire les extrema locaux du gradient (après un filtre de prewitt). Mon objectif est d'appliquer un seuil sur l'image obtenu. Mon souci c'est que ça n'a pas l'air de bien fonctionner. Pouvez-vous regarder mon code et me proposer des solutions de son optimisation s'il vous plaît ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
close all
clear
 
 
I=imread('image/frog.jpg');  %recuperation de l'image
I=rgb2gray(I);
I=double(I)/255.0;
 
%%%%%%%%%%%%%%%%%%% filtre de Prewitt %%%%%%%%%%%%%%%%%%%%%
h_prewitt=fspecial('prewitt'); % creation du filtre (filtre horizontal)
v_prewitt=-h_prewitt';
IderivPrewittH=filter2(h_prewitt,I);
IderivPrewittV=filter2(v_prewitt,I);
 
%%%%%%%%% approche gradient
Ideriv=sqrt(IderivPrewittH.*IderivPrewittH + IderivPrewittV.*IderivPrewittV); %norme du gradient
 
 
%%%%%%% seuillage + histerisis
%% extraction des extrema locaux dans la direction du gradient
dirGrad=atan(IderivPrewittV./IderivPrewittH); %calcul des directions
dirGrad=dirGrad*180/pi; % passage en degré
 
%arondir la matrice des directions par multiple de 45 degré
dirGrad=arrondiMultiple(dirGrad);
 
%detection des extrema
Icontour=Ideriv;
[row,col]=size(dirGrad); %nombre de lignes et de colones
for i1=2:row-1
    for j1=2:col-1
        switch dirGrad(i1,j1)
            case 0
                if Icontour(i1,j1)<Icontour(i1,j1-1) | Icontour(i1,j1)<Icontour(i1,j1+1)
                    Icontour(i1,j1)=0;
                end;
            case 45
                if Icontour(i1,j1)<Icontour(i1-1,j1+1) | Icontour(i1,j1)<Icontour(i1+1,j1-1)
                    Icontour(i1,j1)=0;
                end;
            case -45
                if Icontour(i1,j1)<Icontour(i1+1,j1+1) | Icontour(i1,j1)<Icontour(i1-1,j1-1)
                    Icontour(i1,j1)=0;
                end;
 
            case{-90,90}
                if Icontour(i1,j1)<Icontour(i1-1,j1) | Icontour(i1,j1)<Icontour(i1+1,j1)
                    Icontour(i1,j1)=0;
                end;
            otherwise
                Icontour(i1,j1)=Icontour(i1,j1);
        end;
    end;
end;
figure,
imshow(Icontour,'notruesize');
code de la fonction arrondi multiple de 45 :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function res=arrondiMultiple(mat)
 
[row,col]=size(mat);
 
for i1 =1:row
    for j1 =1:col
 
        if mat(i1,j1)>=-90 & mat(i1,j1)<-67.5
            mat(i1,j1)=-90;
        end
        if mat(i1,j1)>=-67.5 & mat(i1,j1)<-45
            mat(i1,j1)=-45;
        end
 
        if mat(i1,j1)>=-45 & mat(i1,j1)<-22.5
            mat(i1,j1)=-45;
        end
 
        if mat(i1,j1)>=-22.5 & mat(i1,j1)<0
            mat(i1,j1)=0;
        end
 
        if mat(i1,j1)>=0 & mat(i1,j1)<22.5
            mat(i1,j1)=0;
        end
 
        if mat(i1,j1)>=22.5 & mat(i1,j1)<45
            mat(i1,j1)=45;
        end
 
 
        if mat(i1,j1)>=45 & mat(i1,j1)<67.5
            mat(i1,j1)=45;
        end
 
        if mat(i1,j1)>=67.5 & mat(i1,j1)<=90
            mat(i1,j1)=90;
        end
 
    end
end
 
res=mat;
 
%function res=arrondiMultiple(mat,n) % n est le multiple (exemple n=45 et pas=1)
%indice=(mod(mat,n)>0);
%mat(indice)=(floor(mat(indice)/45) + 1)* n; % résultat de la division euclidenne
%res=mat;
Cordialement, Takout