首页 > 解决方案 > 使用计算机视觉实时检测网格块(Open CV、Python)

问题描述

单击此处查看网格图像以供参考

我想检测以下网格中各个块的质心以进行路径规划。这个想法是,像头顶摄像头这样的中央导航系统将与机器人一起检测网格块并帮助导航。到目前为止,我已经尝试过霍夫线概率哈里斯角检测,但它们要么检测到额外的点,要么在现实世界的场景中失败。我想实时检测块并对它们进行编号。那些编号不应该改变,否则整个路径规划将一团糟。

有没有我错过的这个问题的解决方案。

提前致谢

标签: pythonalgorithmopencvgrid

解决方案


您需要学习如何消除噪音。这不是一个完整的答案。您花费和学习的时间越多,您的结果就会越好。

import cv2
import numpy as np
import sys

# Load source as grayscale
im = cv2.imread(sys.path[0]+'/im.jpg', cv2.IMREAD_GRAYSCALE)
H, W = im.shape[:2]

# Convert im to black and white
im = cv2.adaptiveThreshold(
    im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 2)

# Remove noise
im = cv2.medianBlur(im, 11)
im = cv2.erode(im, np.ones((15, 15)))

# Fill the area around the shape
im = ~im
mask = np.zeros((H+2, W+2), np.uint8)
cv2.floodFill(im, mask, (0, 0), 255)
cv2.floodFill(im, mask, (W-1, 0), 255)
cv2.floodFill(im, mask, (0, H-1), 255)
cv2.floodFill(im, mask, (W-1, H-1), 255)

# Remove noise again
im = cv2.dilate(im, np.ones((15, 15)))

# Find the final blocks
cnts, _ = cv2.findContours(~im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    cv2.circle(im, (x+w//2, y+h//2), max(w, h)//2, 127, 5)
print("Found any: ", len(cnts) > 0)

# Save the output
cv2.imwrite(sys.path[0]+'/im_.jpg', im)

在此处输入图像描述


推荐阅读