python - 使用计算机视觉实时检测网格块(Open CV、Python)
问题描述
我想检测以下网格中各个块的质心以进行路径规划。这个想法是,像头顶摄像头这样的中央导航系统将与机器人一起检测网格块并帮助导航。到目前为止,我已经尝试过霍夫线概率和哈里斯角检测,但它们要么检测到额外的点,要么在现实世界的场景中失败。我想实时检测块并对它们进行编号。那些编号不应该改变,否则整个路径规划将一团糟。
有没有我错过的这个问题的解决方案。
提前致谢
解决方案
您需要学习如何消除噪音。这不是一个完整的答案。您花费和学习的时间越多,您的结果就会越好。
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)
推荐阅读
- objective-c - 在 NSString 中间包含一个变量
- amazon-web-services - Amazon CloudSearch:空闲时间后的第一次搜索很慢
- python - Python按钮超时
- c# - 为 Odata 使用不同的数据源
- php - 在 PHP 中从 html 中获取文本
- angular - 角度 5 在 POST 请求中出现错误。发送标头参数不起作用
- python - OOP 和语法 - Ex 43 - 艰难地学习 Python
- azure - 如何在函数设置为只读时创建 Azure 函数、函数键
- android - 删除 EditText 中的选定文本
- c# - 动作脚本到 C#(UNITY)