title: java image processing sobel edge detection
body: <p>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: </p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>And the code for that is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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 &lt; width ; i++ )
              {
                  for(j = 0 ; j &lt; 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&lt;width; i++) {
                  for (j=0; j&lt;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 &lt; width ; ii++ )
              {
                  for(int jj = 0 ; jj &lt; 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);
              }
          }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm really need some help. I hope someone will answer for that post, thank you.<br>
  Greetings :)</p>