Hi I’m quite new in OpenCV, I am currently trying to detect horizontal lines in an image using HoughLinesP function in opencv, in order to calculate the ratio between x and y.
The line detection result is shown in the second picture.
- How to exclude the vertical lines detected?
- The lines detected by HoughLinesP seems not a single line. Is there a better way to detect the edge as a single line instead of multiple lines merged together?
- How should I proceed to the next step of getting the ratio between x and y?
Thank you in advance!
original image
result image
import cv2
import numpy as np
# read image
img = cv2.imread('testtube.png')
# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# apply canny edge detection
edges = cv2.Canny(gray, 90, 100,apertureSize=3)
# get hough lines
result = img.copy()
lines = cv2.HoughLinesP(edges, rho=1,theta=np.pi/2, threshold=30,lines=np.array([]), minLineLength=30,maxLineGap=20)
a,b,c = lines.shape
for i in range(a):
cv2.line(result, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("edges", edges)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
New contributor
user25443827 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.