# Bicubic interpolation function

Hi everyone,
Is anyone aware of a means to obtain a bicubic spline interpolating function that is computed from an image using any ImageJ or Fiji method? I know how to to upsample an image using Image>Adjust>Size… with a bicubic interpolator, but this returns an upsampled image, not the interpolated function.

What I would like instead is a method that, after fitting, will accept any (x, y) tuple where x and y are real numbers and the returned number is the value of the interpolated function at that location.

The Apache Commons Math library provides such a class called PiecewiseBicubicSplineInterpolatingFunction, but I have been unable to determine whether similar functionality is in ImageJ.

Thanks!

Good day,

you may have a look at
http://www.ee.ucl.ac.uk/~mflanaga/java/BiCubicSpline.html

Of course you have to call this Java code from other code, e.g. an ImageJ plugin or a macro.

Regards

Herbie

1 Like

The `Views.interpolate` method from ImgLib2 would give you such a thing (called `RealRandomAccessible`) that you can query for any given position in an n-dimensional space.
Here’s an example Groovy script:

``````#@ Img img

import net.imglib2.view.Views
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory

interpolated = Views.interpolate(img, new NLinearInterpolatorFactory()).realRandomAccess()

interpolated.setPosition([10.3, 20.7, 30.9] as double[])
println interpolated.get().getRealDouble()
interpolated.setPosition([74.5, 92.8, 14.1] as double[])
println interpolated.get().getRealDouble()
``````

But this example uses linear interpolation (`NLinearInterpolatorFactory`), and I’m not aware of a cubic interpolation factory that you can feed as `InterpolatorFactory` input for `Views.interpolate()`.

Maybe others can comment on this (@tpietzsch, @axtimwalde, @bogovicj, …).

2 Likes

Thanks for this! Indeed, I would prefer to have a cubic interpolator, but in the meantime a linear interpolator may very well suffice.

And thank you @anon96376101 for pointing out Michael Flanagan’s library. I had found it previously but am a bit hesitant to commit to using it in the long run since its last update was in 2012.

Cheers,
kmd

Well,

I use the 2012 1D-cubic spline interpolation since long without any problems. Don’t forget that mathematics don’t get outdated and if coded carefully, computer implementations remain valid …

Regards

Herbie

3 Likes

The Apache math interpolation classes work very well. I also find it is easy to change between them and compare a wide range of interpolators. What is your hesitation to using them exactly? They are no trouble to compile into an ImageJ plugin.

Indeed, if you want to interpolate between the values of a 2D array, you can use `BicubicInterpolator` from Apache `commons-math3` that ships with Fiji.

The following Groovy script illustrates its use:

``````import org.apache.commons.math3.analysis.interpolation.BicubicInterpolator

x = [1,2,3,4]
y = [1,2,3,4]
f = [[10, 20, 30, 40], [20, 30, 40, 50], [30, 40, 50, 60], [40, 50, 60, 70]]

ip = new BicubicInterpolator()

intpFunction = ip.interpolate(x as double[], y as double[] ,f as double[][])

println intpFunction.value(2.5, 2.0)
println intpFunction.value(2.5, 2.5)
``````

This might or might not be sufficient for your needs, @kmdouglass. If you work with image data in ImageJ, you’ll have to get your pixel data into a 2D `double` array, so I’m not sure how this compares to ImgLib2 in terms of performance for larger images.

I’m not sure whether there is an implementation of cubic interpolation for 3D (or n-D) images, though.

2 Likes

I was unaware that commons-math3 shipped with Fiji.

Happily this solves my problem. I was looking for ways to avoid increasing the number of external dependencies in my project since it is of moderate complexity. My project already depends on Fiji, which is why I was originally searching for something within Fiji. But since Apache commons-math3 is already part of Fiji, then I suppose the project already depends on commons-math3 as well, and using its BicubicInterpolator won’t increase the dependency count.

I’m not too familiar with Java development, so maybe this paranoia about dependences is not justified. I only know that in my experience with C++/Python development that the amount of maintenance required tends to increase as I add more libraries, so I try to avoid dependency bloat when I can.

Thanks everyone!
Kyle

2 Likes

In general, it’s a good idea to not include unnecessary dependencies. But instead of re-implementing something that is available already (and both modular and performant enough for your needs), I’d always go for third-party dependencies.

In addition, the establishment of Maven as a dependency management tool throughout the SciJava and ImageJ ecosystem – as well as @ctrueden’s fantastic work of creating a central bill of materials (BOM) with `pom-scijava` – really make dependency management a breeze

2 Likes