从java

时间:2016-05-01 19:03:33

标签: java arrays histogram pad

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

000 012 045

000 123 456

000 230 560

00线程“main”中的异常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;
    }

}

1 个答案:

答案 0 :(得分: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;
    }
}