3

エッジ検出にsobel演算子を使用しているJavaプログラムに問題がありますが、その機能を使用しようとすると、コンソールに次のように表示されます。

スレッド「AWT-EventQueue-0」の例外 java.lang.ArrayIndexOutOfBoundsException: Sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1015) で 262144、Obrazek.jButtonSobelActionPerformed(Obrazek.java:566) で

そのためのコードは次のとおりです。

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);
            }
        }

私は本当に助けが必要です。誰かがその投稿に答えてくれることを願っています、ありがとう。
ご挨拶:)

4

2 に答える 2