Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Sobel Operator (Edge Detection)

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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:

    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?
    Last edited by Kimimaru; February 18th, 2011 at 02:48 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Sobel Operator (Edge Detection)

    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:
    pictureArray[I][j])//What is I?

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.

  6. The Following User Says Thank You to hybridxsv For This Useful Post:

    Kimimaru (February 18th, 2011)

Similar Threads

  1. Canny Edge Detection
    By tiny in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2012, 01:54 PM
  2. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM
  3. how to extract variables,keywords,operator...
    By Nanda in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2009, 10:19 PM
  4. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 09:50 AM
  5. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM