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] ajouts filtres divers et variés


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] ajouts filtres divers et variés
    Squelette pour les images en niveau de gris, en accumulant les points appartenant au squelette pour différentes valeurs de seuils.

    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
    package millie.plugins.free;
     
    import java.awt.image.BufferedImage;
     
    import millie.plugins.PluginInfo;
    import millie.plugins.appimage.GenericAppImagePlugin;
    import millie.plugins.parameters.CheckBoxParameter;
    import millie.plugins.parameters.IntSliderParameter;
     
    /**
     * @author Xavier Philippeau
     *
     */
    @PluginInfo(name="Skeleton (grayscale)", category="Morphologique", description="Skeleton for grayscale image")
    public class SkeletonGrayscalePlugin extends GenericAppImagePlugin {
    	public SkeletonGrayscalePlugin() {
    		setReinitializable(true);
    		setLongProcessing(true);
    		addParameter(new IntSliderParameter("step", "Gray Level step", 1, 128, 16));
    		addParameter(new CheckBoxParameter("bright", "Bright background", false) );
    	}
     
    	@Override
    	public BufferedImage filter() throws Exception {
    		int step = getIntValue("step");
    		boolean useBrightBackground = getBooleanValue("bright");
     
    		// input image
    		BufferedImage input = getInputImage();
    		int W=input.getWidth(), H=input.getHeight();
    		int[][] gray = new int[W][H];
     
    		// convert input image to gray level array
    		for(int y=0;y<H;y++) {
    			for(int x=0;x<W;x++) {
    				int rgb = input.getRGB(x, y);
    				int r = (rgb>>16) & 0xFF, g = (rgb>>8) & 0xFF, b = (rgb) & 0xFF;
    				gray[x][y] = (int)(0.5 + 0.299*r + 0.587*g + 0.114*b);
    			}
    		}
     
    		// use negative image if required
    		if (useBrightBackground) {
    			for(int y=0;y<H;y++) for(int x=0;x<W;x++) gray[x][y] = 255 - gray[x][y];
    		}
     
    		// thinning using multiple threshold 
    		int[][] result = multipleThresholdThinning(gray, W, H, step);
     
    		// generate output image
    		BufferedImage output = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    		for(int y=0;y<H;y++) {
    			for(int x=0;x<W;x++) {
    				output.getRaster().setSample(x, y, 0, result[x][y]);
    				output.getRaster().setSample(x, y, 1, result[x][y]);
    				output.getRaster().setSample(x, y, 2, result[x][y]);
    			}
    		}
     
    		return output;
    	}
     
    	// ----------------------------------------------------------------------------------------------------
     
    	public int[][] multipleThresholdThinning(int[][] image, int width, int height, int step) {
     
    		// histogram of gray levels
    		int[] grayhisto = new int[256];
    		for(int y=0;y<height;y++)
    			for(int x=0;x<width;x++)
    				grayhisto[ image[x][y] ]++;
     
    		// CDF of gray levels
    		int[] graycdf = new int[256];
    		graycdf[0]=0;
    		for(int i=1;i<256;i++) graycdf[i]=graycdf[i-1]+grayhisto[i];
     
    		// accumulator of pixels belonging to a skeleton 
    		int[][] accumulator = new int[width][height];
     
    		// work image
    		byte[][] work = new byte[width][height];
     
    		// ** multiple binary thinning for different threshold values **
     
    		int plevel=0,level=0;
    		int EQStep = (int)(0.5+graycdf[255]/(double)step); // step in equalized image
    		while(true) {
     
    			// find next threshold values
    			while( (level<256) && (graycdf[level]-graycdf[plevel])<EQStep ) level++;
     
    			// update accumulator
    			for(int y=0;y<height;y++)
    				for(int x=0;x<width;x++)
    					if (work[x][y]==1) accumulator[x][y]+=(level-plevel);
     
    			if (level>=256) break;
    			plevel=level;
     
    			System.out.println("Skeleton : binary thinning using threshold="+level);
     
    			// create the thresholded image
    			for(int y=0;y<height;y++)
    				for(int x=0;x<width;x++)
    					if (image[x][y]>=level) work[x][y]=1; else work[x][y]=0; 
     
    			// perform binary thinning
    			new SkeletonBinary().thinning(work, width, height);
    		}
     
    		/*
    		for(int level=1;level<255;level++) {
     
    			// if we reach a new valid level 
    			if( (graycdf[level]-graycdf[plevel]) >= DELTA ) {
    				plevel=level;
     
    				System.out.println("Skeleton : binary thinning using threshold="+level+"  cdf="+graycdf[level]);
     
    				// create the thresholded image
    				for(int y=0;y<height;y++)
    					for(int x=0;x<width;x++)
    						if (image[x][y]>level) work[x][y]=1; else work[x][y]=0; 
     
    				// perform binary thinning
    				new SkeletonBinary().thinning(work, width, height);
    			}
     
    			// update accumulator
    			for(int y=0;y<height;y++)
    				for(int x=0;x<width;x++)
    					if (work[x][y]==1) accumulator[x][y]++;
     
    		}
    		*/
     
     
    		// rescale accumulator in 0...255
    		int max=0;
    		for(int y=0;y<height;y++) 
    			for(int x=0;x<width;x++) 
    				max = Math.max(max,accumulator[x][y]);
    		if (max>0) {
    			for(int y=0;y<height;y++) 
    				for(int x=0;x<width;x++) 
    					accumulator[x][y] = (int)(0.5+accumulator[x][y]*255.0/max);
    		}
     
    		// return the accumulator
    		return accumulator;
    	}
     
    	// ----------------------------------------------------------------------------------------------------
     
    	/** @author Xavier Philippeau (based on work of Dr. Chai Quek) */  
    	class SkeletonBinary {
     
    		// Smoothing pattern
    		private byte[] pattern1={-1,1,0,1,0,0,0,0};
    		private byte[] pattern2={0,1,0,1,-1,0,0,0};
    		private byte[] pattern3={0,0,-1,1,0,1,0,0};
    		private byte[] pattern4={0,0,0,1,0,1,-1,0};
    		private byte[] pattern5={0,0,0,0,-1,1,0,1};
    		private byte[] pattern6={-1,0,0,0,0,1,0,1};
    		private byte[] pattern7={0,1,0,0,0,0,-1,1};
    		private byte[] pattern8={0,1,-1,0,0,0,0,1};
     
    		// Neighbourhood
    		private int neighbourhood(byte[][] c,int x,int y) {
    			int neighbourhood=0;
    			if (c[x-1][y-1]==1) neighbourhood++;
    			if (c[x-1][y  ]==1) neighbourhood++;
    			if (c[x-1][y+1]==1) neighbourhood++;
    			if (c[x  ][y+1]==1) neighbourhood++;
    			if (c[x+1][y+1]==1) neighbourhood++;
    			if (c[x+1][y  ]==1) neighbourhood++;
    			if (c[x+1][y-1]==1) neighbourhood++;
    			if (c[x  ][y-1]==1) neighbourhood++;
    			return neighbourhood;
    		}
     
    		// Transitions Count
    		private int transitions(byte[][] c,int x,int y) {
    			int transitions=0;
    			if (c[x-1][y-1]==0 && c[x-1][y  ]==1) transitions++;
    			if (c[x-1][y  ]==0 && c[x-1][y+1]==1) transitions++;
    			if (c[x-1][y+1]==0 && c[x  ][y+1]==1) transitions++;
    			if (c[x  ][y+1]==0 && c[x+1][y+1]==1) transitions++;
    			if (c[x+1][y+1]==0 && c[x+1][y  ]==1) transitions++;
    			if (c[x+1][y  ]==0 && c[x+1][y-1]==1) transitions++;
    			if (c[x+1][y-1]==0 && c[x  ][y-1]==1) transitions++;
    			if (c[x  ][y-1]==0 && c[x-1][y-1]==1) transitions++;
    			return transitions;
    		}
     
    		// Match a pattern
    		private boolean matchPattern(byte[][] c,int x,int y,byte[] pattern) {
    			if (pattern[0]!=-1 && pattern[0]!=c[x-1][y-1]) return false;
    			if (pattern[1]!=-1 && pattern[1]!=c[x-1][y  ]) return false;
    			if (pattern[2]!=-1 && pattern[2]!=c[x-1][y+1]) return false;
    			if (pattern[3]!=-1 && pattern[3]!=c[x  ][y+1]) return false;
    			if (pattern[4]!=-1 && pattern[4]!=c[x+1][y+1]) return false;
    			if (pattern[5]!=-1 && pattern[5]!=c[x+1][y  ]) return false;
    			if (pattern[6]!=-1 && pattern[6]!=c[x+1][y-1]) return false;
    			if (pattern[7]!=-1 && pattern[7]!=c[x  ][y-1]) return false;
    			return true;
    		}
     
    		// Match one of the 8 patterns
    		private boolean matchOneOfPatterns(byte[][] c,int x,int y) {
    			if (matchPattern(c,x,y,pattern1)) return true;
    			if (matchPattern(c,x,y,pattern2)) return true;
    			if (matchPattern(c,x,y,pattern3)) return true;
    			if (matchPattern(c,x,y,pattern4)) return true;
    			if (matchPattern(c,x,y,pattern5)) return true;
    			if (matchPattern(c,x,y,pattern6)) return true;
    			if (matchPattern(c,x,y,pattern7)) return true;
    			if (matchPattern(c,x,y,pattern8)) return true;
    			return false;
    		}
     
    		/**
                     * Skeletonize the image using succesive thinning.
                     * 
                     * @param image  the image in an array[x][y] of values "0" or "1" 
                     * @param width of the image = 1st dimension of the array
                     * @param height of the image = 2nd dimension of the array
                     */
    		public void thinning(byte[][] image,int width,int height) {
     
    			// 3 columns back-buffer (original values)
    			byte[][] buffer = new byte[3][height];
     
    			// initialize the back-buffer
    			for(int y=0;y<height;y++) {
    				buffer[0][y]=0;
    				buffer[1][y]=image[0][y];
    				buffer[2][y]=image[1][y];
    			}
     
    			// loop until idempotence
    			while(true) {
     
    				boolean changed=false;
     
    				// for each columns
    				for(int x=1;x<(width-1);x++) {
     
    					// shift the back-buffer + set the last column
    					byte[] swp0 = buffer[0]; buffer[0]=buffer[1]; buffer[1]=buffer[2]; buffer[2]=swp0;
    					for(int y=0;y<height;y++) buffer[2][y]=image[x+1][y];
     
    					// for each pixel
    					for(int y=1;y<(height-1);y++) {
     
    						// pixel value
    						int v = image[x][y];
     
    						// pixel not set -> next
    						if (v==0) continue;
     
    						// is a boundary/extremity ?
    						int currentNeighbourhood = neighbourhood(buffer,1,y);
    						if (currentNeighbourhood<=1) continue;
    						if (currentNeighbourhood>=6) continue;
     
    						// is a connection ?
    						int transitionsCount = transitions(image,x,y);
    						if (transitionsCount==1 && currentNeighbourhood<=3) continue;
     
    						// no -> remove this pixel
    						if (transitionsCount==1) {
    							changed=true;
    							image[x][y]=0;
    							continue;
    						}
     
    						// can we delete this pixel ?
    						boolean matchOne = matchOneOfPatterns(image,x,y);
     
    						// yes -> remove this pixel
    						if (matchOne) {
    							changed=true;
    							image[x][y]=0;
    							continue;
    						}
    					}
    				}
     
    				// no change -> return result
    				if (!changed) return;
    			}
    		}
    	}
    }
    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

  2. #2
    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
    Fast MeanShift (1D) en utilisant la même technique que pour le Fast Median

    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
    package millie.plugins.free;
     
    import java.awt.image.BufferedImage;
    import java.util.Arrays;
     
    import millie.plugins.PluginInfo;
    import millie.plugins.appimage.GenericAppImagePlugin;
    import millie.plugins.parameters.CheckBoxParameter;
    import millie.plugins.parameters.IntSliderParameter;
     
    /**
     * Fast MeanShift (1D), using sliding histogram window
     * 
     * @author Xavier Philippeau
     * 
     */
    @PluginInfo(name="FastMeanShift", category="Restauration", description="Fas tMeanShift filter (1D)")
    public class FastMeanShiftPlugin extends GenericAppImagePlugin {
     
    	public FastMeanShiftPlugin() {
    		setLongProcessing(true);
    		setReinitializable(true);
     
    		addParameter(new IntSliderParameter("modeseeking_radius", "Mode seeking radius ", 1, 128, 15));
    		addParameter(new IntSliderParameter("sampling_radius", "Sampling radius", 1, 128, 5));
    		addParameter(new CheckBoxParameter("useYUV", "use YUV colorspace", false));	
    	}
     
    	@Override
    	public BufferedImage filter() throws Exception {
    		BufferedImage input = getInputImage();
    		int sampling_radius = getParameter("sampling_radius").getIntValue();
    		int modeseeking_radius = getParameter("modeseeking_radius").getIntValue();
    		boolean useYUV = getParameter("useYUV").getBooleanValue();
     
    		if (useYUV && input.getRaster().getNumBands()>=3) {
    			BufferedImage inputcopy = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_RGB);
    			inputcopy.getGraphics().drawImage(input, 0, 0, null);
    			RGB2YUV(inputcopy);
    			BufferedImage output = filter(inputcopy,sampling_radius,modeseeking_radius);
    			YUV2RGB(output);
    			return output;
    		}
     
    		return filter(input,sampling_radius,modeseeking_radius);
    	}
     
    	// ------------------------------------------------------------
     
    	private BufferedImage filter(BufferedImage input, int radius, int range) {
    		int W=input.getWidth(), H=input.getHeight();
    		BufferedImage output = new BufferedImage(W, H, input.getType());
     
    		// histogram-left = histogram for 1st pixel at line "y"
    		double[] hist_left = new double[256];
     
    		// histogram updated 
    		double[] hist = new double[256];
     
    		// gaussian weighting function
    		double[] gaussian = new double[2*range+1];
    		double sigma2 = Math.pow(range/2,2); // range = 2*sigma
    		for(int i=-range;i<=range;i++)
    			gaussian[i+range]=Math.exp(i*i/(2*sigma2));
     
    		for(int band=0;band<input.getRaster().getNumBands();band++) {
     
    			// initialize histogram-left (at pixel(-1,-1))
    			Arrays.fill(hist_left, 0);
    			for(int dy=-radius;dy<=radius;dy++) {
    				for(int dx=-radius;dx<=radius;dx++) {
    					int xk = -1+dx, yk = -1+dy;
    					if (xk<0) xk=-xk;
    					if (xk>=W) xk=2*W-xk-1;
    					if (yk<0) yk=-yk;
    					if (yk>=H) yk=2*H-yk-1;
    					int pk = input.getRaster().getSample(xk, yk, band);
    					hist_left[pk]++;
    				}
    			}
     
    			for(int y=0;y<H;y++) {
     
    				// at each new line, slide "histogram-left" one pixel to the bottom
     
    				// remove info for top horizontal-strip
    				for(int dx=-radius;dx<=radius;dx++) {
    					int xk = -1+dx, yk = (y-1)-radius;
    					if (xk<0) xk=-xk;
    					if (xk>=W) xk=2*W-xk-1;
    					if (yk<0) yk=-yk;
    					if (yk>=H) yk=2*H-yk-1;
    					int pk = input.getRaster().getSample(xk, yk, band);
    					hist_left[pk]--;
    				}
     
    				// add info for bottom horizontal-strip
    				for(int dx=-radius;dx<=radius;dx++) {
    					int xk = -1+dx, yk = y+radius;
    					if (xk<0) xk=-xk;
    					if (xk>=W) xk=2*W-xk-1;
    					if (yk<0) yk=-yk;
    					if (yk>=H) yk=2*H-yk-1;
    					int pk = input.getRaster().getSample(xk, yk, band);
    					hist_left[pk]++;
    				}
     
    				// copy histogram-left to histogram
    				for(int i=0;i<256;i++) hist[i]=hist_left[i];
     
    				for(int x=0;x<W;x++) {
     
    					// at each new column, slide current histogram one pixel to the right
     
    					// remove info for left vertical-strip
    					for(int dy=-radius;dy<=radius;dy++) {
    						int xk = (x-1)-radius, yk = y+dy;
    						if (xk<0) xk=-xk;
    						if (xk>=W) xk=2*W-xk-1;
    						if (yk<0) yk=-yk;
    						if (yk>=H) yk=2*H-yk-1;
    						int pk = input.getRaster().getSample(xk, yk, band);
    						hist[pk]--;
    					}
     
    					// add info for right vertical-strip  
    					for(int dy=-radius;dy<=radius;dy++) {
    						int xk = x+radius, yk = y+dy;
    						if (xk<0) xk=-xk;
    						if (xk>=W) xk=2*W-xk-1;
    						if (yk<0) yk=-yk;
    						if (yk>=H) yk=2*H-yk-1;
    						int pk = input.getRaster().getSample(xk, yk, band);
    						hist[pk]++;
    					}
     
    					// mode seeking
    					int p = input.getRaster().getSample(x, y, band);
    					for(int loop=0;loop<32;loop++) {
    						int hleft  = Math.max(0,   p-range);
    						int hright = Math.min(255, p+range);
    						double mean=0, wsum=0;
    						for(int i=hleft;i<=hright;i++) {
    							double w = gaussian[i-p+range] * hist[i];
    							mean+=i*w; wsum+=w; 
    						}
    						mean/=wsum;
    						int pshift = (int)(mean+0.5);
    						if (pshift==p) break;
    						p=pshift;
    					}
     
    					// replace input pixel by mode
    					output.getRaster().setSample(x, y, band, p);
    				}
    			}
    		}
     
    		return output;
    	}
     
    	// ------------------------------------------------------------
     
    	static void RGB2YUV(BufferedImage image) {
    		int W=image.getWidth(), H=image.getHeight();
    		for(int y=0;y<H;y++) {
    			for(int x=0;x<W;x++) {
    				// get value
    				int R = image.getRaster().getSample(x, y, 0);
    				int G = image.getRaster().getSample(x, y, 1);
    				int B = image.getRaster().getSample(x, y, 2);
     
    				// convert
    				double Y = 0.299*R + 0.587*G + 0.114*B;
    				double U = 128 + (B-Y)*0.565;
    				double V = 128 + (R-Y)*0.713;
     
    				// round up
    				int iY = (int)(Y+0.5);
    				int iU = (int)(U+0.5);
    				int iV = (int)(V+0.5);
     
    				// clamp
    				iY = Math.max(0, Math.min(255, iY));
    				iU = Math.max(0, Math.min(255, iU));
    				iV = Math.max(0, Math.min(255, iV));
     
    				// replace value
    				image.getRaster().setSample(x, y, 0, iY);
    				image.getRaster().setSample(x, y, 1, iU);
    				image.getRaster().setSample(x, y, 2, iV);
    			}
    		}
    	}
     
    	static void YUV2RGB(BufferedImage image) {
    		int W=image.getWidth(), H=image.getHeight();
    		for(int y=0;y<H;y++) {
    			for(int x=0;x<W;x++) {
    				// get value
    				int Y = image.getRaster().getSample(x, y, 0);
    				int U = image.getRaster().getSample(x, y, 1);
    				int V = image.getRaster().getSample(x, y, 2);
     
    				// convert
    				double R = Y + (1.370705 * (V-128));
    				double G = Y - (0.698001 * (V-128)) - (0.337633 * (U-128));
    				double B = Y + (1.732446 * (U-128));
     
    				// round up
    				int iR = (int)(0.5+R);
    				int iG = (int)(0.5+G);
    				int iB = (int)(0.5+B);
     
    				// clamp
    				iR = Math.max(0, Math.min(255, iR));
    				iG = Math.max(0, Math.min(255, iG));
    				iB = Math.max(0, Math.min(255, iB));
     
    				// replace value
    				image.getRaster().setSample(x, y, 0, iR);
    				image.getRaster().setSample(x, y, 1, iG);
    				image.getRaster().setSample(x, y, 2, iB);
    			}
    		}
    	}
     
    }
    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

  3. #3
    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
    Filtrage linéaire par une dérivée de gaussienne, ordre 1 et 2.

    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
    package millie.plugins.free;
     
    import java.awt.image.BufferedImage;
     
    import millie.automation.Automatable;
    import millie.plugins.PluginInfo;
    import millie.plugins.appimage.GenericAppImagePlugin;
    import millie.plugins.parameters.CheckBoxParameter;
    import millie.plugins.parameters.ComboBoxParameter;
    import millie.plugins.parameters.DoubleSliderParameter;
     
    /**
     * @author Xavier Philippeau
     *
     */
    @PluginInfo(name="Gaussian derivative", category="Analyse", description="Gaussian derivative filter")
    public class GaussianDerivativePlugin extends GenericAppImagePlugin implements Automatable {
     
    	private static String FIRSTORDER = "1st order";
    	private static String SECONDORDER = "2nd order";
     
    	public GaussianDerivativePlugin() {
    		setPluginName("MDIF");
    		setRefreshable(false);
    		setLongProcessing(true);
     
    		addParameter(new ComboBoxParameter("order","Derivative", new String[]{FIRSTORDER,SECONDORDER} ));
    		addParameter(new DoubleSliderParameter("sigma", "sigma",   0,5,0.1,0.7));
    		addParameter(new DoubleSliderParameter("logpower", "Log scale",   0,1,0.01,0.5));
    		addParameter(new CheckBoxParameter("intensity", "multiply by intensity", false));
    	}
     
    	@Override
    	public BufferedImage filter() throws Exception {
    		BufferedImage input = getInputImage();
    		double sigma = getDoubleValue("sigma");
    		double logpower = getDoubleValue("logpower");
    		boolean showintensity = getBooleanValue("intensity");
     
    		int order = 0;
    		if (getStringValue("order")==FIRSTORDER) order=1;
    		if (getStringValue("order")==SECONDORDER) order=2;
     
    		return filter(input, order, sigma, logpower, showintensity);
    	}
     
    	public BufferedImage filter(BufferedImage input, int order, double sigma, double logpower, boolean showintensity) throws Exception {
    		int width = input.getWidth();
    		int height = input.getHeight();
     
    		BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), input.getType());
     
    		// luminosity input image
    		int[][] in = new int[width][height];
    		for (int y=0; y<height; y++) {
    			for (int x=0; x<width; x++) {
    				int rgb = input.getRGB(x, y);
    				int r = (rgb>>16) & 0xFF, g = (rgb>>8) & 0xFF, b = (rgb) & 0xFF;
    				int l = (int)(0.299*r + 0.587*g + 0.114*b);
    				in[x][y] = l;
    			}
    		}
     
    		// build kernel
    		double normalizer=0;
    		int radius=0;
    		double[][] kernel = null;
     
    		if (order==1) {
    			radius = (int)(1+2*sigma);
    			kernel = new double[2*radius+1][2*radius+1];
    			normalizer = buildFirstOrderKernel(radius, sigma, kernel);
    		}
    		if (order==2) {
    			radius = (int)(1+4*sigma);
    			kernel = new double[2*radius+1][2*radius+1];
    			normalizer = buildSecondOrderKernel(radius, sigma, kernel);
    		}
     
    		// logscale
    		int[] logscale = new int[256];
    		for(int i=0;i<256;i++) {
    			logscale[i]=(int)(255*Math.pow(i/255.0,logpower));
    		}
     
    		// convolution
    		for (int y=0; y<height; y++) {
    			for (int x=0; x<width; x++) {
     
    				double gx = 0, gy = 0;
    				for(int dy=-radius;dy<=radius;dy++) {
    					for(int dx=-radius;dx<=radius;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;
     
    						double vk = in[xk][yk];
    						gx += kernel[radius-dy][radius-dx] * vk;
    						gy += kernel[radius-dx][radius-dy] * vk;
    					}
    				}
    				gx/=normalizer;
    				gy/=normalizer;
     
    				// norm
    				double gn=Math.sqrt(gx*gx+gy*gy);
     
    				// multiply by original gray value
    				double v = gn;
    				if (showintensity) {
    					int gray = in[x][y];
    					v = gn*gray/255.0;
    				}
     
    				if (v<0) v=0;
    				if (v>255) v=255;
    				int iv = logscale[(int)v];
    				output.getRaster().setSample(x, y, 0, iv);
    				output.getRaster().setSample(x, y, 1, iv);
    				output.getRaster().setSample(x, y, 2, iv);
    			}
    		}
     
    		return output;
    	}
     
    	private double buildFirstOrderKernel(int radius, double sigma, double[][] kernel) {
    		double sigma2 = sigma*sigma;
    		double normalizer=0;
    		for(int y=-radius;y<=radius;y++) {
    			for(int x=-radius;x<=radius;x++) {
    				double t = (x*x+y*y)/(2*sigma2);
    				double dt = -x / sigma2 ;
    				double e = dt * Math.exp( -t );
    				kernel[radius+x][radius+y] = e;
    				normalizer+=Math.abs(e);
    			}
    		}
    		return normalizer;
    	}
     
    	private double buildSecondOrderKernel(int radius, double sigma, double[][] kernel) {
    		double sigma2 = sigma*sigma;
    		double normalizer=0;
    		for(int y=-radius;y<=radius;y++) {
    			for(int x=-radius;x<=radius;x++) {
    				double t = (x*x+y*y)/(2*sigma2);
    				double d2t = (x*x-sigma2) / (sigma2*sigma2);
    				double e = d2t * Math.exp( -t );
    				kernel[radius+x][radius+y] = e;
    				normalizer+=Math.abs(e);
    			}
    		}
    		return normalizer;
    	}
    }
    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

  4. #4
    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
    Detection de coins par la méthode FAST

    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
    package millie.plugins.free;
     
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.Raster;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
     
    import millie.automation.Automatable;
    import millie.plugins.PluginInfo;
    import millie.plugins.appimage.GenericAppImagePlugin;
    import millie.plugins.parameters.DoubleSliderParameter;
     
    /**
     * @author X.Philippeau, based on work of Edward Rosten and Tom Drummond
     *
     * see "Machine learning for high-speed corner detection", for details.
     *
     */
    @PluginInfo(name="FAST Corner Detector", category="Analyse", description="FAST Corner Detector")
    public class FASTCornerDetectorPlugin extends GenericAppImagePlugin implements Automatable {
     
    	private int[][] circle16 = {
    			{ 0,-3}, {+1,-3}, {+2,-2}, {+3,-1}, {+3, 0}, {3,+1}, {+2,+2}, 
    			{+1,+3}, { 0,+3}, {-1,+3}, {-2,+2},	{-3,+1}, {-3, 0}, {-3,-1},
    			{-2,-2}, {-1,-3}
    	};
     
    	public FASTCornerDetectorPlugin() {
    		setRefreshable(true);
    		setCacheable(true);
    		//setLongProcessing(true);
     
    		addParameter(new DoubleSliderParameter("sensibility", "bright/dark sensibility", 0.0, 1.0, 0.01, 0.50));
    	}
     
    	@Override
    	public BufferedImage filter() throws Exception {
    		BufferedImage input = getInputImage();
    		int width=input.getWidth(), height=input.getHeight();
     
    		BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     
    		BufferedImage grayscale = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    		for (int y = 0; y < height; y++) {
    			for (int x = 0; x < width; x++) {
    				int rgb = input.getRGB(x, y);
    				int r = (rgb>>16) & 0xFF, g = (rgb>>8) & 0xFF, b = (rgb>>0) & 0xFF;
     
    				int gray = (int)(0.299*r + 0.587*g + 0.114*b);
    				grayscale.getRaster().setSample(x, y, 0, gray);
     
    				r*=0.8;g*=0.8;b*=0.8;
    				rgb=(r<<16)|(g<<8)|(b);
    				output.setRGB(x, y, rgb);
    			}
    		}
     
    		List<int[]> corners = findCorners(grayscale, 0);
     
    		Graphics2D g2d = output.createGraphics();
    		g2d.setColor(Color.WHITE);
    		for(int[] p : corners) g2d.drawOval(p[0]-4, p[1]-4, 2*4, 2*4);
     
    		return output;
    	}
     
    	private List<int[]> findCorners(BufferedImage input, int band) {
    		double sensibility = getDoubleValue("sensibility");
    		// s=0.0 --> factor = 4
    		// s=0.5 --> factor = 1
    		// s=1.0 --> factor = 0
    		double factor = Math.pow( 2*(1.0-sensibility) , 2);
     
    		Raster raster = input.getRaster();
    		int width=input.getWidth(), height=input.getHeight();
     
    		double X=0,X2=0,COUNT=0;
    		for (int y = 0; y < height; y++) {
    			for (int x = 0; x < width; x++) {
    				int i = raster.getSample(x, y, band);
    				X+=i;
    				X2+=i*i;
    				COUNT++;
    			}
    		}
    		double variance  = (X2/COUNT) - ((X*X)/(COUNT*COUNT));
    		double stddev = Math.sqrt(variance);
    		int T = (int)(stddev * factor);
     
    		System.out.println("s="+sensibility+", f="+factor+" --> T="+T);
     
     
    		List<int[]> corners = new LinkedList<int[]>();
     
    		int R=3;
    		for (int y=R; y<height-R; y++) {
    			for (int x=R; x<width-R; x++) {
    				int p = raster.getSample(x, y, band);
     
    				// high-speed exclusion test
    				int dcount=0, bcount=0;
    				for (int i=0;i<16;i+=4) {
    					int s = raster.getSample( x+circle16[i%16][0] , y+circle16[i%16][1] , band );
    					if (s<(p-T)) dcount++;
    					if (s>(p+T)) bcount++;
    				}
    				if (dcount<3 && bcount<3) continue;
     
    				// complete scan (with overlap)
    				int Vdark=0, Vbright=0;
    				dcount=0; bcount=0;
    				boolean isPOI = false;
    				for (int i=0;i<16+12;i++) {
    					int s = raster.getSample( x+circle16[i%16][0] , y+circle16[i%16][1] , band );
    					if (s<(p-T)) { // dark
    						if (i<16) Vdark+=(p-T)-s;
    						dcount++;
    						if (dcount>=12) isPOI=true;
    					} else dcount=0;
    					if (s>(p+T)) { // bright
    						if (i<16) Vbright+=s-(p+T);
    						bcount++;
    						if (bcount>=12) isPOI=true;
    					} else bcount=0;
    				}
     
    				// add POI to the list
    				if (isPOI) {
    					int V=Math.max(Vdark,Vbright);
    					corners.add(new int[]{x,y,V});
    				}
    			}
    		}
     
    		// Non-maximal suppression
    		Iterator<int[]> iter = corners.iterator();
    		int dist2max = (int)Math.pow(2*R,2);
    		while(iter.hasNext()) {
    			int[] p = iter.next();
    			for(int[] n:corners) {
    				if (n==p) continue;
    				int dist2 = (p[0]-n[0])*(p[0]-n[0])+(p[1]-n[1])*(p[1]-n[1]);
    				if( dist2>dist2max ) continue;
    				if (n[2]<p[2]) continue;
    				iter.remove();
    				break;
    			}
    		}
     
    		return corners;
    	}
    }
    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

  5. #5
    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
    Pour ceux qui se demandent ce que ca peut donner :

    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

  6. #6
    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
    Juste pour info, si tu vois des problèmes de performances.

    J'avais mis en place il y a déjà quelques temps la classe : ImageProcessor pour travailler plus directement avec le tableau de pixel de l'image (getRGB/setRGB sur un BufferedImage pouvant être un peu long)


    Exemple :

    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
    public class Rotation180Operator extends SimpleOperator {
     
    	public void compute(BufferedImage output, BufferedImage input) {
    		ImageProcessor oP = new ImageProcessor(output);
    		ImageProcessor iP = new ImageProcessor(input);
    		int[] pixels = iP.getPixels();
    		int[] oPixels = oP.getPixels();
     
    		for(int j=0; j<input.getHeight(); j++)
    			for(int i=0; i<input.getWidth(); i++) {
    				int pixel = pixels[i+input.getWidth()*j];
    				oPixels[input.getWidth()-1-i+(input.getHeight()-1-j)*output.getWidth()] = pixel;
    			}
     
    		oP.commit();
     
    	}
    }

    J'ai converti pas mal de mes opérateurs avec ça, et l'amélioration est assez flagrante
    Je ne répondrai à aucune question technique en privé

  7. #7
    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
    Citation Envoyé par millie Voir le message
    Juste pour info, si tu vois des problèmes de performances.

    J'avais mis en place il y a déjà quelques temps la classe : ImageProcessor pour travailler plus directement avec le tableau de pixel de l'image (getRGB/setRGB sur un BufferedImage pouvant être un peu long)

    J'ai converti pas mal de mes opérateurs avec ça, et l'amélioration est assez flagrante
    J'ai pas trop de problème de perfs sur mon quadcore (). C'est vrai que je pourrais utiliser ta classe (ou le DataBuffer) pour éviter les 2/3 indirections des accesseurs du Raster. Je vais voir ce que ca donne.

    En fait, vu le nom de la classe (ImageProcessor), je pensais que c'était lié a l'automatisation des traitements.
    ALGORITHME (n.m.): Méthode complexe de résolution d'un problème simple.

Discussions similaires

  1. [Suggestion] Ajout filtre bilateral (complet)
    Par pseudocode dans le forum Millie
    Réponses: 0
    Dernier message: 16/04/2011, 17h23
  2. [Suggestion] Ajout de traitement en masse
    Par millie dans le forum Millie
    Réponses: 13
    Dernier message: 28/01/2009, 17h36
  3. [Suggestion] Ajout Plugin Hough
    Par pseudocode dans le forum Millie
    Réponses: 3
    Dernier message: 12/12/2008, 19h10
  4. [Builder C++]Petits soucis divers et variés.
    Par WindSpirit dans le forum C++
    Réponses: 9
    Dernier message: 23/04/2008, 18h06
  5. Ajouter filtres à DirectShowLib
    Par TERRIBLE dans le forum API graphiques
    Réponses: 0
    Dernier message: 21/04/2008, 19h41

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