【问题标题】:How to detect lines in a football field using OpenCV如何使用 OpenCV 检测足球场中的线条
【发布时间】:2021-03-11 21:29:48
【问题描述】:

我正在尝试检测足球场视频中的线条,但不幸的是,我无法让它与我的图片一起使用。

我正在使用 Canny 检测边缘,然后使用霍夫线变换来获取线条,我想我找不到适合我使用的参数。

我尝试添加单应性(关闭)来平滑边缘检测,调整图片大小以提高检测精度,调整白平衡,添加高斯模糊,但对于 Canny 边缘检测,我找不到比这更清晰的东西了:

霍夫线变换比这更好:

imgsrc = cv2.imread("image.png")

# Resize to improve detection accuracy
t = int(img.shape[1] * 1.6)
img = imutils.resize(imgsrc, width=t)

# Apply gaussian blur
kernel_size = 3
img = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

# Convert to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Initialize morph-kernel, apply CLOSE before Canny to improve edges detection
kernel0 = np.ones((9,27), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel0)

# Detect edges
low_threshold = 5
high_threshold = 50
edges = cv2.Canny(img, low_threshold, high_threshold)

# Initialize morph-kerne, apply CLOSE after Canny to merge edges
kernel2 = np.ones((8,24), np.uint8)
edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel2)

# Hough Lines params
rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
# minimum number of votes (intersections in Hough grid cell)
threshold = 30
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 40  # maximum gap in pixels between connectable line segments

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

output = np.copy(imgsrc) * 0  # creating a blank to draw lines on
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(output, (int(x1), int(y1)), (int(x2), int(y2)), (255, 255, 255), thickness)

你知道如何改进我的线检测,最后只得到几行吗?

【问题讨论】:

  • 我不认为形态函数可用于此任务。我的建议是你应该使用高级算法(yolo、rcnn 等)
  • 感谢您的帮助,不过我们被要求先提供一个更简单的图像处理算法,不涉及机器学习...
  • 既然你有一张彩色图像,你有没有想过将你的颜色分成 2 或 3 类而不是使用边缘检测?可能有助于创造一个基本的区别,请参阅:docs.opencv.org/master/d1/d5c/tutorial_py_kmeans_opencv.html

标签: python opencv image-processing hough-transform


【解决方案1】:

这是 Python 中的解决方案代码:

import cv2
from matplotlib import pyplot as plt
import numpy

im = cv2.imread("foot.png")
B = im[:,:,2]
Y = 255-B

thresh = cv2.adaptiveThreshold(Y,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY_INV,35,5)

contours, hierarchy = cv2.findContours(thresh,  
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 

x=[]
for i in range(0, len(contours)):
    if cv2.contourArea(contours[i]) > 100:
        x.append(contours[i])
cv2.drawContours(im, x, -1, (255,0,0), 2) 

plt.imshow(im)

输出:

玩弄参数以获得期望的结果。

【讨论】:

  • 非常感谢,我忘记了自适应阈值,这正是我要找的。碰巧,您知道如何避免检测外场轮廓吗?
  • 您可以尝试根据“轮廓长度”进行消除。
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
相关资源
最近更新 更多