Image registration in Java with ITK

Hi @bogovicj,

Have you ever tried this?

@haesleinhuepf and I think it would be worth trying to explore whether we can have elastix more natively in Java…

3 Likes

@Christian_Tischer,

I have not, but thanks for pointing it out!
Let’s keep in touch as we try it out :slight_smile:

John

2 Likes

cc @NicoKiaru should be also very interesting for you. Let us know if you give it a try!

2 Likes

I put a small amount of time into trying things yesterday. Did:

git clone https://github.com/SuperElastix/SimpleElastix
mkdir SimpleElastix_build
cd SimpleElastix_build
cmake ../SimpleElastix
ccmake .  # Then set the WRAP_JAVA flag to ON
make -j8  # This did stuff and seemed to complete successfully

Among other things, I have a directory SimpleITK-build/Wrapping/Java/ which seems to have interesting stuff in it. Using that, I was able to follow this example to get a SimpleITK example working.

Couldn’t quickly find an analogous folder for SimpleElastix though…

John

3 Likes

In you SimpleElastix build folder, after you have build with WRAP_JAVA=ON, is there not an “SimpleITK-build/Wrapping/Java” folder?

The building steps for SimpleElastix Should^TM be identical to that of SimpleITK.

2 Likes

@kaspermarstal,

Thanks for following up here. (If we have questions in the future, do you have a preference for the communication channel to use? i.e, this forum, github, the elastix forum, somewhere else?)

There is such a folder. My mistake was that the java class, org.itk.simple.ElastixImageFilter
s in the simpleitk-...jar, but I was expecting it to be in a different or second jar for some reason… This clears things up, and I seem to have things working now.

I’ll file a PR fleshing out this part of the doc.

Thanks again,
John

2 Likes

I also tried the installation and it ended like this:

-- Installing: /Users/tischer/Documents/build/ITK-prefix/include/ITK-4.13/itkHessianImageFilter.hxx
-- Installing: /Users/tischer/Documents/build/ITK-prefix/include/ITK-4.13/itkSLICImageFilter.h
-- Installing: /Users/tischer/Documents/build/ITK-prefix/include/ITK-4.13/itkHessianImageFilter.h
-- Installing: /Users/tischer/Documents/build/ITK-prefix/include/ITK-4.13/itkObjectnessMeasureImageFilter.h
-- Installing: /Users/tischer/Documents/build/ITK-prefix/lib/cmake/ITK/Modules/SimpleITKFilters.cmake
[ 55%] Completed 'ITK'
[ 55%] Built target ITK
make: *** [all] Error 2

I guess that’s an issue? Because also the folder that should contain the Java wrapper is quite empty:

ls SimpleITK-build/
total 24
drwxr-xr-x   3 tischer  staff    96B Nov 20 20:30 ./
drwxr-xr-x  30 tischer  staff   960B Nov 20 20:34 ../
-rw-r--r--   1 tischer  staff    10K Nov 20 20:33 CMakeCacheInit.txt

This is how I configured using ccmake ., maybe that’s wrong?

I looked for error message in the make -j8 log and found those ones:

checking whether we are cross compiling... configure: error: in `/Users/tischer/Documents/build/PCRE-prefix/src/PCRE-build':
configure: error: cannot run C compiled programs.
[ 36%] Performing build step for 'PCRE'
make[3]: *** No targets specified and no makefile found.  Stop.
make[2]: *** [PCRE-prefix/src/PCRE-stamp/PCRE-build] Error 2
make[1]: *** [CMakeFiles/PCRE.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: /Users/tischer/Documents/build/ITK-prefix/lib/libitkminc2-4.13.a(minc2_error.c.o) has no symbols

Do you think any of those is the problem?

Ok, found this: https://simpleelastix.readthedocs.io/GettingStarted.html#pcre-perl-compatible-regular-expression-build-fails-on-mac-os-x
Probably something we should also add the the documentation for the Java installation instructions…

1 Like

@Christian_Tischer,

Did the build succeed after changing to clang?

I’m traveling this week, and so havn’t had much time. Hope to file that PR later this week…

John

It went much further but then I got another error. I think this was due to the wrapping of Ruby. I will try again, disabling all wrapping, but Java. Rather tedious, every attempt takes 1-2 hours and I cannot do anything else because my computer ventilation is too loud due to all the heavy compilation work :slight_smile:

I think when one asks for more cores N in the make -jN call than one has, it just gets stuck here, without any error message:

Installation complete
[ 61%] Completed 'Swig'
[ 61%] Built target Swig

Maybe something we could also add to the documentation.

1 Like

Running this on MacOS:

git clone https://github.com/SuperElastix/SimpleElastix
mkdir SimpleElastix_build
cmake -DCMAKE_CXX_COMPILER:STRING=/usr/bin/clang++ -DCMAKE_C_COMPILER:STRING=/usr/bin/clang ../SimpleElastix/SuperBuild
ccmake . # Enable *only* WRAP_JAVA, disable all other WRAP_*
make -j3 # 3 is the number of cores, must be <= the number of cores of the system

Finished after about 2 hours with this output:

[100%] Built target DemonsRegistration2
[ 98%] No install step for 'SimpleITKExamples'
[100%] Completed 'SimpleITKExamples'
[100%] Built target SimpleITKExamples

I guess that’s a good start? I will test more tomorrow.

2 Likes

Whew… I’ll look forward to hearing more.

I’ve got started on documenting, but there’s still more I’d like to add:

1 Like

I am documenting more steps, just so that we can find it back if needed.

in IntelliJ

And then in your Application you still have to add the java.library.path

This allowed me to run this code:

Image image = SimpleITK.readImage(ROOT_DIR + "/test-data/sbem.mhd" );
Image output = SimpleITK.discreteGaussian(image);
SimpleITK.writeImage(output, ROOT_DIR + "/tmp/sbem-blur.mhd");

Note on image file formats

I am using here the .mhd file format. Reading data from ome.tif did not work in a sense that the output image was empty.

1 Like

Does this work for you too?

If so, would you be up for adding those images to an IntelliJ section of the doc?
(or I can add them to the PR I’m putting together, with credit, if that’s okay with you)

John

1 Like

Yes, this works, too.
I just tested this code and it ran through :slight_smile:

		final ElastixImageFilter elastix = new ElastixImageFilter();
		Image fixed = SimpleITK.readImage(ROOT_DIR + "/test-data/sbem.mhd" );
		Image moving = SimpleITK.readImage(ROOT_DIR + "/test-data/sbem.mhd" );
		elastix.setFixedImage( fixed );
		elastix.setMovingImage( moving );
		elastix.setParameterMap( SimpleITK.getDefaultParameterMap("rigid") );
		elastix.execute();
		final Image resultImage = elastix.getResultImage();

Yes, please go ahead!

1 Like

The next thing to figure out would be how to construct an ITK Image from an RandomAccessibleInterval, without going through saving stuff on disk. Did you already look into this?

That’ll be next on the list after doc :slight_smile:

The imagej/imagej-itk repository might already have what you need:

3 Likes