0

tennis court

How can I detect the white lines alone from the above image using opencv python? which method can I use? or is there any inbuilt function available for this purpose?

Hough's didn't work well on this case. The final output should like these: lane enter image description here

2
  • It would be helpful if you clarify in what manner you want to detect the lines and what exactly do you mean by 'detect'? Do you want an image with white lines only, or do you need to describe their position mathematically? Do you want to extract them because they are lines or because they are white? There are e.g. also black lines... Commented Jan 16, 2018 at 16:58
  • Edited and added expected output. Commented Jan 16, 2018 at 17:09

1 Answer 1

5

It seems like Hough's does actually do a good job. I took the example shown here and introduced only a small modification for the extraction of edges:

Load image and convert to grayscale:

import numpy as np
import cv2
import scipy.ndimage as ndi

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

Then I applied a median filter to get rid of some high frequency edges/noise like the net in the image:

smooth = ndi.filters.median_filter(gray, size=2)

Result looks like this: enter image description here

Then I applied a simple threshold to extract the white lines. Alternatively, you could apply e.g. a Laplace filter to extract edges, however, simple thresholding will neglect e.g. the horizontal line at the back of the tennis court:

edges = smooth > 180

Result: enter image description here

Then perform the Hough line transform similar to this example.

lines = cv2.HoughLines(edges.astype(np.uint8), 0.5, np.pi/180, 120)

for rho,theta in lines[0]:
    print(rho, theta)
    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))
    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

# Show the result
cv2.imshow("Line Detection", img)

You can play around with the accuracies and modify the result. I ended up choosing the parameters 0.5 and 120. Overall result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

You need a nested loop to print all the lines, the output won't show up as it shows or at least mention that the code shows how to show only single line and rest can be looped over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.