# Fitting a curve to binary images

Hello all.

I am using ImageJ/FIJI for my image analysis. I have a cross-sectional fluorescent image of a cell monolayer and would like to use these images to estimate the monolayer/cell height. The strategy I am hoping to use is fit a smooth curve to the upper and lower portion of the image, and then eventually subtract the z values of the two curves to calculate height at every point. I attached some sample images explaining what I’m hoping to do with manually drawn curves. Not all the cells are fluorescent which results in some dark gaps.

Does anyone know of a plugin which could automate fitting a smooth curve to the binary image? Or maybe there is a better strategy to calculate cell height? I can also import the images into Matlab if there are builtin functions which would be easier to use.

Thank you!
Vivek

A_FluorescentImage.tif (16.2 KB) B_BinaryImage.tif (16.2 KB) C_BinaryImageWithFit.tif (22.7 KB) D_BinaryImageWithFitAndHeight.tif (22.8 KB)

@vkg9

Searching here on the forum… perhaps there are pieces in this older thread that will help?

There is also a curve fitting macro code example you could potentially adapt/use:

``````// Curve Fitting Demo
//
// This macro demonstates how to use the Fit.* functions,
// which were added to the macro language in v1.41k.

x = newArray(0, 1, 2, 3, 4, 5);
y = newArray(0, 0.9, 4.5, 8, 18, 24);

// Do a straight line fit
Fit.doFit("Straight Line", x, y);
print("a="+d2s(Fit.p(0),6)+", b="+d2s(Fit.p(1),6));

// Do all possible fits, plot them and add the plots to a stack
setBatchMode(true);
for (i=0; i<Fit.nEquations; i++) {
Fit.doFit(i, x, y);
Fit.plot();
if (i==0)
stack = getImageID;
else {
run("Copy");
close();
selectImage(stack);
run("Paste");
}
Fit.getEquation(i, name, formula);
print(""); print(name+ " ["+formula+"]");
print("   R^2="+d2s(Fit.rSquared,3));
for (j=0; j<Fit.nParams; j++)
print("   p["+j+"]="+d2s(Fit.p(j),6));
}
setBatchMode(false);
run("Select None");
rename("Curve Fits");
``````

This is very helpful. I realize I should have done a more thorough forum search. Thank you!

1 Like