How to detect green lanes and border of the football field?

Viewed 2391

I am tring to somehow very accurate detect green lanes and border of the footbal field.

Here if my code and result but looks like I something missed and I guess result can be much better

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

kernel_size = 3

bilarImg = cv2.bilateralFilter(gray,7,7,7)
image_enhanced = cv2.equalizeHist(bilarImg)

plt.imshow(image_enhanced)

masked_edges = cv2.Canny(image_enhanced, 100, 170, apertureSize = 3)

plt.imshow(masked_edges, cmap='gray')

# Define the Hough transform parameters
# Make a blank the same size as our image to draw on
rho = 2 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 110     # minimum number of votes (intersections in Hough grid cell)
min_line_length = 90 #minimum number of pixels making up a line
max_line_gap = 20   # maximum gap in pixels between connectable line segments

line_image = np.copy(img)*0 #creating a blank to draw lines on

# Run Hough on edge detected image
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                            min_line_length, max_line_gap)

# Iterate over the output "lines" and draw lines on the blank
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,255,255),2)

# Create a "color" binary image to combine with line image
color_edges = np.dstack((masked_edges, masked_edges, masked_edges)) 

# Draw the lines on the edge image
combo = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) 

# remove small objects
se1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
combo = cv2.morphologyEx(combo, cv2.MORPH_OPEN, se1)

cv2.imwrite("gray.png", image_enhanced)
cv2.imwrite("res.png", combo)

Here is src image: src image

and results enter image description here enter image description here

Ideally I would keep only field border and vertical lines of green lanes.

4 Answers

You can follow these steps to get desired result:

1) Change RGB color image to some other domain like HSV or HSL.

2) Then segment out the region that has green color using the changed color domain image. e.g: for specific ranges of H, S and L you will be able to extract green color out of the image.

3)perform morphological closing operation just in case there is any discontinuity or the shape is not proper(in your case somewhat rectangle).

4)some random noises other than the green field might be present, you can remove those by doing contour detection and selecting only the contour that is largest(in your case the largest contour will be the field).

5) Now, once you have got the ROI (from above largest contour step), you can use this ROI in original image to detect straight lines using Hough Line transform or Line segment detector(LSD).

6) If there is breakage in one single line then you can go for line fitting and join the parts of one single line.

Note: For almost all the methods mentioned above, you will find equivalent functionalities in OpenCV.

It is realy difficult task. I think, you should find a mask for this field. The mask help you identify field withound peoples and noise (as an option to use segmentation by color). As you said you can you Hough method to determined lines. You must play around with parameters for the best result. In sum you can find the equation of each lines and compute tan(a). With tan(a) you can identify horizontal and vertical lines of the football field.

I recommend to split color space into RGB to Green space. You can use Split function.

Related