Voici un petit programme Java qui calcule quelques caracteristiques d'Haralick sur la matrice de co-occurence.

Un exemple d'applet interactif (qui n'utilise pas du tout ce code) est dispo sur ce site.

Code java : 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import java.util.Arrays;
 
public class Haralick {
 
	// matrices de densité spatiale (aka GLCM)
	private int MSIZE=256;
	private double[][] matrix = new double[MSIZE][MSIZE];
 
	// image source
	private Image image = null;
 
	// precalculs
	private double[] px_y = new double[MSIZE];  // Px-y(i)
	private double[] pxy = new double[2*MSIZE]; // Px+y(i)
 
	// valeurs specifiques de la texture
	public double[] features = new double[15];
 
	public Haralick(Image img) {
		this.image=img;
 
		int n=4;
		int[] dx = new int[] {1, 1, 0,-1};
		int[] dy = new int[] {0, 1, 1, 1};
 
		for(int k=0;k<n;k++) {
			computeMatrix(dx[k],dy[k]);
 
			precomputation();
 
			this.features[1] += getF1();
			this.features[2] += getF2();
			this.features[3] += getF3();
			this.features[4] += getF4();
			this.features[5] += getF5();
			this.features[6] += getF6();
			this.features[7] += getF7(this.features[6]);
			this.features[8] += getF8();
			this.features[9] += getF9();
			this.features[10]+= getF10();
			this.features[11]+= getF11();
		}
 
		for(int f=1;f<this.features.length;f++) 
			this.features[f]/=n;
	}
 
	private void precomputation() {
		// Px-y(i)
		Arrays.fill(px_y, 0);
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				px_y[Math.abs(i-j)]+=matrix[i][j];
			}
		}
 
		// Px+y(i)
		Arrays.fill(pxy, 0);
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				pxy[i+j]+=matrix[i][j];
			}
		}
	}
 
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				sb.append(matrix[i][j]+" ");
			}
			sb.append("\n");
		}
		return sb.toString();
	}
 
	private void computeMatrix(int dx, int dy) {
		// raz matrice
		for(int i=0;i<MSIZE;i++) 
			Arrays.fill(matrix[i], 0);
 
		// calcul des co-occurences
		int sum=0;
		int height = image.getHeight();
		int width = image.getWidth();
		for(int y0=0;y0<height;y0++) {
			for(int x0=0;x0<width;x0++) {
				// pour chaque pixel
				int v0 = (int)(MSIZE*this.image.getValue(x0,y0)/256.0);
 
				// on cherche le voisin
				int x1 = x0 + dx;
				if (x1<0 || x1>=width) continue;
				int y1 = y0 + dy;
				if (y1<0 || y1>=height) continue;
				int v1 = (int)(MSIZE*this.image.getValue(x1,y1)/256.0);
 
				// on incremente la matrice
				matrix[v0][v1]++;
				matrix[v1][v0]++;
				sum+=2;
			}
		}
 
		// normalisation
		for(int j=0;j<MSIZE;j++)
			for(int i=0;i<MSIZE;i++)
				matrix[i][j]/=sum;
	}
 
 
 
	// F1 - Second moment angulaire (homogeneite)
	private double getF1() {
		double h=0;
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				h+=matrix[i][j]*matrix[i][j];
			}
		}
		return h;
	}
 
	// F2 - contraste
	private double getF2() {
	    double contrast=0;
	    for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				contrast+=(i-j)*(i-j)*matrix[i][j];
			}
		}
		return contrast;
	}
 
	// F3 - correlation
	private double getF3() {
 
		// optimisation car matrice symetrique
		// ==> distribution marginale sur X = distribution marginale sur Y
 
		// moyenne = somme { p(i,j) * i } 
		double mean = 0;
		for(int i=0;i<MSIZE;i++) {
			for(int j=0;j<MSIZE;j++) {
				mean+=i*matrix[i][j];
			}
		}
 
		// variance = somme { p(i,j) * (i-moyenne)^2 } 
		double var=0;
		for(int i=0;i<MSIZE;i++) {
			for(int j=0;j<MSIZE;j++) {
				var+=matrix[i][j]*(i-mean)*(i-mean);
			}
		}
		if (var<=0) return 1;
 
		// correlation = somme { (i-moyenne) * (j-moyenne) * p(i,j) / variance^2 }  
		double sum = 0;
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				sum+= matrix[i][j]*(i-mean)*(j-mean);
			}
		}
		double r = sum/(var*var);
 
		return r;
	}
 
	// F4 - Variance
	private double getF4() {
		double mean = 0;
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				mean+=i*matrix[i][j];
			}
		}
 
		double variance=0;
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				variance+=(i-mean)*(i-mean)*matrix[i][j];
			}
		}
		return variance;
	}
 
	// F5 - Moment des différences inverses OK
	private double getF5() {
		double invdiff=0;
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				double coef = 1.0/(1.0+(i-j)*(i-j)); 
				invdiff+=coef*matrix[i][j];
			}
		}
		return invdiff;
	}
 
	// F6 - Moyenne des sommes
	private double getF6() {
		double sumavg=0;
		for(int k=0;k<=2*(MSIZE-1);k++) {
			sumavg+=k*pxy[k];
		}
		return sumavg;
	}
 
	// F7 - Variance des sommes
	private double getF7(double f6) {
		double sumavg=f6;
		int sumvar=0;
		for(int k=0;k<=2*(MSIZE-1);k++) {
			sumvar+=(k-sumavg)*(k-sumavg)*pxy[k];
		}
		return sumvar;
	}
 
	// F8 - Entropie des sources
	private double getF8() {
		double entropysrc=0;
		for(int k=0;k<=2*(MSIZE-1);k++) {
			if (pxy[k]==0) continue;
			entropysrc+=pxy[k]*Math.log(pxy[k]);
		}
		return -entropysrc;		
	}
 
	// F9 - Entropie
	private double getF9() {
		double entropy=0;
		for(int j=0;j<MSIZE;j++) {
			for(int i=0;i<MSIZE;i++) {
				if (matrix[i][j]==0) continue;
				entropy+=matrix[i][j]*Math.log(matrix[i][j]);
			}
		}
		return -entropy;		
	}
 
	// F10 - variance des differences
	private double getF10() {
		double mean=0;
		for(int k=0;k<=MSIZE-1;k++) {
			mean+=k*px_y[k];
		}
		double var=0;
		for(int k=0;k<=MSIZE-1;k++) {
			var+=(k-mean)*(k-mean)*px_y[k];
		}
		return var;	
	}
 
	// F11 - Entropie des differences
	private double getF11() {
		double entropydiff=0;
		for(int k=0;k<=MSIZE-1;k++) {
			if (px_y[k]==0) continue;
			entropydiff+=px_y[k]*Math.log(px_y[k]);
		}
		return -entropydiff;		
	}
 
}

Voici un résumé des formules utilisées: