Hough Transform Detection Line

Source: Internet
Author: User

After the image edge detection and binary processing is black bottom, white line. Set each white pixel in the picture to (Xi,Yi). Theorem: For each point in a Cartesian coordinate system, there is a curve ρ=xicosθ+yisinθ corresponding to the polar coordinates. make θ equal to 0, θ,2θ, respectively. π, we can find the corresponding ρ value. The ρ is also divided into a number of small ρ, so that the polar coordinates are divided into many (ρ, θ) as a unit of small pieces. Calculates the number of times each curve falls in each small segment.      After all the data points have been transformed, the small units are detected, so that the number of units that fall into more times indicates that this point is a common point for more curves, andthat the points on the (x, y) plane corresponding to these curvescan be considered collinear. Actually, I just needed the θ value in the most intersecting intersection coordinates (ρ,θ) in polar coordinates ...

My own understanding: The original linear equation is: y=kx+b; now turn it into the form of polar coordinates: ρ =xicos θ +yisin θ. Note that x, Y is the pixel point in the direct coordinates. Then θ and ρ piecewise discretization values, θ value corresponds to different ρ, so in a Cartesian coordinate system in a value (x, Y) corresponds to a curve in polar coordinates, in fact, note ρ,θ is equivalent to a Cartesian coordinate system in the K,b,ρ =xicos θ +yisin θ. is to calculate the ρ,θ by the principle of fast computing speed. It's not finished, A value in the Cartesian coordinate system (x, Y) corresponds to a curve in polar coordinates. So, the pixel point one by one in the straight line in the Cartesian coordinate system corresponds to a polar curve, and they intersect at a point. The corresponding (ρ,θ) is the K and b we need. Of course, the specific details also need to check the source code, the number of votes ah, the specific segmentation ρ,θ. The number of votes is the same point in the (ρ,θ) plane intersection, when it is greater than a certain threshold is a straight line out of (ρ, θ) is the corresponding linear parameter coefficient


The curve in the figure is the curve (50,30) corresponding to the (ρ,θ) plane



The curve in the graph is (50,30), (30,10) 2 curves intersect, when the direct coordinate system in the straight line pixel one by one map to the (ρ,θ) plane, should all intersect in the same (ρ,θ), when a threshold is reached, it can be explained.


Here is the Hough detection line step in the paper:



Here is the code on the book:

< Span style= "font-family: Arial" >

Linefinder.h#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/core/ Core.hpp "#include <iostream>//#include <math.h>//#include <cmath>using namespace std;using namespace CV; #define PI 3.141592class Linefinder{private:mat img;vector<vec4i>lines;double deltarho;double Deltatheta;int minvote;double minlength;double maxgap;public:linefinder ():d Eltarho (1), DeltaTheta (PI/180), MinVote ( , MinLength (0.), Maxgap (0.) {}void setaccresolution (double drho,double dtheta) {Deltarho=drho;deltatheta=dtheta;} void setminvote (int minv) {MINVOTE=MINV;} void Setlinelengandgap (double length,double gap) {minlength=length;maxgap=gap;} Vector<vec4i> findlines (Mat &binary) {lines.clear (); Houghlinesp (binary,lines,deltarho,deltatheta,minvote,minlength,maxgap); return lines;} void Drawdetectedlines (Mat &image,scalar color=scalar (255,255,255)) {Vector<vec4i>::const_iterator it2= Lines.begin (); while (It2!=lines.end ()) {Point pt1 ((*IT2) [0], (*it2) [1]); Point Pt2 ((*IT2) [2], (*IT2) [3]); line (image,pt1,pt2,color); ++it2;}};
Main#include "LineFinder.h" void Main () {Mat img=imread ("c:\\users\\administrator\\desktop\\ work \\testp\\road.jpg", 0);   Mat out1; Canny (img,out1,125,350); Mat img1;img1=img.clone ();vector<vec2f> lines; Houghlines (out1,lines,1,pi/180,60); Vector<vec2f>::const_iterator It=lines.begin (); while (It!=lines.end ()) { Float rho= (*it) [0];float theta= (*it) [1];if (theta<pi/4.| | Theta>3.*pi/4.) {Point pt1 (Rho/cos (theta), 0); Point Pt2 ((Rho-out1.rows*sin (theta))/cos (theta), out1.rows); line (Img1,pt1,pt2,scalar (255), 1);} Else{point pt1 (0,rho/sin (theta)); Point Pt2 (Out1.cols, (Rho-out1.cols*cos (theta))/sin (theta)); line (Img1,pt1,pt2,scalar (255), 1);} ++it;} Linefinder Finder;finder.setlinelengandgap (100,20); finder.setminvote; Finder.findlines (OUT1); Finder.drawdetectedlines (IMG); Imshow ("HOUGHLINEP", IMG), Imshow ("original", IMG1); Imshow ("Out", OUT1); Waitkey (0);}




Detection Circle::

#include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace Cv;void Main () {Mat img=imread ("c:\\users\\administrator\\desktop\\ work \\testp\\chariot.jpg", 0); Mat IMG1; Gaussianblur (Img,img1,size (5,5), 1.5);vector<vec3f>circles; Houghcircles (img1,circles,cv_hough_gradient,2,50,200,100,25,100); Vector<vec3f>::const_iterator itc= Circles.begin (); while (Itc!=circles.end ()) {Circle (Img1,point (*ITC) [0], (*ITC) [1]), (*ITC) [2],scalar (255), 5); + + ITC;} Imshow ("Original", IMG), Imshow ("Img1", IMG1); Waitkey (0);}


Line fitting for a point set:

#include "LineFinder.h" void Main () {Mat img=imread ("c:\\users\\administrator\\desktop\\ work \\testp\\road.jpg", 0); Mat out1; Canny (img,out1,125,350); Mat img1;img1=img.clone ();vector<vec2f> lines; Houghlines (out1,lines,1,pi/180,60); Vector<vec2f>::const_iterator It=lines.begin (); while (It!=lines.end ()) { Float rho= (*it) [0];float theta= (*it) [1];if (theta<pi/4.| | Theta>3.*pi/4.) {Point pt1 (Rho/cos (theta), 0); Point Pt2 ((Rho-out1.rows*sin (theta))/cos (theta), out1.rows); line (Img1,pt1,pt2,scalar (255), 1);} Else{point pt1 (0,rho/sin (theta)); Point Pt2 (Out1.cols, (Rho-out1.cols*cos (theta))/sin (theta)); line (Img1,pt1,pt2,scalar (255), 1);} ++it;} Linefinder Finder;finder.setlinelengandgap (100,20); finder.setminvote; Finder.findlines (OUT1); Finder.drawdetectedlines (IMG); vector<vec4i>lines111=finder.findlines (OUT1); int n=0; Mat oneline (Out1.size (), Cv_8u,scalar (0)), line (Oneline,point (lines111[n][0],lines111[n][1)), point (Lines111[n][2] , Lines111[n][3]), Scalar (255), 3); Bitwise_and (Out1,oneline,oneliNE);//threshold (ONELINE,ONELINE,100,255,THRESH_BINARY_INV); vector<point>points;for (int y=0;y< oneline.rows;y++) {Uchar *rowptr=oneline.ptr<uchar> (y); for (int x=0;x<oneline.cols;x++) {if (Rowptr[x]) { Points.push_back (Point (x, Y));}}} vec4f Line12;fitline (points,line12,cv_dist_l2,0,0.01,0.01); int X0=line12[2];int Y0=line12[3];int x1=x0+100*line12[ 0];int Y1=y0+100*line12[1];line (Img,point (x0,y0), point (X1,y1), Scalar (0), 3), Imshow ("src", img), Imshow ("Oneline", Oneline); Waitkey (0);



Hough Transform Detection Line

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.