如何从imag中提取平滑骨架

2024-04-18 04:04:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

输入图像样本

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)示例

^{pr2}$

输出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


Tags: fromio图像imageimport示例imgas
1条回答
网友
1楼 · 发布于 2024-04-18 04:04:27

您的代码很好,但是您可能需要更改将图像转换为二进制的方式。另外,为了避免看起来很嘈杂的输出,可以对骨架图像应用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()

你的两张图片给了我下面的输出-

enter image description here

enter image description here

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

^{pr2}$

相关问题 更多 >