Is it possible to converse the output of a Houghlines function to one of the HoughlinesP function?

41 Views Asked by At

I have an image and I want to get the red grid out of this image. I tried already with the HoughlinesP function and this works (see enter image description here), but right now i want to do this with the HoughLines function, because this is faster. The problem is that the output of this one is not a [x1,y1,x2,y2], but it is [rho, theta].

I already tried to converse it like this:

line_coordinates = []
for line in lines:
    for rho,theta in line:
        x1 = int(rho * math.cos(theta))
        y1 = int(rho * math.sin(theta))
        x2 = int(x1 + 1000 * (-math.sin(theta)))  
        y2 = int(y1 + 1000 * (math.cos(theta)))
        line_coordinates.append([x1, y1, x2, y2])

    cv2.line(imgOriginal, (x1, y1), (x2, y2), (0, 0, 255), 2)

But if i do this, then i get something like this: enter image description here, how can i make this conversion better?

I am using this as functions:

imgOriginal = cv2.imread(path, cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2HSV)

mask_red = cv2.inRange(hsv, np.array([150, 25, 150]), np.array([180, 255, 255]))
mask_red_extra = cv2.inRange(hsv, np.array([1, 25, 150]), np.array([40, 255, 255]))
red_mask = mask_red + mask_red_extra
cv2.imshow('edges', red_mask)
threshold = 150
lines = cv2.HoughLines(red_mask, 1, np.pi/180, threshold)

hough_lines = cv2.HoughLinesP(red_mask, 1, np.pi / 180, threshold=50, minLineLength=75, maxLineGap=300)

and right here the original picture: enter image description here

1

There are 1 best solutions below

0
toyota Supra On

No. Cannot output of a Houghlines function to one of the HoughlinesP function?

Use HoughLines instead of HoughLinesP

Snippet:

import cv2
import numpy as np

img = cv2.imread('l.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray, 50, 150, apertureSize=3)

lines = cv2.HoughLines(edges, 1, np.pi/180, 200)

for r_theta in lines:
    arr = np.array(r_theta[0], dtype=np.float64)
    r, theta = arr
    # Stores the value of cos(theta) in a
    a = np.cos(theta)

    b = np.sin(theta)

    x0 = a*r

    y0 = b*r

    x1 = int(x0 + 1000*(-b))

    y1 = int(y0 + 1000*(a))

    x2 = int(x0 - 1000*(-b))

    y2 = int(y0 - 1000*(a))

    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

cv2.imshow('linesDetected', img)
cv2.waitKey(0)

Screenshot:

enter image description here