使用计算机视觉实时检测网格块(Open CV、Python)

2024-06-12 07:40:17 发布

您现在位置:Python中文网/ 问答频道 /正文

Click here to see grid image for reference

我想检测以下网格中单个块的质心,以进行路径规划。其想法是,像头顶摄像头这样的中央导航系统将与机器人一起检测网格块,并帮助导航。到目前为止,我已经尝试了Hough线概率Harris角点检测,但这两种方法要么检测到额外的点,要么在现实场景中失败。我想实时检测这些块并给它们编号。这些编号不应更改,否则整个路径规划将混乱

我错过了这个问题的任何解决方案

提前谢谢


Tags: 方法路径网格机器人概率导航系统编号规划
1条回答
网友
1楼 · 发布于 2024-06-12 07:40:17

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

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)

enter image description here

相关问题 更多 >