VC Image ProcessingProgramLine Detection is often used, and the commonly used line detection method is the Hough transformation.
As one of the basic methods for recognizing geometric shapes in image processing. The basic principle of Hough transformation is to convert the given curve of the original image space into a point in the parameter space through curve expression by using the parity between the point and the line. In this way, the detection problem of the given curve in the original image is transformed into the peak problem in the search parameter space. That is, the overall detection feature is transformed into a local detection feature. For example, straight lines, ovans, circles, and arcs.
In short, the idea of the Hough transformation is: a point in the original image coordinate system corresponds to a straight line in the parameter coordinate system, and a straight line in the same parameter coordinate system corresponds to a point in the original coordinate system. Then, all vertices of a straight line are displayed in the original coordinate system. Their slope and intercept are the same, so they correspond to the same point in the parameter coordinate system. In this way, after each point in the original coordinate system is projected under the parameter coordinate system, check whether there are any clustering points under the parameter coordinate system. Such a clustering point corresponds to a straight line in the original coordinate system.
BelowCodeImplements the simplest straight line detection of the Hough transformation. The input is a two-value graph with width * Height (the background is 0 and the foreground is 255), which is stored in the matrix SRC, ithreshold is the domain value that is determined as a straight line. The output PR is the distance from the origin to the straight line, and PTH is the angle of the straight line.
Program code
Void Hough (byte * SRC, int width, int height, int * PR, int * PTH, int ithreshold)
{
Int * parray;
Int irmax = (INT) SQRT (width * width + height * Height) + 1;
Int ithmax = 361;
Int ith = 0;
Int IR;
Int IMAX =-1;
Int ithmaxindex =-1;
Int irmaxindex =-1;
Parray = new int [irmax * ithmax];
Memset (parray, 0, sizeof (INT) * irmax * ithmax );
Float Frate = (float) (PI/180 );
for (INT y = 0; y {< br> for (INT x = 0; x {< br> If (* src = 255)
{< br> for (ith = 0; ith {< br> ir = (INT) (x * Cos (ith * Frate) + y * sin (ith * Frate);
If (IR> 0)
{
Parray [Ir/1 * ithmax + ith] ++;
}
}
}
SRC ++;
} // X
} // Y
For (IR = 0; IR <irmax; IR ++)
{
For (ith = 0; ith <ithmax; ith ++)
{
Int icount = parray [ir * ithmax + ith];
If (icount> IMAX)
{
IMAX = icount;
Irmaxindex = IR;
Ithmaxindex = ith;
}
}
}
If (IMAX> = ithreshold)
{
* Pr = irmaxindex;
* PTH = ithmaxindex;
}
Delete [] parray;
Return;
} // End of Hough