Skip to main content
 首页 » 编程设计

python-3.x之平滑图像的锯齿状边缘

2024年12月31日10yjmyzz

我想从图像中生成骨架。由于使用原始图像的 skimage 生成的边缘不平滑,因此从 binary 获得的生成的 skeleton 具有带结的断开边缘。

import skimage 
from skimage import data,io,filters 
import numpy as np 
import cv2 
import matplotlib.pyplot as plt 
from skimage.filters import threshold_adaptive,threshold_mean 
from skimage.morphology import binary_dilation 
from skimage import feature 
from skimage.morphology import skeletonize_3d 
 
imgfile = "edit.jpg" 
image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 
thresh = threshold_mean(image) 
binary = image > thresh 
edges = filters.sobel(binary) 
dilate = feature.canny(binary,sigma=0) 
skeleton = skeletonize_3d(binary) 
fig, axes = plt.subplots(nrows=2,ncols=2, figsize=(8, 2)) 
ax = axes.ravel() 
 
ax[0].imshow(binary, cmap=plt.cm.gray) 
ax[0].set_title('binarize') 
 
ax[1].imshow(edges, cmap=plt.cm.gray) 
ax[1].set_title('edges') 
 
ax[2].imshow(dilate, cmap=plt.cm.gray) 
ax[2].set_title('dilates') 
 
ax[3].imshow(skeleton, cmap=plt.cm.gray) 
ax[3].set_title('skeleton') 
 
for a in ax: 
    a.axis('off') 
 
plt.show() 

我尝试使用 dilate 来平滑锯齿状边缘。但是 骨架 中的轮廓有两条边,而不是所需的一条边。

我想就如何平滑边缘以避免生成的骨架中的结和断开的边缘提出建议。

edit.jpg输入图片

output输出图片

编辑:使用高斯平滑后

binary = image > thresh 
gaussian = skimage.filters.gaussian(binary) 
skeleton = skeletonize_3d(gaussian) 

请您参考如下方法:

这个中值滤波器应该对您的二值图像进行骨架化处理。

import scipy 
binary_smoothed = scipy.signal.medfilt (binary, 3) 

对于边框,我可能会使用它并使用下面链接中显示的参数 https://claudiovz.github.io/scipy-lecture-notes-ES/advanced/image_processing/auto_examples/plot_canny.html :

from image_source_canny import canny 
borders = canny (binary_smoothed, 3, 0.3, 0.2)