1

为了进行局部直方图均衡,我试图从一个零填充的二维数组中读取一些行和列,并对它们进行一些处理。第一步,我arr[][]用零填充我的原始数组()并将其保存到temp_arr[][]其中工作正常。temp_arr[][]然后我尝试每次从中读取一个 3*3 数组并将它们保存在一个 3*3 数组中以对它们进行一些处理。但是,我得到部分结果,然后出现错误。这就是我运行代码后得到的结果:

000 012 045

000 123 456

000 230 560

00 线程“主”中的异常 java.lang.ArrayIndexOutOfBoundsException: 5 在 lhe_test.main(lhe_test.java:28)

有人可以帮忙吗?

这是我的代码:

public class lhe_test {

    public static int x=0 ; 
    public static double sum = 0;
    public static double mn =0;
    public static int [][] savedImage;
    public static int row , col;
    public static int [][] temp_tb= new int[ImagePro.pad][ImagePro.pad];


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int [][] arr = {{1,2,3},
                {4,5,6},{7,8,9},{10,11,12}};

        ImagePro.pad = 3 ;
        int temp_arr[][] = zero_pad(arr , ImagePro.pad);
        int p = ImagePro.pad/2; 

      for (int i = 0 ; i < temp_arr[0].length -p ; i ++){
        for (int j = 0 ; j< temp_arr.length - p ; j++){
        for (int ii=0 ; ii< ImagePro.pad ; ii++){
              for (int jj =0 ; jj<ImagePro.pad ; jj++){

                  int temp=temp_arr[ii+i][jj+j];
                  temp_tb[ii][jj]= temp;
                 System.out.print(temp_tb[ii][jj]); 
            }
              System.out.println();
                      }

          System.out.println();

        }
       }

      }
    public static int[][] zero_pad (int [][] imageData , int ratio){


        int w = imageData[0].length +((ratio-1));
        int h = imageData.length +((ratio-1));
        int [][]temp = new int[h][w];
        for (int i = 0 ; i<h ; i++){
            for (int j =0 ; j<w ; j++){
                temp[i][j]=0;
            }
        }

        for (int k=0 ; k<imageData.length ; k++){
            for (int l=0 ; l <imageData[0].length ; l++){
                temp[k+(ratio-1)/2][l+(ratio-1)/2]= imageData[k][l];
            }
        }
    return temp;
    }

}
4

1 回答 1

1

在一些问题区域添加了一些注释

import java.awt.image.BufferedImage;

public class lhe_test {

    public static int [][] temp_tb= new int[3][3];
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int [][] arr = {{1,2,3},
                {4,5,6},{7,8,9},{10,11,12}};

        int temp_arr[][] = zero_pad(arr , 3); // tmp_arr is [6][5]

        int p = 3/2; // integer division 3/2 = 1

        // i will have values 1,2,3
        for (int i = p ; i < temp_arr[0].length -p ; i ++){   
            // j will have values 1,2,3,4
            for (int j = p ; j< temp_arr.length -p ; j++){    
                // ii will have values 0,1,2,3,4, which is > 2
                for (int ii=i-p ; ii< i+ 2*p ; ii++){         
                    // jj will have values 0,1,2,3,4,5, which is > 2
                    for (int jj =j- p ; jj<j + 2*p  ; jj++){  
                        temp_tb[ii][jj]=temp_arr[ii][jj];
                    }

                }
            }
        }
    }
    public static int[][] zero_pad (int [][] imageData , int ratio){


        int w = imageData[0].length +((ratio-1)); 
        // w = 3+(3-1) = 5
        int h = imageData.length +((ratio-1)); 
        // h = 4+(3-1) = 6
        int [][]temp = new int[h][w];
        for (int i = 0 ; i<h ; i++){
            for (int j =0 ; j<w ; j++){
                temp[i][j]=0;
            }
        }

        for (int k=0 ; k<imageData.length ; k++){
            for (int l=0 ; l <imageData[0].length ; l++){
                temp[k+(ratio-1)/2][l+(ratio-1)/2]= imageData[k][l];
            }
        }
        return temp;
    }
}
于 2016-05-01T20:13:33.227 回答