问题
Let us take a look at this C# implementation,
... ...
// get source image size
int width = image.Width;
int height = image.Height;
int halfWidth = width / 2;
int halfHeight = height / 2;
// make sure the specified rectangle recides with the source image
rect.Intersect( new Rectangle( 0, 0, width, height ) );
int startX = -halfWidth + rect.Left;
int startY = -halfHeight + rect.Top;
int stopX = width - halfWidth - ( width - rect.Right );
int stopY = height - halfHeight - ( height - rect.Bottom );
int offset = image.Stride - rect.Width;
// calculate Hough map's width
int halfHoughWidth = (int) Math.Sqrt( halfWidth * halfWidth + halfHeight * halfHeight );
int houghWidth = halfHoughWidth * 2;
houghMap = new short[houghHeight, houghWidth];
// do the job
unsafe
{
byte* src = (byte*) image.ImageData.ToPointer( ) +
rect.Top * image.Stride + rect.Left;
// for each row
for ( int y = startY; y < stopY; y++ )
{
// for each pixel
for ( int x = startX; x < stopX; x++, src++ )
{
if ( *src != 0 )
{
// for each Theta value
for ( int theta = 0; theta < houghHeight; theta++ )
{
int radius = (int) Math.Round( cosMap[theta] * x - sinMap[theta] * y ) + halfHoughWidth;
if ( ( radius < 0 ) || ( radius >= houghWidth ) )
continue;
houghMap[theta, radius]++;
}
}
}
src += offset;
}
}
... ... ...
Q.1. rect.Intersect(new Rectangle( 0, 0, width, height));
- why is this line important?
Q.2. Why are values modified using rect
In the following code:
int startX = -halfWidth + rect.Left;
int startY = -halfHeight + rect.Top;
int stopX = width - halfWidth - ( width - rect.Right );
int stopY = height - halfHeight - ( height - rect.Bottom );
Q.3. Why are the y, and x loops starting in a negative point?
回答1:
Q.1.
This line simply ensures that the bounding rectangle is completely inside the image. If you were to skip this, and rect
is (partly) outside the image, you'd end up indexing out of bounds.
Note that pixel access is done through a pointer, incrementing the pointer by 1 for every x-increment, and by offset
for every y-increment. If rect
is larger than the image, we'd increment the pointer past the image buffer. If rect
is simply shifted out the image bounds, but not too large, we'd read pixels that do not correspond to the coordinates we're using.
Q.2.
Note that
int stopX = width - halfWidth - ( width - rect.Right );
int stopY = height - halfHeight - ( height - rect.Bottom );
can be simplified to
int stopX = - halfWidth + rect.Right;
int stopY = - halfHeight + rect.Bottom;
The code defines the origin of the coordinate system in the middle of the image. This bit of code shifts the bounding box (originally defined in the range [0,width
) and [0,height
) ) to the new coordinate system.
Usually the Hough transform is defined with the origin in the top-left pixel. But in principle it does not matter how the coordinate system is defined, this just modifies the parametrization of a line. The sinusoid drawn for each input pixel will be different, but the local maxima will still appear for the set of parameters corresponding to lines in the image.
来源:https://stackoverflow.com/questions/51863258/hough-transform-c-sharp-code