@@ -197,30 +197,54 @@ public static BufferedImage mean_filter(BufferedImage image){
197
197
}
198
198
return image ;
199
199
}
200
- public static BufferedImage sobel_filter (BufferedImage image ){
201
- BufferedImage Gx , Gy ;
202
- float [] x1 = {-1 , 0 , 1 , -2 , 0 , 2 , -1 , 0 , 1 };
203
- float [] y1 = {-1 ,-2 ,-1 ,0 ,0 ,0 ,1 ,2 ,1 };
204
- //2x2 matrix, with those two float arrays.
205
- Kernel MatrixA = new Kernel (3 , 3 , x1 );
206
- Kernel MatrixB = new Kernel (3 , 3 , y1 );
207
- ConvolveOp convolve1 = new ConvolveOp (MatrixA );
208
- ConvolveOp convolve2 = new ConvolveOp (MatrixB );
209
- Gx = convolve1 .filter (image , null );
210
- Gy = convolve2 .filter (image , null );
211
- for (int x =0 ; x <image .getWidth (); x ++) {
212
- for (int y =0 ; y <image .getHeight (); y ++) {
213
- int derp = Gx .getRGB (x ,y );
214
- int herp = Gy .getRGB (x ,y );
215
- double result = Math .sqrt (Math .pow (derp , 2.0 ) + Math .pow (herp , 2.0 ));
216
- if (result < 20726564.99 ) {
217
- image .setRGB (x ,y ,Color .white .getRGB ());
218
- } else {
219
- image .setRGB (x ,y ,Color .black .getRGB ());
200
+ public static BufferedImage sobel_filter (BufferedImage bi ){
201
+ int i ,j ;
202
+ int gx []={1 ,0 ,-1 ,2 ,0 ,-2 ,1 ,0 ,-1 };
203
+ int gy []={1 ,2 ,1 ,0 ,0 ,0 ,-1 ,-2 ,-1 };
204
+ double Gx [][], Gy [][], G [][];
205
+ int width = bi .getWidth ();
206
+ int height = bi .getHeight ();
207
+ int [] pixels = new int [width * height ];
208
+ int [][] output = new int [width ][height ];
209
+ int counter = 0 ;
210
+ for (i = 0 ; i < width ; i ++ )
211
+ {
212
+ for (j = 0 ; j < height ; j ++ )
213
+ {
214
+ output [i ][j ] = bi .getRGB (i , j );
215
+ counter = counter + 1 ;
216
+ }
217
+ }
218
+ Gx = new double [width ][height ];
219
+ Gy = new double [width ][height ];
220
+ G = new double [width ][height ];
221
+ for (i = 1 ; i < width - 1 ; i ++) {
222
+ for (j = 1 ; j < height - 1 ; j ++) {
223
+
224
+ if (i ==0 || i ==width -2 || j ==0 || j ==height -1 )
225
+ Gx [i ][j ] = Gy [i ][j ] = G [i ][j ] = 0 ;
226
+ else {
227
+ Gx [i ][j ] = output [i +1 ][j -1 ] + 2 *output [i +1 ][j ] + output [i +1 ][j +1 ] -
228
+ output [i -1 ][j -1 ] - 2 *output [i -1 ][j ] - output [i -1 ][j +1 ];
229
+ Gy [i ][j ] = output [i -1 ][j +1 ] + 2 *output [i ][j +1 ] + output [i +1 ][j +1 ] -
230
+ output [i -1 ][j -1 ] - 2 *output [i ][j -1 ] - output [i +1 ][j -1 ];
231
+ G [i ][j ] = Math .abs (Gx [i ][j ]) + Math .abs (Gy [i ][j ]);
220
232
}
221
233
}
222
- }
223
- return image ;
234
+ }
235
+ counter = 0 ;
236
+ for (int ii = 0 ; ii < width ; ii ++ )
237
+ {
238
+ for (int jj = 0 ; jj < height ; jj ++ )
239
+ {
240
+ bi .setRGB (ii , jj , (int ) G [ii ][jj ]);
241
+ counter = counter + 1 ;
242
+ }
243
+ }
244
+ BufferedImage outImg = new BufferedImage (width ,height ,BufferedImage .TYPE_BYTE_GRAY );
245
+ outImg .getRaster ().setPixels (0 ,0 ,width ,height ,pixels );
246
+
247
+ return bi ;
224
248
}
225
249
public static BufferedImage scale_2_binary (BufferedImage image ){
226
250
int height = image .getHeight ();
0 commit comments