Line Detection by Probabilistic Hough Line Transform

void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0 )
  • image – 8-bit, single-channel binary source image. The image may be modified by the function.
  • lines – Output vector of lines. Each line is represented by a 4-element vector (x1,y1,x2,y2) , where (x1,y1) and (x2,y2) are the ending points of each detected line segment.
  • rho – Distance resolution of the accumulator in pixels.
  • theta – Angle resolution of the accumulator in radians.
  • threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes ( >threshold ).
  • minLineLength – Minimum line length. Line segments shorter than that are rejected.
  • maxLineGap – Maximum allowed gap between points on the same line to link them.
A good example for Probabilistic Hough Line Transform is provided in OpenCV Documentation.


  1. Load image and convert to gray-scale.
  2. Apply the Probabilistic Hough Transform to find the lines.(HoughLinesP)
  3. Draw the detected lines.(line)
  4. Show the result



#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
    Mat src = imread("image1.jpg", 0);

    Mat dst, cdst;
    Canny(src, dst, 50, 200, 3); 
    cvtColor(dst, cdst, CV_GRAY2BGR); 

    vector<Vec4i> lines;
    // detect the lines
    HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
    for( size_t i = 0; i < lines.size(); i++ )
        Vec4i l = lines[i];
        // draw the lines
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);

    imshow("source", src);
    imshow("detected lines", cdst);


    return 0;

1 comment:

  1. Is there any function in openCV through which we can detect lines based on the width of the line??