How to only plot length of Houghline

Viewed 21

I'm trying to find a way to filter the hough lines obtained in order to only get the shape of a tennis court. To begin, I'd like to simply visualize the hough lines only for the length of the line which was found, not for the whole image. What's the best way to do this?

# import the necessary packages
import numpy as np
import cv2

# load the image
#img = cv2.imread("assets/game-chunks/game-2/chunk_20/frame_294.jpg")
img = cv2.imread("assets/chunk_0/frame_1173.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,100,apertureSize = 3)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_white = np.array([0,0,210], dtype=np.uint8)
upper_white = np.array([255,255,255], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_white, upper_white)
lines = cv2.HoughLines(mask, 0.2, np.pi/180, 50, 40)

for line in lines:
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))
    if theta > 0.5 and theta < 2: 
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)



#cv2.imwrite('houghlines3.jpg',img)
cv2.imshow("Detected Lines (in red) - Standard Hough Line Transform", img)
cv2.waitKey(0)
0 Answers
Related