找到了三种骨架提取方法,比对一下效果
import cv2
import skimage
from skimage import morphology
import matplotlib.pyplot as plt
import time
if __name__ == '__main__':
gray=cv2.imread("demo.png",0)#以灰度图像读取
ret, binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) #OTSU二值化
binary=255-binary
t0=time.time()
#方法一
thin1= morphology.medial_axis(binary)
t1 = time.time()
#方法二
thin2=skimage.morphology.thin(binary,max_iter=5)
t2 = time.time()
#方法三
thin3=Xihua(binary,array,num=5)
t3=time.time()
print(t1-t0,t2-t1,t3-t2)
fig, axes = plt.subplots(2, 2)
ax0, ax1, ax2, ax3= axes.ravel()
ax1.set_title("thin1")
ax2.set_title("thin2")
ax3.set_title("thin3")
ax0.imshow(binary, cmap="gray")
ax1.imshow(thin1, cmap="gray")
ax2.imshow(thin2, cmap="gray")
ax3.imshow(thin3, cmap="gray")
plt.show()
方法三的Xihua函数,来自一个大神的博客 OpenCV—python 图片细化(骨架提取)
结果:
时间:0.05282950401306152 0.007979393005371094 2.0426905155181885
我自己想要做的任务:
虽然说大神的方法速度慢,但是效果是真不戳。
方法一结果比较毛躁,方法二和大神的方法结果还是很顺滑的。