2d阵列奇怪地填满了

时间:2014-01-19 07:30:21

标签: java arrays 2d

我在下面有这个代码块,我无法理解这里填充了2d数组int[][] integralImage

具体来说,我有这条线

  System.out.println(this.integralImage[1][1]); 

在构造函数中的第一个for循环中。它给我的输出是:0 0 12.为什么?我不知道它是如何填充的。

感谢。

/**
* Represents an integer integral image, which allows the user to query the mean
* value of an arbitrary rectangular subimage in O(1) time.  Uses O(n) memory,
* where n is the number of pixels in the image.
*
*/
public class IntegralImage2 {
private int[][] integralImage;
private int imageHeight; // height of image (first index)
private int imageWidth; // width of image (second index)

/**
 * Constructs an integral image from the given input image.  Throws an exception
 * if the input array is not rectangular.
 *
 */
public IntegralImage2(int[][] image) throws InvalidImageException {
    int[] imageRow;
    int integralValue;
    imageHeight = image.length;
    if (image.length > 0) {
        imageWidth = image[1].length;
    } else {
        imageWidth = 0;
    }

    integralImage = new int[imageHeight][imageWidth];

    for (int i = 0; i < imageHeight; i++) {
        System.out.println(this.integralImage[1][1]);
        imageRow = image[i];
        if (imageRow.length != imageWidth) {
            throw new InvalidImageException("Image is not rectangular");
        }
        for (int j = 0; j < imageWidth; j++) {
            integralValue = image[i][j];
            if (i > 0) {
                integralValue += integralImage[i - 1][j];
            }
            if (j > 0) {
                integralValue += integralImage[i][j - 1];
            }
            if (i > 0 && j > 0) {
                integralValue -= integralImage[i - 1][j - 1];
            }
            integralImage[i][j] = integralValue;
        }
    }
}

/**
 * Returns the mean value of the rectangular subimage specified by the
 * top, bottom, left and right parameters. The subimage should include
 * pixels in rows top and bottom and columns left and right.  For example,
 * top = 1, bottom = 2, left = 1, right = 2 specifies a 2 x 2 subimage starting
 * at (top, left) coordinate (1, 1).  Throws an exception if the specified
 * subimage is empty (top > bottom or left > right).
 */
public double meanSubImage(int top, int bottom, int left, int right) throws BoundaryViolationException {
    double mean;

    top = Math.max(top, 0);
    bottom = Math.min(bottom, imageHeight - 1);
    left = Math.max(left, 0);
    right = Math.min(right, imageWidth - 1);
    if (top > bottom || left > right) {
        throw new BoundaryViolationException("Invalid Subimage Indices");
    } else {
        mean = integralImage[bottom][right];
        if (top > 0) {
            mean -= integralImage[top - 1][right];
        }
        if (left > 0) {
            mean -= integralImage[bottom][left - 1];
        }
        if (top > 0 && left > 0) {
            mean += integralImage[top - 1][left - 1];
        }
        mean /= (bottom - top + 1) * (right - left + 1);
        return mean;
    }
}
public static void main(String[] args) {
    int[][] image1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int top,bottom,left,right;
    double mean;


    IntegralImage2 integralImage1;
    top = 1;
    bottom = 2;
    left = 1;
    right = 2;

    try {
        integralImage1 = new IntegralImage2(image1);
    } catch (InvalidImageException iix) {
        return;
    }
   try {
        mean = integralImage1.meanSubImage(top, bottom, left, right); //should be 7.0
        System.out.println("The mean of the subimage from ("
                + top + "," + left + ") to (" + bottom + "," + right + ") is " + mean);
    } catch (BoundaryViolationException bvx) {
        System.out.println("Index out of range.");
    }

}
}

1 个答案:

答案 0 :(得分:0)

Java中的数组元素始终自动初始化。每个值都初始化为类型的默认值; int为0(零)。

第一次和第二次在循环中打印时,它尚未在第1行中设置任何值,因此它打印0.第三次,由于赋值,在行0的所有列中设置了值声明integralImage[i][j] = integralValue;。在您的情况下,根据输入图像,该值为12。