Alors tout d'abord bonjour !
Je pense que je vais en faire éternuer plus d'un en dépoussiérant ce sujet ... :mouarf:
Comme l'intitulé l'indique, mon problème concerne le code Java du GVF fourni par pseudocode. J'aimerais tout simplement porter ce code en Python mais j'éprouve quelques difficultés.
Code de pseudocode :
Code:
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 /** * @param f : image normalized in [0,1] * @param w : width of image * @param h : height of image * @param ITER : number of iterations * @param mu : iteration step * @return u[x,y] and v[x,y] arrays */ public static double[][][] gvf(double[][] f, int w, int h, int ITER, double mu) { // create empty arrays double[][] u = new double[w][h]; double[][] v = new double[w][h]; double[][] fx = new double[w][h]; double[][] fy = new double[w][h]; double[][] Lu = new double[w][h]; double[][] Lv = new double[w][h]; // precompute edge-map (gradient) for (int y=1;y<(h-1);y++) { for (int x=1;x<(w-1);x++) { fx[x][y] = (f[x+1][y]-f[x-1][y])/2; fy[x][y] = (f[x][y+1]-f[x][y-1])/2; } } // iterative diffusion for(int loop=0;loop<ITER;loop++) { // compute laplacian of U and V for (int y=1;y<(h-1);y++) { for (int x=1;x<(w-1);x++) { Lu[x][y] = -u[x][y] + 0.25*(u[x-1][y]+u[x+1][y]+u[x][y-1]+u[x][y+1]); Lv[x][y] = -v[x][y] + 0.25*(v[x-1][y]+v[x+1][y]+v[x][y-1]+v[x][y+1]); } } // update U and V for (int y=0;y<h;y++) { for (int x=0;x<w;x++) { double gnorm2 = fx[x][y]*fx[x][y] + fy[x][y]*fy[x][y]; u[x][y] += mu*4*Lu[x][y] - (u[x][y]-fx[x][y])*gnorm2; v[x][y] += mu*4*Lv[x][y] - (v[x][y]-fy[x][y])*gnorm2; } } } // return U and V arrays return new double[][][]{u,v}; }
Mon implémentation (assez ressemblante oui oui :oops:) :
Après avoir testé avec la même matrice de test :Code:
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 import numpy as np from math import * def gvf(f, w, h, ITER, mu): u = np.empty((w, h), float) v = np.empty((w, h), float) fx = np.empty((w, h), float) fy = np.empty((w, h), float) Lu = np.empty((w, h), float) Lv = np.empty((w, h), float) for y in range(h-1): for x in range(w-1): fx[x,y] = (f[x+1,y]-f[x-1,y])/2 fy[x,y] = (f[x,y+1]-f[x,y-1])/2 for loop in range(ITER): for y in range(h-1): for x in range(w-1): Lu[x,y] = -u[x,y] + 0.25 * (u[x-1,y]+u[x+1,y]+u[x,y-1]+u[x,y+1]) Lv[x,y] = -v[x,y] + 0.25 * (v[x-1,y]+v[x+1,y]+v[x,y-1]+v[x,y+1]) for y in range(h): for x in range(w): gnorm2 = fx[x,y]*fx[x,y] + fy[x,y]*fy[x,y] u[x,y] += mu*4*Lu[x,y] - (u[x,y]-fx[x,y])*gnorm2 v[x,y] += mu*4*Lu[x,y] - (v[x,y]-fy[x,y])*gnorm2 return u, v
[[ 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 0. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1.]]
et après avoir calculé la norme du vecteur ainsi :
j'ai en sortie une matrice contenant des nan et des valeurs bizarres :Code:
1
2
3
4 for y,a in enumerate(v): for x,b in enumerate(u): flux[x,y] = sqrt(u[x,y]*u[x,y] + v[x,y]*v[x,y])
Pouvez vous s'il vous plait m'aider à trouver le problème ? :mrgreen:Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 [[ nan nan nan nan nan nan nan] [ nan nan nan nan nan nan nan] [ nan nan nan nan nan nan inf] [ nan nan nan nan nan nan nan] [ nan nan nan nan nan nan inf] [ nan nan nan nan nan nan nan] [ inf inf nan inf nan nan 6.01334434e-154]]