Code for Straight Line Detection Using Hough Transformation

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.