Test the line detection function by using the Hough transform algorithm.

Source: Internet
Author: User

Label


Test the line detection function by using the Hough transform algorithm.

[Email protected]

Http://blog.csdn.net/kezunhai

Hof transform is one of the classic algorithms in image transformation. It is mainly used to separate geometric shapes (such as straight lines and circles) with the same features from images ). Compared with other methods, the HOF transform method for finding a straight line and a circle can better reduce noise interference. The basic principle of the Hough transformation is to convert the curve of the original image space to a point in the parameter space by means of the parity between the point and the line.


We can see that the x-y coordinates and K-B coordinates are a bit-the line parity. P1 and P2 in the x-y coordinate correspond to L1 and L2 in the K-B coordinate, while P0 in the K-B coordinate correspond to the linear l0 In the x-y coordinate. Considering that when the K value is infinite, the result of the computation is not changed, the shares are expressed in polar coordinates:

P = x * Cos (A) + y * sin ()

For a sine curve (P = C * sin (a + theta) in polar A-P, A is obtained (0-180 ° ).


In order to detect the straight line composed of points in the Cartesian coordinate X-Y, the polar coordinate A-P can be quantified into many small cells. Based on the coordinates (x, y) of each vertex in the Cartesian coordinate, the P values of each vertex are calculated with the length of the cell within a = 0-180 °, and the obtained values fall within the cell, add 1 to the corresponding accumulators. After all vertices in the Cartesian coordinates are transformed, the small cells are tested. The (A, P) value of the smallest cell corresponds to the straight line of the Cartesian coordinate. In this way, the detection problem of the given curve in the original image is converted to the peak value in the search parameter space, that is, the overall detection feature is converted to the local detection feature.

The opencv library comes with the houghlines linear detection function, which mainly implements the standard and probability-based Hough Transformation (and also implements multi-scale Hough Transformation ).

Int main () {iplimage * srcrgb = cvloadimage ("F: \ images \ chess1.jpg"); iplimage * src = cvcreateimage (cvsize (srcrgb-> width, srcrgb-> height), ipl_depth_8u, 1); cvcvtcolor (srcrgb, SRC, cv_rgb2gray); iplimage * DST = cvcreateimage (cvsize (SRC-> width, Src-> height ), ipl_depth_8u, Src-> nchannels); iplimage * color_dst = cvcloneimage (srcrgb); cvsetzero (color_dst); cvmemstorage * storage = cvcreatemstorage (); cvseq * lines = 0; Cvcvtcolor (SRC, DST, 180,150); cvcvtcolor (DST, color_dst, cv_gray2rgb); # If 1 lines = cvhoughlines2 (DST, storage, cv_hough_standard, 1, cv_pi/, 0, 0); For (INT I = 0; I <min (lines-> total, 100); I ++) {float * line = (float *) cvgetseqelem (lines, i); float ROV = line [0]; float threta = line [1]; cvpoint pt1, pt2; // the formula for calculating the position is described below. Double A = cos (threta), B = sin (threta); double X0 = A * rock, Y0 = B * rock; pt1.x = cvround (x0 + 1000 * (-B); pt1.y = cvround (y0 + 1000 * ()); pt2.x = cvround (x0-1000 * (-B); pt2.y = cvround (y0-1000 * (a); cvline (color_dst, pt1, pt2, cv_rgb (255, 0, 0), 180,100) ;}# elselines = cvhoughlines2 (DST, storage, cv_hough_probabilistic, 1, cv_pi/, 50, 10); For (INT I = 0; I <lines-> total; ++ I) {cvpoint * line = (cvpoint *) cvgetseqelem (lines, I); cvline (color_dst, line [0], line [1], cv_rgb (, 0),) ;}# endifcvshowimage ("Source", Src); cvshowimage ("Hough", color_dst ); cvshowimage ("DST", DST); cvwaitkey (0 );}
In addition to detecting straight lines, you can also detect circular, elliptical, rectangular, and other regular geometric figures. In addition, scholars have generalized the Hough Transform and proposed the Generalized HOUGH transform, which is also implemented in opencv. The implementation code is as follows:

void cv::GeneralizedHough::setTemplate(InputArray _templ, int cannyThreshold, Point templCenter){    Mat templ = _templ.getMat();    CV_Assert(templ.type() == CV_8UC1);    CV_Assert(cannyThreshold > 0);    Canny(templ, edges_, cannyThreshold / 2, cannyThreshold);    Sobel(templ, dx_, CV_32F, 1, 0);    Sobel(templ, dy_, CV_32F, 0, 1);    if (templCenter == Point(-1, -1))        templCenter = Point(templ.cols / 2, templ.rows / 2);    setTemplateImpl(edges_, dx_, dy_, templCenter);}void cv::GeneralizedHough::setTemplate(InputArray _edges, InputArray _dx, InputArray _dy, Point templCenter){    Mat edges = _edges.getMat();    Mat dx = _dx.getMat();    Mat dy = _dy.getMat();    if (templCenter == Point(-1, -1))        templCenter = Point(edges.cols / 2, edges.rows / 2);    setTemplateImpl(edges, dx, dy, templCenter);}void cv::GeneralizedHough::detect(InputArray _image, OutputArray positions, OutputArray votes, int cannyThreshold){    Mat image = _image.getMat();    CV_Assert(image.type() == CV_8UC1);    CV_Assert(cannyThreshold > 0);    Canny(image, edges_, cannyThreshold / 2, cannyThreshold);    Sobel(image, dx_, CV_32F, 1, 0);    Sobel(image, dy_, CV_32F, 0, 1);    detectImpl(edges_, dx_, dy_, positions, votes);}void cv::GeneralizedHough::detect(InputArray _edges, InputArray _dx, InputArray _dy, OutputArray positions, OutputArray votes){    cv::Mat edges = _edges.getMat();    cv::Mat dx = _dx.getMat();    cv::Mat dy = _dy.getMat();    detectImpl(edges, dx, dy, positions, votes);}
The algorithm can use a template to represent the geometric shape, so as to extract its shape. As a widely used algorithm for geometric shape extraction, hough transformation has also appeared some other geometric shape extraction algorithms in recent years. For example, line segment detector is used for line extraction).

References:

1. opencv convert Detection Line

2. http://rosettacode.org/wiki/Example:Hough_transform/C

Kezunhai Source: http://blog.csdn.net/kezunhai or sharding, but the documents must be published.

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.