1 | initial version |
import cv2
import numpy as np
img = cv2.imread('maze.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,80,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True
cv2.imwrite('maze_skel.png', skel)
cv2.imshow("maze",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here is output
2 | No.2 Revision |
img = cv2.imread('maze.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,80,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True
cv2.imwrite('maze_skel.png', skel)
cv2.imshow("maze",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
Before input: Here is output
3 | No.3 Revision |
import cv2 import numpy as np
img = cv2.imread('maze.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,80,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True
cv2.imwrite('maze_skel.png', skel)
cv2.imshow("maze",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
Before input:
Here is