Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smoothing jagged edges of an image

I would like to generate a skeleton out of an image. Since the edges that are generated using skimage from the original image isn't smooth, the resulting skeleton obtained from binary has disconnected edges with knots.

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

I tried using dilate to smoothen the jagged edges. But the contours in the skeleton has two edges instead of a single edge that is desired.

I would like to ask for suggestions on how the edges can be smoothened to avoid knots and disconnected edges in the resulting skeleton.

edit.jpg Input image

output Output images

Edit:After using gaussian smoothing

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

enter image description here

like image 887
Natasha Avatar asked Jan 01 '26 14:01

Natasha


1 Answers

This median filter should do the work on your binary image for the skeletonization.

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

For the borders, I will probably use this and play with the parameters as shown in the link below 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)
like image 136
Sav Avatar answered Jan 03 '26 08:01

Sav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!