Home > front end >  How to smooth a skeletonized image and search for a point on the smoothed line?
How to smooth a skeletonized image and search for a point on the smoothed line?

Time:06-08

I have a skeletonized image and I want to obtain a smoothened one from the image attached here below. The main goal after smoothening is to locate any point nearby or exactly at the center of the horizontal plane, I just want to pick any point say (x, y) = (530, 355) and determine if it lies on the horizontal line whether exactly at the center or at any part of the skeleton. Any procedures to go through this? input image

My trial

I tried to find the contour and then determine the largest contour from the skeleton but I cannot determine any point that lies on the horizontal plane. Any idea to go through this

CodePudding user response:

If I understand you correctly you are interested in the diagonal of the skeleton. Here is one idea that may need some work before it does exactly what you want.

I want to find a line where the picture is white by optimising the length and brightness of some starting line. For that it would be useful if the skeleton would be a bit thicker and it would turn into black slowly away from the center. So I do the following preprocessing

import numpy as np
import matplotlib.pyplot as plt
import cv2
from scipy.ndimage import map_coordinates

img = cv2.imread('test.png', cv2.IMREAD_GRAYSCALE)
img = ((img>100)*255).astype('uint8')
n = 20

img = cv2.dilate(img, np.ones((n 5,n 5)))
img = cv2.blur(img, (n 1,n 1)) 

enter image description here

Now I do the optimisation mentioned above

def get_points_on_line(x):
    p = x[:2].reshape(-1,1)
    q = x[2:].reshape(-1,1)
    t = np.linspace(0,1,10**3)
    coords = t*p (1-t)*q
    length = np.linalg.norm(p-q)
    values = (map_coordinates(img, coords, prefilter=False)-255*9/10)
    value = values.mean()
    return -value*length

res = minimize(get_points_on_line, [250,200,500,700], method = 'Nelder-Mead')

and plot the result

x = res.x
p = x[:2].reshape(-1,1)
q = x[2:].reshape(-1,1)
t = np.linspace(0,1,10**3)
coords = t*p (1-t)*q

plt.imshow(img, cmap='gray')
plt.plot(coords[1], coords[0])

enter image description here

  • Related