python - 如何从图像中提取平滑的骨架

标签 python image-processing scikit-image

我有一些固定大小的字体字符图像,如输入图像示例所示。我想提取角色骨架(单像素宽)。我尝试了如下所示的各种方法,但输出都不同并且不平滑。我认为一像素宽的骨架将是平滑的(像素不会破裂并且没有噪声像素)。有一个更好的方法吗?如果没有的话,这三者中哪一个最好?

输入图像样本

enter image description here

1) 示例

from skimage import img_as_bool, io, color, morphology
import matplotlib.pyplot as plt

image = img_as_bool(color.rgb2gray(io.imread('image.jpeg')))
out = morphology.medial_axis(image)

f, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(image, cmap='gray', interpolation='nearest')
ax1.imshow(out, cmap='gray', interpolation='nearest')
plt.show()

输出1

enter image description here

2) 示例

from PIL import Image, ImageDraw, ImageFont
import mahotas as mh
import numpy as np

image = Image.new("RGBA", (600,150), (255,255,255))
draw = ImageDraw.Draw(image)
fontsize = 150
font = ImageFont.truetype("font.TTF", fontsize)
txt = '가'
draw.text((30, 5), txt, (0,0,0), font=font)
img = image.resize((188,45), Image.ANTIALIAS)
print(type(img))
plt.imshow(img)

img = np.array(img)
im = img[:,0:50,0]
im = im < 128
skel = mh.thin(im)
noholes = mh.morph.close_holes(skel)
plt.subplot(311)
plt.imshow(im)
plt.subplot(312)
plt.imshow(skel)

输出2

enter image description here

3) 示例

from skimage.morphology import skeletonize
from skimage import draw
from skimage.io import imread, imshow
from skimage.color import rgb2gray
import os

# load image from file
img_fname='D:\Ammar Data\Debbie_laptop_data\Ammar\sslab-deeplearning\GAN models\sslab_GAN\skeleton\hangul_1.jpeg' 
image=imread(img_fname)

# Change RGB color to gray 
image=rgb2gray(image)

# Change gray image to binary
image=np.where(image>np.mean(image),1.0,0.0)

# perform skeletonization
skeleton = skeletonize(image)

plt.imshow(skeleton)

输出3

enter image description here

最佳答案

您的代码很好,但您可能需要更改将图像转换为二进制的方式。此外,为了避免看起来嘈杂的输出,您可以将 binary_looking 应用于骨架图像。看看下面的代码 -

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()

您的两个示例图像给了我以下输出 -

enter image description here

enter image description here

编辑: 为了避免将图像转换为 bool 时的精度损失,您还可以使用可用的 thresholding algorithms 之一对图像进行二值化。 。我更喜欢大津的。

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()

关于python - 如何从图像中提取平滑的骨架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55596378/

相关文章:

python - 通过 Python shell 执行 Git 服务器命令

Python warn 打印消息并在 Windows cmd 上调用

python - powercli 中的 invoke-vmscript 的 pyvmomi 等效项是什么?

php - 绘制图像的脚本

python - 检测扫描图像中的疟疾细胞

python - python中的中值下采样

python - 在 Python 3 CGI 脚本中设置编码

android - 验证 Android 位图是 GrayScale

c# - validateImageData 参数和 Image.FromStream()

python - 导入 skimage.io 时 TkInter 在 OSX 上崩溃