2014-01-19 236 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