IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Millie Discussion :

[Suggestion] Mise à jour Operator Canny


Sujet :

Millie

  1. #1
    Rédacteur
    Avatar de pseudocode
    Homme Profil pro
    Architecte système
    Inscrit en
    Décembre 2006
    Messages
    10 062
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Architecte système
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2006
    Messages : 10 062
    Points : 16 081
    Points
    16 081
    Par défaut [Suggestion] Mise à jour Operator Canny
    Optimisation de la partie "edge continuation" en utilisant une liste chainée des pixels à traiter.

    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
    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
    /**
     * Millie : Multifunctional Library For Image Processing
     * 
     * (c) Copyright 2009 by Humbert Florent
     * 
     *      This program is free software; you can redistribute it and/or modify  
     *      it under the terms of the GNU General Public License as published by  
     *      the Free Software Foundation; only version 2 of the License.          
     *                                                                            
     *      This program is distributed in the hope that it will be useful,       
     *      but WITHOUT ANY WARRANTY; without even the implied warranty of        
     *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
     *      GNU General Public License for more details.                          
     *                                                                            
     *      You should have received a copy of the GNU General Public License     
     *      along with this program; if not, write to the Free Software           
     *      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA             
     *      02111-1307, USA.                                                      
     */
    package millie.operator.detection;
     
    import java.util.LinkedList;
     
    import millie.image.Image;
    import millie.operator.commons.component.ComponentSeparableOp;
     
    /**
     * Canny Filter (edge detector)
     * 
     * @author Xavier Philippeau
     *
     */
    public class CannyOperator extends ComponentSeparableOp {
    	//private int gaussianwindow;
    	//private double gaussiansigma2;
    	private int lowvalue ;
    	private int highvalue;
    	private int[][] gaussianKernel = null;
    	private int gaussianKernelFactor = 0;
     
    	static private void setPixel(Image in, int x, int y, int canal, float value) {
    			if(x<0 || x>=in.getWidth() ||y<0 || y>=in.getHeight())
    				return;
    			in.setPixel(x,y,canal, value);
    	}
     
    	static private float getPixel(Image in, int x, int y, int canal) {
    		if(x<0 || x>=in.getWidth() ||y<0 || y>=in.getHeight())
    			return 0;
    		return in.getPixel(x,y,canal);
    	}
     
     
    	public CannyOperator(int gaussianwindow, double gaussiansigma2, int lowvalue, int highvalue) {
    		this.lowvalue = lowvalue;
    		this.highvalue = highvalue;
    		if(highvalue<lowvalue)
    			throw new IllegalArgumentException("Low value>high value");
     
    		initGaussianKernel(gaussianwindow,gaussiansigma2);
    	}
     
     
     
    	@Override
    	public void computeComponent(Image out, Image in, int canal) {
    		filter( out, in, canal, this.lowvalue, this.highvalue );
    	}
     
    	/**
             * Compute the gaussian kernel G(x,y) = Exp[ - (x^2+y^2)/(2*sigma^2) ]
             * 
             * @param window size of the kernel
             * @param sigma std-dev of the gaussian
             */
    	private void initGaussianKernel(int window, double sigma2) {
    		int aperture = window/2;
    		this.gaussianKernel = new int[2*aperture+1][2*aperture+1];
     
    		// factor to have only integers in the kernel 
    		int intFactor = 1000;
     
    		this.gaussianKernelFactor = 0;
    		for(int dy=-aperture;dy<=aperture;dy++) {
    			for(int dx=-aperture;dx<=aperture;dx++) {
    				double e = Math.exp( - (dx*dx+dy*dy) / (2*sigma2) );
    				int k = (int) Math.rint(intFactor * e); 
    				this.gaussianKernel[dx+aperture][dy+aperture]=k;
    				this.gaussianKernelFactor += k;
    			}
    		}
    	}
     
    	/**
             * Histogram stretching
             * 
             * @param c Image map
             */
    	public static void histogramStretch(Image bp, int canal) {
    		int width = bp.getWidth();
    		int height = bp.getHeight();
     
    		// search min/max value in the image 
    		int min=255,max=0;
    		for (int y=0; y<height; y++) {
    			for (int x=0; x<width; x++) {
    				int v = (int) getPixel(bp, x, y, canal);
    				if (v<min) min=v;
    				if (v>max) max=v;
    			}
    		}
     
    		// compute translation table
    		int color[] = new int[256];
    		for(int i=0;i<256;i++) {
    			double t = (double)(i-min)/(double)(max-min);
    			if (t<0) t=0;
    			if (t>1) t=1;
    			int v = (int)(255*t);
    			color[i] = v;
    		}
     
    		// replace value in the image
    		for (int y=0; y<height; y++) {
    			for (int x=0; x<width; x++) {
    				int v =  (int) getPixel(bp, x, y, canal);
    				setPixel(bp, x, y, canal, color[v]);
    			}
    		}
    	}
     
     
    	/**
             * Perform convolution (Image x Kernel) at a single point
             * 
             * @param c Image map
             * @param x x coord of the computation
             * @param y y coord of the computation
             * @param kernel the kernel matrix
             * @param factor the kernel factor
             * @return convolution result
             */
    	private int convolve(Image bp, int x, int y, int canal, int[][] kernel, int factor) {
    		int width = bp.getWidth();
    		int height = bp.getHeight();
     
    		// assume a square kernel
    		int aperture = kernel[0].length/2;  
     
    		int v = 0;
    		for(int dy=-aperture;dy<=aperture;dy++) {
    			for(int dx=-aperture;dx<=aperture;dx++) {
    				int xk = x + dx;
    				int yk = y + dy;
    				if (xk<0) xk=0;
    				if (xk>=width) xk=width-1;
    				if (yk<0) yk=0;
    				if (yk>=height) yk=height-1;
    				int vk = (int)  getPixel(bp, xk,yk, canal);
    				v += kernel[aperture+dy][aperture+dx] * vk;
    			}
    		}
    		v/=factor;
     
    		return v;
    	}
     
     
    	/**
             * Compute the local gradient
             * 
             * @param c Image map
             * @param x x coord of the computation (int)
             * @param y y coord of the computation (int)
             * @return norme and direction (-pi...pi) of the gradient
             */
    	private double[] gradient(Image bp, int x, int y, int canal) {
    		int width = bp.getWidth();
    		int height = bp.getHeight();
     
    		int px = x - 1;  // previous x
    		int nx = x + 1;  // next x
    		int py = y - 1;  // previous y
    		int ny = y + 1;  // next y
     
    		// limit to image dimension
    		if (px < 0)	px = 0;
    		if (nx >= width) nx = width - 1;
    		if (py < 0)	py = 0;
    		if (ny >= height) ny = height - 1;
     
    		// Intesity of the 8 neighbors
    		int Ipp = (int) getPixel(bp, px,py, canal);
    		int Icp = (int) getPixel(bp, x,py, canal);
    		int Inp = (int) getPixel(bp, nx,py, canal);
    		int Ipc = (int) getPixel(bp, px, y, canal);
    		int Inc = (int) getPixel(bp, nx, y, canal);
    		int Ipn = (int) getPixel(bp, px,ny, canal);
    		int Icn = (int) getPixel(bp, x,ny, canal);
    		int Inn = (int) getPixel(bp, nx,ny, canal);
     
    		// Local gradient
    		double r2 = 2.8284271247; // 2*Math.sqrt(2)
    		double gradx = (Inc-Ipc)/2.0 + (Inn-Ipp)/r2 + (Inp-Ipn)/r2; // horizontal + 2 diagonals
    		double grady = (Icn-Icp)/2.0 + (Inn-Ipp)/r2 + (Ipn-Inp)/r2; // vertical + 2 diagonals
     
    		// compute polar coordinates
    		double norme = Math.sqrt(gradx*gradx+grady*grady);
    		double angle = Math.atan2(grady, gradx);
     
    		// multiply norme by 8/3, to have the same values as Sobel Kernel 3x3
    		return new double[] { norme*2.6666666667, angle };
    	}
     
     
    	/**
             * Compute the local gradient (subpixel with bilinear interpolation)
             * 
             * @param c Image map
             * @param x x coord of the computation (double)
             * @param y y coord of the computation (double)
             * @return norme and direction (-pi...pi) of the gradient
             */
    	 private double[] gradientInterpolated(Image bp, double x, double y, int canal) {
     
    		 double wx = x - (int)x;
    		 double wy = y - (int)y;
     
    		 double[] Gpp = gradient(bp,(int)x,(int)y, canal);
    		 double[] Gnp = gradient(bp,(int)x+1,(int)y, canal);
    		 double[] Gpn = gradient(bp,(int)x,(int)y+1, canal);
    		 double[] Gnn = gradient(bp,(int)x+1,(int)y+1, canal);
     
    		 double norme = (1-wx)*(1-wy)*Gpp[0] + wx*(1-wy)*Gnp[0] + (1-wx)*wy*Gpn[0] + wx*wy*Gnn[0];
    		 double angle = (1-wx)*(1-wy)*Gpp[1] + wx*(1-wy)*Gnp[1] + (1-wx)*wy*Gpn[1] + wx*wy*Gnn[1];
     
    		 return new double[] { norme, angle };
    	 }
     
     
    	/**
             * compute if a position is a local maxima
             * 
             * @param c Image map
             * @param x x coord of the computation
             * @param y y coord of the computation
             * @return true if position is a local maxima
             */
    	private boolean isLocalMaxima(Image bp, int x, int y, int canal) {
     
    		// gradient at current position
    		double[] grad = gradient(bp,x,y, canal);
     
    		// gradient direction
    		double gx = Math.cos(grad[1]);
    		double gy = Math.sin(grad[1]);
     
    		// gradient value at next position in the gradient direction
    		double nx = x + gx;
    		double ny = y + gy;
    		double[] gradn = gradientInterpolated(bp,nx,ny, canal);
     
    		// gradient value at previous position in the gradient direction
    		double px = x - gx;
    		double py = y - gy;
    		double[] gradp = gradientInterpolated(bp,px,py, canal);
     
    		// is the current gradient value a local maxima ?
    		double EPSILON=1E-7;
    		if ((grad[0]-gradn[0])<EPSILON) return false;
    		if ((grad[0]-gradp[0])<EPSILON) return false;
     
    		return true;
    	}
     
    	/**
             * Perfom Canny filtering
             * 
             * @param c Image map
             * @param lowThreshold low value of the hysteresis
             * @param highThreshold high value of the hysteresis
             * @return filtered image map
             */
    	public void filter(Image out, Image c,int canal, int lowThreshold, int highThreshold) {
    		int LOWSTATE=92, HIGHSTATE=255;
    		int width = c.getWidth();
    		int height = c.getHeight();
     
    		Image bpg = new Image(c.getWidth(), c.getHeight(), c.getNumComponents());
    		// Gaussian filter
    		for (int y=0; y<height; y++) {
    			for (int x=0; x<width; x++) {
    				int v = convolve(c,x,y, canal, this.gaussianKernel,this.gaussianKernelFactor);
    				setPixel(bpg, x,y,canal, v);
    			}
    		}
     
    		// histogram stretching (compensate the loss of energy caused by gaussian filtering)
    		histogramStretch(bpg, canal);
     
    		// list of pixels above highThreshold
    		LinkedList<int[]> highpixels = new LinkedList<int[]>();
     
    		// gradient thresholding
    		for (int y=0; y<height; y++) {
    			for (int x=0; x<width; x++) {
     
    				// gradient intesity
    				double g = gradient(bpg,x,y, canal)[0];
     
    				// low-threshold -> not an edge
    				if (g<lowThreshold) continue;
     
    				// not a local maxima -> not an edge
    				if (!isLocalMaxima(bpg,x,y, canal)) continue;
     
    				// high-threshold -> is an edge
    				if (g>highThreshold) {
    					setPixel(out, x,y, canal, HIGHSTATE);
    					highpixels.addLast(new int[]{x,y});
    					continue;
    				}
     
    				// between thresholds -> "unknown state" (depends of neighbors)
    				setPixel(out, x,y, canal, LOWSTATE);
    			}
    		}
     
    		// edge continuation
    		int[] dx8 = new int[] {-1, 0, 1, 1, 1, 0,-1,-1};
    		int[] dy8 = new int[] {-1,-1,-1, 0, 1, 1, 1, 0};
     
    		while(!highpixels.isEmpty()) {
    			int[] pixel = highpixels.removeFirst();
    			int x=pixel[0], y=pixel[1];
     
    			// move low-state pixel to high-state
    			for(int k=0;k<8;k++) {
    				if (getPixel(out, x+dx8[k], y+dy8[k], canal)==LOWSTATE) {
    					setPixel(out,x+dx8[k], y+dy8[k], canal, HIGHSTATE);
    					highpixels.addLast(new int[]{x+dx8[k], y+dy8[k]});
    				}
    			}
    		}
     
    		// keep only high-state pixels
    		for (int y=0; y<height; y++)
    			for (int x=0; x<width; x++)
    				if (getPixel(out, x, y, canal)!=HIGHSTATE) setPixel(out,x,y,canal,0);
    	}
    }
    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

  2. #2
    Rédacteur

    Avatar de millie
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    7 015
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 7 015
    Points : 9 818
    Points
    9 818
    Par défaut
    MAJ
    Je ne répondrai à aucune question technique en privé

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Suggestion] Mise à jour Plugin Bloom
    Par pseudocode dans le forum Millie
    Réponses: 8
    Dernier message: 22/01/2010, 19h42
  2. [Suggestion] Mise à jour Operator Skeleton
    Par pseudocode dans le forum Millie
    Réponses: 1
    Dernier message: 01/11/2009, 18h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo