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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
package rdf.textures;
import imagetiti.Image;
import java.util.Arrays;
/**
* <p>Description : Classe qui calcule onze caracteristiques Haralick et cinq caracteristiques supplementaires.
* Grandement inspire du code recupere sur Developpez.com et produit par Xavier Philippeau.
* Toutes les formules presentees ainsi que les explications sur la taille variable de la matrice de co-occurence se trouvent dans la these de Djamel BRAHMI.
* Sinon regarder le lien : <html>http://murphylab.web.cmu.edu/publications/boland/boland_node26.html</html>.
* Les indices F1 a F11 sont des Haralick's Features, les autres sont des ajouts perso.
* - Moyenne
* - F1 - Second moment angulaire (homogeneite), egale a l'energie.
* - F2 - Contraste
* - F3 - Correlation
* - F4 - Variance
* - F5 - Moment des différences inverses OK
* - F6 - Moyenne des sommes
* - F7 - Variance des sommes
* - F8 - Entropie des sources
* - F9 - Entropie
* - F10 - Variance des differences
* - F11 - Entropie des differences
* - Dissimilarite
* - Homogeneite
* - Inertie
* </p>
* <p>Date de creation : 26 septembre 2007.</p>
* <p>Packages necessaires : imagetiti.</p>
* <p>Copyright : Copyright (c) 2007.</p>
* <p>Laboratoire : LSIS.</p>
* <p>Equipe : Image et Modele, I&M (ex LXAO).</p>
*
* @author Guillaume THIBAULT
* @version 1.0
*/
public class Haralick
{
private int MSIZE = 256 ;
private double[][] matrix = new double[MSIZE][MSIZE] ; // matrices de densité spatiale (aka GLCM)
private Image image = null ; // image source
// precalculs
private double[] px_y = new double[MSIZE] ; // Px-y(i)
private double[] pxy = new double[2*MSIZE] ; // Px+y(i)
private double[] features = new double[15] ; // valeurs specifiques de la texture
/**Constructeur, par defaut la dimension de la matrice de co-occurence est de 16.
* @param image image (vignette) de la texture a analyser.*/
public Haralick(Image image)
{
this.image = image ;
Calculer() ;
}
/**Constructeur
* @param image image (vignette) de la texture a analyser.
* @param nbNiveauxGris Nombre de niveau de gris qui va engendre la dimension de la matrice de co-occurence. Lire les proprietes dans la these cite ci-dessus.*/
public Haralick(Image image, int nbNiveauxGris)
{
if ( nbNiveauxGris < 2 || nbNiveauxGris > 256 ) throw new Error("Parametre nbNiveauxGris(2..256) incorrect : " + nbNiveauxGris) ;
this.MSIZE = nbNiveauxGris ;
this.image = image ;
Calculer() ;
}
/** Methode qui lance et gere les differentes etapes de calcul.*/
private void Calculer()
{
int i, n = 4 ;
int[] dx = new int[] {1, 1, 0,-1} ;
int[] dy = new int[] {0, 1, 1, 1} ;
for (i=0 ; i < n ; i++)
{
computeMatrix(dx[i], dy[i]) ;
precomputation() ;
this.features[0] += Moyenne() ; // F0 - Moyenne
this.features[1] += getF1() ; // F1 - Second moment angulaire (homogeneite), Egal à l'energie
this.features[2] += getF2() ; // F2 - contraste
this.features[3] += getF3() ; // F3 - correlation
this.features[4] += getF4() ; // F4 - Variance
this.features[5] += getF5() ; // F5 - Moment des différences inverses OK
this.features[6] += getF6() ; // F6 - Moyenne des sommes
this.features[7] += getF7(this.features[6]) ; // F7 - Variance des sommes
this.features[8] += getF8() ; // F8 - Entropie des sources
this.features[9] += getF9() ; // F9 - Entropie
this.features[10] += getF10() ; // F10 - variance des differences
this.features[11] += getF11() ; // F11 - Entropie des differences
this.features[12] += Dissimilarite() ;
this.features[13] += Homogeneite() ;
this.features[14] += Inertie() ;
}
for(i=1 ; i < this.features.length ; i++) this.features[i] /= n ;
}
/* ----------------------------------------- Calcules preliminaires et remplissage de la matrice ----------------------------------------- */
private void precomputation()
{
int i, j ;
Arrays.fill(px_y, 0) ; // Px-y(i)
for (j=0 ; j < MSIZE ; j++)
for(i=0 ; i < MSIZE ; i++)
px_y[Math.abs(i-j)] += matrix[i][j] ;
Arrays.fill(pxy, 0) ; // Px+y(i)
for (j=0 ; j < MSIZE ; j++)
for (i=0 ; i < MSIZE ; i++)
pxy[i+j] += matrix[i][j] ;
}
private void computeMatrix(int dx, int dy)
{
int i, j ;
for (i=0 ; i < MSIZE ; i++) Arrays.fill(matrix[i], 0) ; // raz matrice
// 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)((double)MSIZE*(double)this.image.getPix(y0, x0)/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)((double)MSIZE*(double)this.image.getPix(y1, x1)/256.0) ;
// on incremente la matrice
matrix[v0][v1]++ ;
matrix[v1][v0]++ ;
sum += 2 ;
}
// normalisation
for (j=0 ; j < MSIZE ; j++)
for (i=0 ; i < MSIZE ; i++)
matrix[i][j] /= sum ;
}
/* --------------------------------------------------------- Calcul des caracteristiques --------------------------------------------------------- */
/** Methode qui calcule la moyenne.
* @return La moyenne.*/
private double Moyenne()
{
int i, j ;
double mean = 0 ;
for (j=0 ; j < MSIZE ; j++)
for (i=0 ; i < MSIZE ; i++)
mean += matrix[i][j] ;
mean /= MSIZE*MSIZE ;
return mean ;
}
/** F1 - Methode qui calcule l'angle du moment du second ordre : ASM (homogeneite). Egal a la formule de l'energie.
* @return L'angle du moment du second ordre.*/
private double getF1()
{
double h = 0.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 - Methode qui calcule le contrast.
* @return Le contrast.*/
private double getF2()
{
double contrast = 0.0 ;
for (int n=0 ; n < MSIZE ; n++)
for (int j=0 ; j < MSIZE ; j++)
for (int i=0 ; i < MSIZE ; i++)
if ( Math.abs(i-j) == n )
contrast += (double)n*(double)n*matrix[i][j] ;
return contrast ;
}
/** F3 - Methode qui calcule la correlation.
* @return La correlation.*/
private double getF3()
{ // optimisation car matrice symetrique ==> distribution marginale sur X = distribution marginale sur Y
//moyenne = somme { p(i,j) * i }
int i, j ;
double md_mean = 0 ;
for (i=0 ; i < MSIZE ; i++)
for(j=0 ; j < MSIZE ; j++)
md_mean += i*matrix[i][j] ;
double md_var = 0 ; // variance = somme { p(i,j) * (i-moyenne)^2 }
for (i=0 ; i < MSIZE ; i++)
for (j=0 ; j < MSIZE ; j++)
md_var += matrix[i][j]*(i-md_mean)*(i-md_mean) ;
if ( md_var <= 0 ) return 1 ;
double sum = 0 ; // correlation = somme { (i-moyenne) * (j-moyenne) * p(i,j) / variance^2 }
for(j=0 ; j < MSIZE ; j++)
for(i=0 ; i < MSIZE ; i++)
sum += matrix[i][j]*(i-md_mean)*(j-md_mean) ;
return sum/(md_var*md_var) ;
}
/** F4 - Methode qui calcule la variance.
* @return La variance.*/
private double getF4()
{
int i, j ;
double mean = 0 ;
for (j=0 ; j < MSIZE ; j++)
for (i=0 ; i < MSIZE ; i++)
mean += matrix[i][j] ;
mean /= MSIZE*MSIZE ;
double variance = 0 ;
for (j=0 ; j < MSIZE ; j++)
for (i=0 ; i < MSIZE ; i++)
variance += (i-mean)*(i-mean)*matrix[i][j] ;
return variance ;
}
/** F5 - Methode qui calcule la difference inverse.
* @return La difference inverse.*/
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 - Methode qui calcule la moyenne des sommes.
* @return La moyenne des sommes.*/
private double getF6()
{
double sumavg = 0 ;
for(int k=2 ; k < 2*MSIZE-1 ; k++) sumavg += k*pxy[k] ;
return sumavg ;
}
/** F7 - Methode qui calcule la variance des sommes.
* @return La variance des sommes.*/
private double getF7(double f6)
{
double sumavg = f6 ;
int sumvar = 0 ;
for (int k=2 ; k < 2*MSIZE-1 ; k++) sumvar += (k-sumavg)*(k-sumavg)*pxy[k] ;
return sumvar ;
}
/** F8 - Methode qui calcule l'entropie des sources.
* @return L'entropie des sources.*/
private double getF8()
{
double entropysrc = 0 ;
for(int k=2 ; k < 2*MSIZE-1 ; k++)
{
if ( pxy[k] == 0 ) continue ;
entropysrc += pxy[k]*Math.log(pxy[k]) ;
}
return -entropysrc ;
}
/** F9 - Methode qui calcule l'entropie.
* @return L'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 - Methode qui calcule la variance des differences.
* @return La variance des differences.*/
private double getF10()
{
int k ;
double mean = 0 ;
for (k=0 ; k < MSIZE-1 ; k++) mean += k*px_y[k] ;
double var = 0 ;
for (k=0 ; k < MSIZE-1 ; k++) var += (k-mean)*(k-mean)*px_y[k] ;
return var ;
}
/** F4 - Methode qui calcule l'entropie des differences.
* @return L'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 ;
}
/** Methode qui calcule la homogeneite.
* @return La homogeneite.*/
private double Homogeneite()
{
double homogeneity = 0.0 ;
for(int j=0 ; j < MSIZE ; j++)
for(int i=0 ; i < MSIZE ; i++)
homogeneity += matrix[i][j] / (1.0+Math.abs(i-j)) ;
return homogeneity ;
}
/** Methode qui calcule la dissimilarite.
* @return La dissimilarite.*/
private double Dissimilarite()
{
double dissimilarity = 0.0 ;
for(int j=0 ; j < MSIZE ; j++)
for(int i=0 ; i < MSIZE ; i++)
dissimilarity += matrix[i][j]*Math.abs(i-j) ;
return dissimilarity ;
}
/** Methode qui calcule l'inertie.
* @return L'.*/
private double Inertie()
{
double inertie = 0.0 ;
for(int j=0 ; j < MSIZE ; j++)
for(int i=0 ; i < MSIZE ; i++)
inertie += matrix[i][j]*Math.pow(i-j, 2.0) ;
return inertie ;
}
/* ---------------------------------------------------- Les getters ---------------------------------------------------- */
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() ;
}
/** Methode qui retourne la liste des caracteristiques.
* @return Le tableau de double contenant les caracteristiques.*/
public double[] getFeatures()
{
return features ;
}
} |
Partager