所以, 咨询了算法的同学, 使用图像学中的骨架化,可以解决这个问题。
代码
点击(此处)折叠或打开
- import numpy as np
-
- im=cv2.imread('1.png',cv2.IMREAD_GRAYSCALE)
- thresh,im=cv2.threshold(im,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
- cv2.imshow('binary.png',im)#控制背景为黑色
- dst = im.copy()
-
- num_erode = 0
-
- while(True):
- if np.sum(dst)==0:
- break
- kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
- dst = cv2.erode(dst,kernel)
- num_erode = num_erode + 1
-
- skeleton = np.zeros(dst.shape,np.uint8)
-
- for x in range(num_erode):
- kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
- dst = cv2.erode(im,kernel,None,None,x)
- open_dst = cv2.morphologyEx(dst,cv2.MORPH_OPEN,kernel)
- result = dst - open_dst
- skeleton = skeleton + result
- cv2.waitKey(1000)
-
-
- cv2.imshow('result',skeleton)
-
- cv2.waitKey(0)
- cv2.destroyAllWindows()
用rect作为结构元
OK, 看图示的效果应该是可以达到我的要求, 后面有时间找个地图实测一下。