我试图以某种方式非常准确地检测足球场的绿色车道和边界。
在这里,如果我的代码和结果但看起来我遗漏了什么,我想结果可能会好得多
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)
这是 src 图片:
和结果
理想情况下,我只会保留 field 边界和绿色车道的垂直线。
最佳答案
这真的是一项艰巨的任务。我认为,你应该为这个领域找到一个掩码。 mask 可帮助您识别没有人声和噪音的字段(作为使用颜色分割的选项)。正如你所说,你可以用霍夫方法来确定线条。您必须使用参数以获得最佳结果。总之,您可以找到每条线的方程并计算 tan(a)。使用 tan(a),您可以识别足球场的水平线和垂直线。
关于python - 如何检测足球场的绿道和边界?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52017963/