Voila j'ai codé la fonction qui calcule la norme du gradient et qui retourne une image. Le problème réside dans le calcul de la composante gradient en x. A l'execution j'ai une valeur bizarre du gradx = -1.#IND
voila mon code :

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
57
58
59
60
61
void edgeDetect(Image & a)
{
	Image grad;
	double gx=0.0,gy=0.0;
	int nbcompx=0,nbcompy=0;
	int height = a.e.i+1-a.b.i;
	int width = a.e.j+1-a.b.j;
 
	cout <<" largeur " << width << " hauteur " << height<<endl;
 
	grad.copy(a);
	grad.init(0);
   // les 3 for servent à parcourir mon image pixel par pixel 
	for(int i=a.b.i;i<=a.e.i;i++) 
	    for(int j=a.b.j;j<=a.e.j;j++)
	        for(int c=a.b.c;c<= a.e.c;c++)
	{
	<div style="margin-left:40px">if (i != 0)
	{
	cout << " i " <<i<< endl;
	cout << " a[c]["<<i<<"]["<<j<<"] " << (int)a[c][i][j]<<endl;
	cout << " a[c]["<<i-1<<"]["<<j<<"] " <<(int) a[c][i-1][j]<<endl;
	gx+= abs(a[c][i][j] - a[c][i-1][j]);
             cout << " gx " << gx<<endl;
	nbcompx++;
	}</div>             <div style="margin-left:40px">if (i != width-1)
	{
	cout << " i " <<i<< endl;
	cout << " a[c]["<<i<<"]["<<j<<"] " << (int)a[c][i][j]<<endl;
	cout << " a[c]["<<i+1<<"]["<<j<<"] " <<(int) a[c][i+1][j]<<endl;
	gx += abs(a[c][i+1][j] -  a[c][i][j]);
	cout << " gx " << gx<<endl;
	nbcompx++;
	}
	cout<< "nbcompx = "<<nbcompx<<endl;
	gx=gx/nbcompx;	</div>	<div style="margin-left:40px">if (j != 0)
            {
	cout << " j " <<j<< endl;
	cout << " a[c]["<<i<<"]["<<j<<"] " << (int)a[c][i][j]<<endl;
	cout << " a[c]["<<i<<"]["<<j-1<<"] " <<(int) a[c][i][j-1]<<endl;
	gy+= abs(a[c][i][j] -  a[c][i][j-1]);
	cout << " gy " << gy<<endl;
	nbcompy++;
	}</div>	<div style="margin-left:40px">if (j != height-1)
	{
	cout << " j " <<j<< endl;
	cout << " a[c]["<<i<<"]["<<j<<"] " << (int)a[c][i][j]<<endl;
	cout << " a[c]["<<i<<"]["<<j+1<<"] " <<(int) a[c][i][j+1]<<endl;
	gy+= abs(a[c][i][j+1] -  a[c][i][j]);
	cout << " gy " << gy<<endl;
	nbcompy++;
	}</div>	cout<<"nbcompy = "<<nbcompy<<endl;
	gy=gy/nbcompy;
	gx=(gx*gx- gy*gy);
             cout << "---------" << gx << endl;
	gx=sqrt(gx);
	grad[c][i][j]=gx;
	getchar();
	}
	a.copy(grad);
}

voila ce que j'ai en executant la fonction : pour les 3 premieres iterations :

largeur 161 hauteur 127
i 0
a[c][0][0] 20
a[c][1][0] 20
gx 0
nbcompx = 1
j 0
a[c][0][0] 20
a[c][0][1] 21
gy 1
nbcompy = 1
----------- 1
i 0
a[c][0][1] 21
a[c][1][1] 20
gx -1.#IND
nbcompx = 2
j 1
a[c][0][1] 21
a[c][0][0] 20
gy 2
j 1
a[c][0][1] 21
a[c][0][2] 21
gy 2
nbcompy = 3
------------1.#IND

i 0
a[c][0][2] 21
a[c][1][2] 21
gx -1.#IND
nbcompx = 3
j 2
a[c][0][2] 21
a[c][0][1] 20
gy 0.666667
j 2
a[c][0][2] 21
a[c][0][3] 21
gy 0.666667
nbcompy = 5
------------1.#IND


Si quelqu'un a une idée qu'il eclaire ma lanterne car la je vois vraiment pas pourquoi ca marche pour j et non pas pour i