【发布时间】: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