You need to reverse white & black, and fill all the holes by call
cv2.dilate
first:
import numpy as npimport cv2img = cv2.imread("e_5.jpg",0)size = np.size(img)skel = np.zeros(img.shape,np.uint8)ret,img = cv2.threshold(img,127,255,0)element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))img = 255 - imgimg = cv2.dilate(img, element, iterations=3)done = Falsewhile( 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
Here is the result:
But, the result is not good, because there are many gaps. The following
algorithm is better, it uses functions in
scipy.ndimage.morphology:
import scipy.ndimage.morphology as mimport numpy as npimport cv2def skeletonize(img): h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]]) m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]]) h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]]) m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) img = img.copy() while True: last = img for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(img, hit, miss) img = np.logical_and(img, np.logical_not(hm)) if np.all(img == last): break return imgimg = cv2.imread("e_5.jpg",0)ret,img = cv2.threshold(img,127,255,0)element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))img = 255 - imgimg = cv2.dilate(img, element, iterations=3)skel = skeletonize(img)imshow(skel, cmap="gray", interpolation="nearest")
The result is:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)