0

所以我原本打算使用分水岭并摸索通过。我在 git 上找到了一个迷宫求解器,我希望更好地理解它,并为 picamera 图像工作 https://github.com/raincrash/image_processing/blob/master/maze_solver/maze_solver.py

当我使用他提供的图像时,它可以工作,但是当我将图像换成我用 pi 拍摄的图像时,我得到的只是一个黑屏,当它看起来要找到轮廓时。

import cv2
import numpy as np

i = cv2.imread('img.jpg')

cv2.imshow("Original", i)
cv2.waitKey(0)

# Convert to binary
# Find the contour, draw
# Make a mask and Dilate
# Then, Erode
# Find the difference
# Get the new mask (Solution)
# Merge the images and show

gray_scale = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray_scale, 127, 255, cv2.THRESH_BINARY_INV)

cv2.imshow("first thresh", thresh)
cv2.waitKey(0)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(thresh, contours, 0, (255, 255, 255), -1)
ret, thresh = cv2.threshold(thresh, 240, 255, cv2.THRESH_BINARY)

cv2.imshow("thresh", thresh)
cv2.waitKey(0)

dilate_mask = np.ones((19, 19), np.uint8)
dilate_result = cv2.dilate(thresh, dilate_mask, iterations = 1)

cv2.imshow("dilate", dilate_result)
cv2.waitKey(0)

erosion_result = cv2.erode(dilate_result, dilate_mask, iterations = 1)

cv2.imshow("erosion", erosion_result)
cv2.waitKey(0)

difference = cv2.absdiff(dilate_result, erosion_result)

cv2.imshow("difference", difference)
cv2.waitKey(0)

b, g, r = cv2.split(i)
mask = difference
result_mask_inverse = cv2.bitwise_not(difference)
r = cv2.bitwise_and(r, r, mask = result_mask_inverse)
g = cv2.bitwise_and(g, g, mask = result_mask_inverse)


result = cv2.merge((b, g, r))
cv2.imshow('solved maze', result)

cv2.waitKey(0)
cv2.destoyAllWindows()

这是我正在使用的图像

Pi迷宫

我错过了什么?函数不应该findCountor寻找边缘吗?边缘是否过于锯齿而无法找到?

4

0 回答 0