pythonopencvcomputer-visionhoughlinesp

Python OpenCV HoughLinesP Inaccurate Line Detection


Consider three points A, B, C in an image.

enter image description here

Below are their coordinates in the image of size 300x300.

enter image description here

I am trying to detect and draw a line connecting through these three points using below HoughLinesP code.

import cv2
import numpy as np

img = cv2.imread('test.png')
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Convert img to grayscale 
lines = cv2.HoughLinesP(img, rho=1, theta=np.pi/180, threshold=1, minLineLength=5, maxLineGap=10)
print(lines)

for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), 255, 1)
cv2.imshow("result", img)

But it detects a line that passes through only B and C. Why is it so?

Output:
[[[110 100 120 100]]]

enter image description here


Solution

  • lines = cv2.HoughLinesP(img, rho=0.1, theta=np.pi/180 * 0.1, threshold=2, minLineLength=5, maxLineGap=10)
    

    enter image description here