So I was originally going to use watershed and fumble my way through. I found a maze solver on git that I'm looking to understand better, and make work for a picamera image https://github.com/raincrash/image_processing/blob/master/maze_solver/maze_solver.py
when I use the image he supplies it works, but when I swap the image out for one that I took with the pi, all I get is a black screen when at the point it looks to find the contour.
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()
here's the image I'm using
Pi maze
what am I missing? shouldn't the findCountor
function look for the edges? are the edges too jagged for it to find?