codes of houghTransform(standard)

I am studying the codes of hough transform(HoughLinesStandard) on this link hough transform.

Have some problems about the codes

int const numrho = cvRound(((width + height) * 2 + 1) / rho); //#1
for(int i = 0; i < height; ++i){
uchar const *img_ptr = img.ptr<uchar>(i);
for(int j = 0; j < width; ++j){
if( img_ptr[j] != 0 ){
for(int n = 0; n < numangle; ++n){
int r = cvRound( j * tabCos[n] + i * tabSin[n] );
r += (numrho - 1) / 2; //#2
++accum[(n+1) * (numrho+2) + r + 1];
}
}
}
}

Q1 : The first question is(#1), why is the size of numrho = cvRound(((width + height) * 2 + 1) / rho)?

let x = width, y = height

x * x + y * y = r * r --(1)
sqrt((x + y) * (x + y) - 2*x*y) = r --(2)
from(2) => x + y >= r
since openCV consider only points such that r > 0
=> r == 2 * (x + y)
+1 because we need to include the 0 point

Is this correct?Anything need to fix?

Q2 : why r += (numrho - 1) / 2; ? make sure it is always positive?

Thanks a lot

edit retag close merge delete