Football field line detection

1.2k Views Asked by At

I wanted to create a model which can detect penalty area field lines. I managed to get desirable results for one specific image by changing some hyperparamaters, but the same code doesn't work for another image. i.e. the code is quite specific for an image. If I want to the code to work for every image what changes should I make.

One of the input images I took as input is here the output for the image is here One of the failure case which I got for an image while executing the code is here

this code is specific for the image provided above and is not working for some other image. Please let me know how can I improve my code.

import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("C:/Users/user/Desktop/football1.jpg")

low_threshold = np.array([0,90,0], dtype=np.uint8)
high_threshold = np.array([170,255,255], dtype=np.uint8)  


mask1 = cv2.inRange(img, low_threshold, high_threshold)
cv2.imshow("mask1",mask1)

l_threshold = np.array([0, 115, 0], dtype=np.uint8)
h_threshold = np.array([75, 147, 172], dtype=np.uint8)  

mask2=cv2.inRange(img, low_threshold , high_threshold)
cv2.imshow("mask2",mask2)
and1=cv2.bitwise_and(img,img,mask=mask1)
cv2.imshow("and1",and1)

and2=cv2.bitwise_and(and1,and1,mask=mask2)
cv2.imshow("and2",and2)

lowest=np.array([73, 145, 45], dtype=np.uint8)
highest=np.array([114, 255, 255], dtype=np.uint8)

mask3=cv2.inRange(and2, lowest , highest)
cv2.imshow("and3",mask3)
and3=cv2.bitwise_and(and2,and2,mask=mask3)
cv2.imshow("and3",and3)
gray=cv2.cvtColor(and3,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",gray)

gaussian=cv2.GaussianBlur(gray, (3, 3), 5)
cv2.imshow("gaussian",gaussian)

edge=cv2.Canny(gaussian, 10, 150)
cv2.imshow("edge",edge)

rho = 1

# 1 degree
theta = (np.pi/180) * 1
threshold = 15
min_line_length = 20
max_line_gap = 10

lines=cv2.HoughLinesP(edge, rho, theta, threshold, np.array([]),
                     minLineLength=min_line_length, maxLineGap=max_line_gap)
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(img, (x1, y1), (x2, y2), [0,255,0], 1)
        m=(y2-y1)/(x2-x1)
        print ("y="+str(round(m,2))+ "x+"+str(y2-y1))

cv2.imshow('output',img)

cv2.waitKey(0)
cv2.destroyAllWindows()
#cv2.imshow('output',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
0

There are 0 best solutions below