Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit curve to segmented image

In my current data analysis I have some segmented Images like for example below.

My Problem is that I would like to fit a polynom or spline (s.th. one-dimensional) to a certain area (red) in the segmented image. ( the result would be the black line).

Usually i would use something like orthogonal distance regression, the problem is that this needs some kind of fit function which I don't have in this case. So what would be the best approach to do this with python/numpy? Is there maybe some standard algorithm for this kind of problem?

example

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

UPDATE2: The overall target would be to have a parametrized curve p(t) which returns the position i.e. p(t) => (x, y) for t in [0,1]. where t=0 start of black line, t= 1 end of black line.

like image 452
jrsm Avatar asked May 15 '14 16:05

jrsm


People also ask

What is curve fitting in image processing?

2D curve-fitting is a method that converts the image data (pixel values) to a set of mathematical equations that are used to represent the image. These equations have a fixed form with a few coefficients estimated from the image which has been divided into several blocks.

Which method is best for curve fitting?

Curve Fitting using Polynomial Terms in Linear Regression Despite its name, you can fit curves using linear regression. The most common method is to include polynomial terms in the linear model. Polynomial terms are independent variables that you raise to a power, such as squared or cubed terms.


1 Answers

I used scipy.ndimage and this gist as a template. This gets you almost there, you'll have to find a reasonable way to parameterize the curve from the mostly 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 may find other answers that reference skeletonization useful, an example of that is here:

Problems during Skeletonization image for extracting contours

like image 180
Hooked Avatar answered Sep 21 '22 21:09

Hooked