Fitting a curve to a segmented image

In my current data analysis, I have some segmented images, such as below.

My problem is that I would like to place a polynomial or spline (s.th. one-dimension) in a specific area (red) in a segmented image. (the result will be a black line).

Usually I would use something like orthogonal distance regression, the problem is that it needs some suitable function, which I don’t have in this case. So what would be the best way to do this with python / numpy? Maybe some standard algorithm for this kind of problem?

example

UPDATE: it seems that my drawing skills are probably not the best, the red area in the picture may also have some random noise and should not be fully connected (there may be small gaps due to noise).

UPDATE2: The general goal would be to have a parameterized curve p (t) that returns the position, that is, p (t) => (x, y) for t in [0,1]. where t = 0 is the beginning of the black line, t = 1 is the end of the black line.

+6
source share
1 answer

I used scipy.ndimage and this gist as a template. This will allow you to almost find a place, you will need to find a reasonable way to parameterize the curve in the main skeletonized image.

 from scipy.misc import imread import scipy.ndimage as ndimage # Load the image raw = imread("bG2W9mM.png") # Convert the image to greyscale, using the red channel grey = raw[:,:,0] # Simple thresholding of the image threshold = grey>200 radius = 10 distance_img = ndimage.distance_transform_edt(threshold) morph_laplace_img = ndimage.morphological_laplace(distance_img, (radius, radius)) skeleton = morph_laplace_img < morph_laplace_img.min()/2 import matplotlib.cm as cm from pylab import * subplot(221); imshow(raw) subplot(222); imshow(grey, cmap=cm.Greys_r) subplot(223); imshow(threshold, cmap=cm.Greys_r) subplot(224); imshow(skeleton, cmap=cm.Greys_r) show() 

enter image description here

You can find other answers that are useful when using skeletonization, for example, here:

Skeletonization Issues for Contouring

+3
source

Source: https://habr.com/ru/post/969341/


All Articles