Sobel Operator (Edge Detection)

• February 18th, 2011, 01:44 AM
Kimimaru
Sobel Operator (Edge Detection)
Hello everyone! I have a program that allows you to do 4 things by typing in the corresponding letter for each: (r) read a .pgm file, (S) smooth the file, (E) use edge detection on the file, (s) save the file, or (q) quit the program. In this program, I just had to fill in the code for saving a file (done) and writing the edge detection. Here's the code I have so far for the sobel operator:

Code :

```case 'E': boolean[][] bo = Sobel(pictureArray, 3); for(int i = 0; i<height; i++) { for(int j = 0; i<width; j++) { if(bo[i][j] ==true) { System.out.print("(" + i + "," + j + ") "); } } }   break;   public static boolean[][] Sobel(int[][] pictureArray, int threshold) { threshold=3; int c = 1; double Gx=0, Gy=0, G; boolean[][] b = new boolean[width][height]; int[][] array = new int[3][3]; int[][] sobel = {{-1,-2,-1},{0,0,0},{1,2,1}}; for(int i=0; i<height; i++) { for(int j=0; j<width; j++) { for (int k=0; k<3; k++) { for (int l=0; l<3; l++) { Gx += (sobel[k][l] * pictureArray[B][i][j][/B]); Gy += (sobel[l][k] * pictureArray[I][k][l][/I]); } } G = Math.sqrt(Math.pow(Gx,2)+Math.pow(Gy,2)); if (G > threshold) { b[i][j] = true; } else { b[i][j] = false; } } } return b; }```

The italicized and bold sections of code you see is where my professor said I was wrong. I get an ArrayOutOfBounds Exception and I'm not sure how to fix it. Can anyone help with this?
• February 18th, 2011, 09:33 AM
copeg
Re: Sobel Operator (Edge Detection)
Quote:

I get an ArrayOutOfBounds Exception and I'm not sure how to fix it. Can anyone help with this?
Add some println's in there to debug...check the values of the indexes you are trying to access and the values of the array lengths. Or, post an SSCCE. Based upon the posted code I can't even say this would compile:
Code :

`pictureArray[I][j])//What is I?`
• February 18th, 2011, 01:56 PM
Kimimaru
Re: Sobel Operator (Edge Detection)
Sorry about that; I italicized the text there and it didn't work well. It's now bolded.

Anyway, my professor said that on the first array index of pictureArray of "Gx += (sobel[k][l] * pictureArray[i][j])," I have to put something like "i(some mathematical expression other than division)k(some mathematical expression other than division)c(a constant)."

This is really annoying me because the constant can be anything...and on top of that, this lab assignment is far beyond the class' programming level, so I don't know why it was given to us in the first place. I tried an enormous amount of index combinations and they all failed. This assignment is due 8 hours from now. Can someone please correct my code and explain it to me? The program compiles, but I keep getting ArrayOutOfBoundsException whenever i enter "E" to detect the edges.
• February 18th, 2011, 02:26 PM
copeg
Re: Sobel Operator (Edge Detection)
Again, stripping out the problematic portions and posting an SSCCE as I suggested above would both let you break the problem down (and you learn in the process and might even see the error yourself) as well as give us context to reproduce and debug.
• February 18th, 2011, 05:21 PM
hybridxsv
Re: Sobel Operator (Edge Detection)
I'm also having a problem wtih this assignment so if I could get some help along the way that'd be chill.

As far as your program error goes, it's happening because you've flipped the height and width when creating that double boolean array. Try doing [height][width] instead and it should take care of it.