1 回答
TA贡献1795条经验 获得超7个赞
您的代码很好,但您可能需要更改将图像转换为二进制的方式。此外,为了避免看起来嘈杂的输出,您可以应用binary_closing到您的骨架图像。看看下面的代码——
import matplotlib.pyplot as plt
from skimage import img_as_bool
from skimage.io import imread
from skimage.color import rgb2gray
from skimage.morphology import skeletonize, binary_closing
im = img_as_bool(rgb2gray(imread('0jQjL.jpg')))
out = binary_closing(skeletonize(im))
f, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(im, cmap='gray', interpolation='nearest')
ax1.imshow(out, cmap='gray', interpolation='nearest')
plt.show()
您的两个示例图像给了我以下输出 -
编辑:为避免将图像转换为 bool 时的精度损失,您还可以使用可用的阈值算法之一对图像进行二值化。我更喜欢大津的。
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.filters import threshold_otsu
from skimage.color import rgb2gray
from skimage.morphology import skeletonize, binary_closing
def get_binary(img):
thresh = threshold_otsu(img)
binary = img > thresh
return binary
im = get_binary(rgb2gray(imread('Snip20190410_9.png')))
out = binary_closing(skeletonize(im))
f, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(im, cmap='gray', interpolation='nearest')
ax1.imshow(out, cmap='gray', interpolation='nearest')
plt.show()
添加回答
举报