2014-01-19 238 views
0

我有下面这段代码,我不明白如何在这里填充二维数组int[][] integralImage2d奇怪的填充阵列

具体来说,我有这条线

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

我想这是java? –

回答

0

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

第一次和第二次在循环中打印时,它还没有在第1行设置任何值,因此它会打印0.第三次,值被设置在第0行的所有列中,因为赋值语句integralImage[i][j] = integralValue;。在你的情况,给你的输入图像,该值是12.

+0

现在都清楚了,谢谢。 – user2963044