[英]Reading a 2d array from inside another 2D array in java
In order to do local histogram equalization, I am trying to read some rows and columns from a zero padded 2D array and do some process on them.为了进行局部直方图均衡,我试图从零填充的二维数组中读取一些行和列,并对它们进行一些处理。 At first step I pad my original array ( arr[][]
) with zero and save it into temp_arr[][]
which works fine.第一步,我用零填充我的原始数组( arr[][]
)并将其保存到temp_arr[][]
,它工作正常。 Then I try to read a 3*3 array from this temp_arr[][]
each time and save them in a 3*3 array to do some process on them.然后我尝试每次从这个temp_arr[][]
读取一个 3*3 数组并将它们保存在一个 3*3 数组中以对它们进行一些处理。 however, I get result partially and then an error.但是,我得到部分结果,然后出现错误。 That is what I get after running my code :这就是我运行我的代码后得到的:
000 012 045 000 012 045
000 123 456 000 123 456
000 230 560 000 230 560
00Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at lhe_test.main(lhe_test.java:28)线程“main”中的 00Exception java.lang.ArrayIndexOutOfBoundsException: 5 at lhe_test.main(lhe_test.java:28)
Can anyone help please?有人可以帮忙吗?
here is my code:这是我的代码:
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;
}
}
Added some comments in some problem areas在一些问题区域添加了一些注释
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.