Search code examples
javaimage-processingedge-detectionsobel

java image processing sobel edge detection


I have a problem with java program, in which i'm using sobel operator for edge detection, but when I'm trying to use that funcion, console says:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 262144 at sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1015) at Obrazek.jButtonSobelActionPerformed(Obrazek.java:566)

And the code for that is:

FileInputStream inFile = null;   
        try {
            long beginTime = (new java.util.Date()).getTime();
            int i, j;
            double Gx[][], Gy[][], G[][];
            inFile = new FileInputStream("D://lenacsmall.bmp");
            BufferedImage bi = ImageIO.read(inFile);
            int width = bi.getWidth();
            int height = bi.getHeight();
            int[] pixels = new int[width * height];
            int[][] output = new int[width][height];
            int[] raster = bi.getRaster().getPixels(0,0,width,height,pixels); 
           
            int counter = 0;
            for(i = 0 ; i < width ; i++ )
            {
                for(j = 0 ; j < height ; j++ )
                {
                    output[i][j] = pixels[counter];
                    counter = counter + 1;
                }
            }   
            Gx = new double[width][height];
            Gy = new double[width][height];
            G  = new double[width][height];
            for (i=0; i<width; i++) {
                for (j=0; j<height; j++) {
                    if (i==0 || i==width-1 || j==0 || j==height-1)
                        Gx[i][j] = Gy[i][j] = G[i][j] = 0;
                    else{
                        Gx[i][j] = output[i+1][j-1] + 2*output[i+1][j] + output[i+1][j+1] -
                                output[i-1][j-1] - 2*output[i-1][j] - output[i-1][j+1];
                        Gy[i][j] = output[i-1][j+1] + 2*output[i][j+1] + output[i+1][j+1] -
                                output[i-1][j-1] - 2*output[i][j-1] - output[i+1][j-1];
                        G[i][j]  = Math.abs(Gx[i][j]) + Math.abs(Gy[i][j]);
                    }
                }
            }
            counter = 0;
            for(int ii = 0 ; ii < width ; ii++ )
            {
                for(int jj = 0 ; jj < height ; jj++ )
                {
                    pixels[counter] = (int) G[ii][jj];
                    counter = counter + 1;
                }
            }
            BufferedImage outImg = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
            outImg.getRaster().setPixels(0,0,width,height,pixels);
            FileOutputStream outFile = null;
            try {
                outFile = new FileOutputStream("D://copyTop.bmp");
            } catch (FileNotFoundException ex) {
                
            }
            try {
                ImageIO.write(outImg,"BMP",outFile);
            } catch (IOException ex) {
                
            }
            JFrame TheFrame = new JFrame("obrazek " + width + " " + height);
            JLabel TheLabel = new JLabel(new ImageIcon(outImg));
            TheFrame.getContentPane().add(TheLabel);
            TheFrame.setSize(600, 600);
            TheFrame.addWindowListener(new WindowAdapter() {   
          public void windowClosing(WindowEvent e) {   
            System.exit(0);                 
          }   
        });
            TheFrame.setVisible(true);   
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                inFile.close();
            } catch (IOException ex) {
                Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

I'm really need some help. I hope someone will answer for that post, thank you.
Greetings :)


Solution

  • In your for loops when you check

    if (i==0 || i==width-1 || j==0 || j==height-1)
    

    you should probably be checking for i >= width-2 rather than i==width-1.

    For example, if width is 10 it falls into the statement if i == 9. you want to catch if i == 8 since you later check [i+1], which in your code would be out of the bounds of your image array, since the maximum you can have is 9 (width-1).

    Obviously the same applies for j.