Detect a straight line using the Hough Transform

Source: Internet
Author: User

/*
function: detects a straight line by using the Hough transform
parameter description: imgbinaryin, indicating a binary image
width, indicates the image width
height, indicating the image height
houghbuf, indicating the buffer pointer required by the Hough Transformation
houghwidth,
houghheight indicates the width of the buffer required by the transformation.
radiusresolution indicates the height of the buffer required by the transformation.
angleresolution indicates the detection resolution of the polar radius of the transformation.
angleresolution, indicates the angle resolution of the Hough transformation.
radius indicates that the polar radius of the longest straight line detected by the Hough transformation is returned.
angle, returns the angle of the longest straight line detected by the Hough transformation.
*/
void imagealgorithm: houghtransform (unsigned char * imgbinaryin, int width, int height, int * houghbuf, int houghwidth, int houghheight, float radiusresolution, float angleresolution, float * radius, float * angle)
{< br> int I, J;
for (I = 0; I {< br> for (j = 0; j {< br> * (houghbuf + I * houghwidth + J) = 0;
}< BR >}

int R, A;
float tempr, Tempa;
// traverse image data
for (I = 0; I {< br> for (j = 0; j {< br> If (* (imgbinaryin + I * width + J) = 1)
{< br> // A indicates the circular variable of the angle,
for (a = 0; A {< br> // obtain the angle based on the resolution of the given angle of conversion
Tempa = (a-houghheight/2) * angleresolution;
// calculate the corresponding polar coordinate based on the angle of the current traversal and the X and Y values.
tempr = (J-width/2) * Cos (Tempa * 2 * PI/360) + (I-height/2) * sin (Tempa * 2 * PI/360);
r = tempr/radiusresolution;
// accumulate array
* (houghbuf + A * houghwidth + R + houghwidth/2) + = 1;
}< BR >}

// Calculate the maximum value of the accumulated array and record the array coordinates at this time
Int Max, maxr, maxa;
Max = * (houghbuf + 0 * houghwidth + 0 );
Maxr = 0;
Maxa = 0;
For (A = 0; A {
For (r = 0; r {
If (max <= * (houghbuf + A * houghwidth + r ))
{
Max = * (houghbuf + A * houghwidth + r );
Maxr = R;
Maxa =;
}
}
}

// Convert the maximum position to the polar radius and angle, and return the result through the Parameter
* Radius = (maxr-houghwidth/2) * radiusresolution;
* Angle = (maxa-houghheight/2) * angleresolution;

}

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.