我想创建一个可以检测罚球区场线的模型。通过更改一些超参数,我设法为一个特定图像获得了理想的结果,但是相同的代码不适用于另一个图像。即代码对于图像是非常具体的。如果我想让代码适用于每个图像,我应该进行哪些更改。
我作为输入的输入图像之一是这里 图像的输出是这里 我在执行代码时得到的图像失败案例之一是这里
此代码特定于上面提供的图像,不适用于其他图像。请让我知道如何改进我的代码。
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()