I wish to ask if there is a method to detect only the nearly horizontal Hough Lines or ignoring those nearly vertical Hough Lines? My code for the HoughLine now is as shown:
HoughLinesP(imagec, lines, 80, CV_PI/2, CV_THRESH_OTSU|CV_THRESH_BINARY, 25, 7);
for(size_t i = 0; i < lines.size(); i++)
{
Vec4i l = lines[i];
line(imagec, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,255,255), 8, 8);
line(imagelines, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,255,255), 8, 8); //draw on black image as well
}
However in the image, I am seeking only to detect horizontal lines, or at least near horizontal lines like by 2 to 4 cm. I am using CV_PI/2
for the theta parameter in the HoughLineP, hence the vertical lines/ near vertical lines are detected too.
Any suggestions, code samples, etc will be greatly appreciated. Thanks.
One approach is to rotate the image 90 deg, then apply your same code, then rotate back 90 deg. Another approach is to change your horizontal kernel to a vertical kernel in your morphology.
The Hough transform takes a binary edge map as input and attempts to locate edges placed as straight lines. The idea of the Hough transform is, that every edge point in the edge map is transformed to all possible lines that could pass through that point.
First of all, let's detect some lines, which is done with the HoughLines and HoughLinesP functions. The only difference between the two functions is that one uses the standard Hough transform, and the second uses the probabilistic Hough transform (hence P in the name).
HoughLines(). It simply returns an array of ( (\rho, \theta) values. \rho is measured in pixels and \theta is measured in radians. First parameter, Input image should be a binary image, so apply threshold or use canny edge detection before applying hough transform.
Python version
import numpy as np
angle = np.arctan2(y2 - y1, x2 - x1) * 180. / np.pi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With