Skip to content

Commit bbde5f4

Browse files
committed
fix sobel operation
1 parent aa924bd commit bbde5f4

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
lines changed

ImageProcess/bin/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Filter.class
2+
/ImageHandler.class
3+
/Main.class

ImageProcess/bin/ImageHandler.class

-9 Bytes
Binary file not shown.

ImageProcess/bin/Main.class

-9 Bytes
Binary file not shown.

ImageProcess/src/Filter.java

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,30 +197,54 @@ public static BufferedImage mean_filter(BufferedImage image){
197197
}
198198
return image;
199199
}
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]);
220232
}
221233
}
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;
224248
}
225249
public static BufferedImage scale_2_binary(BufferedImage image){
226250
int height = image.getHeight();

ImageProcess/src/ImageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void outputImage(BufferedImage image , String filename){
2525

2626
try {
2727
ImageIO.write(image, "jpg", new File(
28-
"C:\\Users\\CodingXiang\\Desktop\\image\\" + filename + ".png"));
28+
"/Users/user/Desktop/image/" + filename + ".png"));
2929

3030
} catch (IOException e) {
3131
System.out.println(e.getMessage());

ImageProcess/src/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class Main {
1111

1212
public static void main(String[] args) {
1313
// original image file path
14-
String image_path = "C:\\Users\\CodingXiang\\Desktop\\image\\origin_image.png";
14+
// String image_path = "\\Users\\user\\Desktop\\image\\origin_image.png";
15+
String image_path = "/Users/user/Desktop/image/origin_image.png";
1516
ImageHandler image_handler = new ImageHandler(image_path);
1617

1718
// convert original image to gray

0 commit comments

Comments
 (0)