How to do it...

OpenCV offers two implementations of the Hough transform for line detection. The basic version is cv::HoughLines. Its input is a binary map that contains a set of points (represented by nonzero pixels), some of which are aligned to form lines. Usually, this is an edge map obtained, for example, from the Canny operator. The output of the cv::HoughLines function is a vector of the cv::Vec2f elements, each of them being a pair of floating point values, which represents the parameters of a detected line, (ρ, θ). The following is an example of using this function:

  1. First, apply the Canny operator to obtain the image contours:
// Apply Canny algorithm 
cv::Mat contours; 
cv::Canny(image,contours,125,350);
  1. Then, detect the lines ...

Get OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.