2014-01-19 4 views
0

아래 코드가 있는데 두 번째 배열 int[][] integralImage이 어떻게 채워지는지 이해할 수 없습니다.2d 어레이가 이상하게 채워짐

구체적으로,이 선 루프에 대한 생성자 제 내부

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

있다. 출력은 다음과 같습니다. 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

Java의 배열 요소는 항상 자동으로 초기화됩니다. 각 값은 유형의 기본값으로 초기화됩니다. int의 경우 0입니다.

처음으로 루프를 인쇄 할 때 아직 행 1에 값이 설정되어 있지 않으므로 0을 인쇄합니다. 세 번째로 값은 행 0의 모든 열에 설정되었습니다. 할당 문 integralImage[i][j] = integralValue;. 귀하의 경우, 귀하의 입력 이미지가 주어진 그 값은 12입니다.

+0

지금 모두 깨끗합니다. 고마워요. – user2963044

관련 문제