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
|
package rdf.textures.glrlm;
import mathematics.Maths;
import morphee.Levelings.Leveling;
import processing.filters.Gaussian;
import utils.arrays.ArraysOperations;
import imageTiTi.Image;
import imageTiTi.reducer.ColorReducer;
/**
* <p>Description: Cette classe calcule la "run length matrix" pour une image (vignette). Elle peut etre calculee
* dans plusieurs directions, mais alors une moyenne des differentes matrices est effectuee.<br>
* <p>Package(s) required: imageTiTi, mathematics.</p>
* <p>Copyright: Copyright (c) 2007-2010.</p>
* <p>Laboratories/Teams: CMM (Mines-ParisTech/ENSMP), I&M (ex LXAO) LSIS.</p>
* <p>Updates: <br>
* 12 Avril 2010, 1.2 => Simplification par l'utilisation de l'interface ColorReducer et ajout du Leveling.<br>
* 10 Novembre 2009, 1.1 => Gestion des tous les types de niveaux de gris, ycompris les binaires.<br>
* 02 Mai 2008 => Ajout de la valeur interdite.<br>
* 08 Avril 2008 => Creation.</p>
*
* @author Guillaume THIBAULT
* @version 1.2
* <p>
* <!-- technical-bibtex-start -->
* BibTeX:
* <pre>
* @article{Galloway75,
* author = {M. M. Galloway},
* journal = {Computer Graphics Image Process},
* volume = {4},
* pages = {1971--1975},
* title = {Texture analysis using grey level run lengths},
* year = {1975}
* }
* </pre>
* <pre>
* @article{CSG90,
* author = {A. Chu and C. M. Sehgal and J. F. Greenleaf},
* journal = {Pattern Recognition Letters},
* volume = {11},
* number = {6},
* pages = {415--420},
* title = {Use of gray value distribution of run lengths for texture analysis},
* year = {1990}
* }
* </pre>
* <pre>
* @article{PeronaMalik1990,
* author = {K. Yogesan and T. Jørgensen and F. Albregtsen and K. J. Tveter and H. E. Danielsen},
* journal = {Cytometry},
* volume = {24},
* pages = {268--276},
* title = {Entropy based texture analysis of chromatin structure in advanced prostate cancer},
* year = {1996}
* }
* </pre>
* <pre>
* @article{PeronaMalik1990,
* author = {Pietro Perona and Jitendra Malik},
* journal = {IEEE Transaction on Pattern Analysis and Machine Intelligence},
* volume = {12},
* number = {7},
* pages = {629-639},
* publisher = {Morgan Kaufmann},
* title = {Scale-Space and Edge Detection Using Anysotropic Diffusion},
* year = {1990}
* }
* </pre>
<!-- technical-bibtex-end -->
</p>
*/
public class GrayLevelRunLengthMatrix
{
/** Nombre de niveaux de gris <=> Hauteur de la run length matrix.*/
protected int nbGrayLevel = -1 ;
/** Largeur de la matrice. Si FixedSize Alors Width=nbSizes, Sinon Width=TailleZoneMax/nbSizes+1.*/
protected int Width = -1 ;
/** Tableau representant la run length matrix (a la taille de la chaine la plus longue).*/
protected double[][] matrix = null ;
/** Tableau representant la run length matrix brute.*/
private double[][] mat = null ;
// Les directions de calculs.
/** La variation en X pour le calcul de la matrice.*/
protected int[] dx = new int[] {1, 1, 0,-1} ;
/** La variation en Y pour le calcul de la matrice.*/
protected int[] dy = new int[] {0, 1, 1, 1} ;
/** L'image (la vignette) sur laquelle on a calcule la matrice des longueurs de segments.*/
protected Image image = null ;
/** Un filtre gaussien, utile pour construire le marqueur du leveling.*/
private Gaussian gauss = new Gaussian(1, 1) ;
/** Un constructeur qui permet de modifier la taille de la run length matrix.
* @param nbGrayLevel Le nombre de niveaux de gris a prendre en compte.*/
public GrayLevelRunLengthMatrix(int nbGrayLevel)
{
if ( nbGrayLevel < 1 )
throw new IllegalArgumentException("Bad gray level: " + nbGrayLevel + ", wished [1..2^n < 65535]") ;
this.nbGrayLevel = nbGrayLevel ;
if ( nbGrayLevel == 1 ) return ; // Image binaire où l'on ne considère qu'une seule couleur.
if ( !Maths.isPowerOf(nbGrayLevel, 2) )
throw new IllegalArgumentException("Number of gray level must be a power of 2: " + nbGrayLevel) ;
}
/** Methode qui affecte la direction de calcul.
* @param DX Deplacement en X.
* @param DY Deplacement en Y.*/
public void setDirection(int DX, int DY)
{
int x = DX ;
int y = DY ;
if ( x < 0 ) x = -x ;
if ( y < 0 ) y = -y ;
if ( x > 1 || y > 1 ) throw new Error("Variation de direction trop importante. Attendu [-1..1].") ;
if ( x + y == 0 ) throw new Error("Direction nulle : x = y = 0.") ;
dx = null ;
dy = null ;
dx = new int[]{DX} ;
dy = new int[]{DY} ;
}
/** Methode qui affecte le nouveau tableau de direction. Si le tableau a une taille superieure a 1, une moyenne sera faite.
* @param dx Nouveau tableau de variation en X.
* @param dy Nouveau tableau de variation en Y.*/
public void setDirection(int[] dx, int[] dy)
{
if ( dx == null || dy == null ) throw new NullPointerException("Un des tableau est null.") ;
if ( dx.length != dy.length )
throw new IllegalArgumentException("Lengths of arrays different : " + dx.length + " & " + dy.length) ;
this.dx = null ;
this.dy = null ;
this.dx = dx ;
this.dy = dy ;
}
/** Methode qui lance et gere les differentes etapes de calcul.
* @param image L'image (la vignette) sur laquelle on doit calculer la matrice.
* @param reducer Classe qui permet de reduire le nombre de couleurs.
* @param leveling Methode de nivellement a utiliser avant la reduction des couleurs avec un filtre gaussien comme marqueur.
* Si ce parametre est null, alors rien n'est realise.
* @param ForbidenValue Valeur interdite (du fond) =>
* Valeur a ne pas prendre en compte pour les calculs. Si strictement negative, on prend toutes les valeurs en consideration.
* @param nbCPU Nombre de CPU autorises dans cette classe => Nombre de threads autorises.*/
public void FillMatrix(Image image, ColorReducer reducer, Leveling leveling, int ForbidenValue, int nbCPU)
{
if ( image.isColored() ) throw new IllegalArgumentException("Only gray level image supported.") ;
this.image = null ;
if ( leveling == null ) this.image = reducer.Reduce(image, nbGrayLevel, ForbidenValue) ;
else this.image = reducer.Reduce(leveling.Filter(image, nbCPU, gauss.Filter(image, nbCPU)), nbGrayLevel, ForbidenValue) ;
int x, y, i ;
int max = 0 ;
if ( image.getWidth() > image.getHeight() ) Width = image.getWidth() + 1 ;
else Width = image.getHeight() + 1 ;
mat = null ;
mat = new double[nbGrayLevel][Width] ;
ArraysOperations.SetConstant(mat, 0.0) ; // raz matrix
for (i=0 ; i < dx.length ; i++) FillMatrix(dx[i], dy[i], ForbidenValue) ;
// On trouve la largeur de la matrice.
for (y=0 ; y < nbGrayLevel ; y++)
for (x=0 ; x < Width ; x++)
if ( mat[y][x] > 0.0 && x > max ) max = x ;
// Moyenne
for (y=0 ; y < nbGrayLevel ; y++)
for (x=0 ; x < Width ; x++)
mat[y][x] /= (double)dx.length ;
// On trouve la largeur pour ajuster puis on moyenne
Width = max + 1 ;
matrix = null ;
matrix = new double[nbGrayLevel][Width] ;
for (y=0 ; y < nbGrayLevel ; y++)
for (x=0 ; x < Width ; x++)
matrix[y][x] = mat[y][x] ;
}
/** Methode qui effectue le remplissage de la matrice Run Length.
* @param dx Variation de calcul en X.
* @param dy Variation de calcul en Y.
* @param ForbidenValue Valeur a exclure des calculs.*/
protected void FillMatrix(int dx, int dy, int ForbidenValue)
{
int x, y, x1, y1, v0, nb ;
int height = image.getHeight() ;
int width = image.getWidth() ;
boolean Fin ;
int[][] tampon = new int[mat.length][mat[0].length] ;
ArraysOperations.SetConstant(tampon, 0) ; // Initialisation
for (y=0 ; y < height ; y++) // calculs des run length
for (x=0 ; x < width ; x++)
{ // pour chaque pixel
if ( image.Pixel(y, x) == ForbidenValue ) continue ; // On est sur la valeur interdite, donc on sort.
v0 = image.Pixel(y, x) ;
nb = 1 ;
Fin = false ;
x1 = x ;
y1 = y ;
do {
x1 += dx ;
if ( x1 < 0 || x1 >= width ) Fin = true ;
y1 += dy ;
if ( y1 < 0 || y1 >= height ) Fin = true ;
if ( !Fin && image.Pixel(y1, x1) != ForbidenValue && image.Pixel(y1, x1) == v0 ) nb++ ;
else Fin = true ;
} while ( !Fin ) ;
tampon[v0][nb]++ ; // on incrémente la matrice
}
for (y=0 ; y < nbGrayLevel ; y++) // On ajuste pour corriger les erreurs de comptages multiples.
for (x=1 ; x < Width ; x++)
tampon[y][x-1] -= tampon[y][x] ;
for (y=0 ; y < nbGrayLevel ; y++) // On met le résultat dans la matrice.
for (x=1 ; x < Width ; x++)
mat[y][x-1] += tampon[y][x] ; // On supprime la colonne 0 qui contient que des 0 (aucune chaine de longueur 0).
tampon = null ;
}
/* ------------------------------------------------------ Les getters ------------------------------------------------------ */
/** Methode qui retourne la run length matrix non ajustee.
* @return Le tableau de double[][] contenant la run length matrix.*/
public double[][] getMatrix()
{
return matrix ;
}
/** Tableau d'int contenant les variations de directions en X.
* @return Direction en X.*/
public int[] getDx()
{
return dx ;
}
/** Tableau d'int contenant les variations de directions en Y.
* @return Direction en Y.*/
public int[] getDy()
{
return dy ;
}
public String toString(String Separator)
{
StringBuffer sb = new StringBuffer() ;
for (int j=0 ; j < nbGrayLevel ; j++)
{
for (int i=0 ; i < Width ; i++) sb.append(matrix[j][i] + Separator) ;
sb.append("\n") ;
}
return sb.toString() ;
}
public void Display(String Separator)
{
System.out.println(toString(Separator)) ;
}
} |
Partager