How can I open and read an MHA file from within a Maven project?

Hi,

I am new to ImageJ. I can see that the Fiji distribution of ImageJ supports MHA file access:

I am trying to build a very simple Java/Maven application that reads MHA files. I believe I have declared the right things in my pom.xml file:

[...]
<parent>
  <groupId>org.scijava</groupId>
  <artifactId>pom-scijava</artifactId>
  <version>16.2.0</version>
</parent>
	<repositories>
		<repository>
			<id>scijava.public</id>
			<url>https://maven.scijava.org/content/groups/public</url>
		</repository>
	</repositories>
	<dependencies>
		<dependency>
		  <groupId>net.imagej</groupId>
		  <artifactId>imagej</artifactId>
		</dependency>
		<dependency>
		    <groupId>sc.fiji</groupId>
		    <artifactId>fiji</artifactId>
		</dependency>
	</dependencies>

My reading code is :

		ImageJ ij = new ImageJ();
		Object dataset = ij.io().open(mhaFile.getAbsolutePath());

I get an exception when the first line is executed:

log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
17:35:22.215 [SciJava-6579cdbb-Thread-0] DEBUG loci.formats.ClassList - Could not find loci.formats.in.SlideBook6Reader
java.lang.ClassNotFoundException: loci.formats.in.SlideBook6Reader
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at loci.formats.ClassList.parseLine(ClassList.java:196)
	at loci.formats.ClassList.parseFile(ClassList.java:258)
	at loci.formats.ClassList.<init>(ClassList.java:138)
	at loci.formats.ClassList.<init>(ClassList.java:122)
	at loci.formats.ImageReader.getDefaultReaderClasses(ImageReader.java:79)
	at io.scif.bf.BioFormatsFormat.cacheReaderClasses(BioFormatsFormat.java:487)
	at io.scif.bf.BioFormatsFormat.<init>(BioFormatsFormat.java:135)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at org.scijava.plugin.PluginInfo.createInstance(PluginInfo.java:304)
	at org.scijava.plugin.DefaultPluginService.createInstance(DefaultPluginService.java:236)
	at org.scijava.plugin.DefaultPluginService.createInstances(DefaultPluginService.java:225)
	at org.scijava.plugin.DefaultPluginService.createInstancesOfType(DefaultPluginService.java:216)
	at io.scif.services.DefaultFormatService$2.run(DefaultFormatService.java:451)
	at org.scijava.thread.DefaultThreadService$2.run(DefaultThreadService.java:220)
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

When the second line is executed, I get:

io.scif.FormatException: C:\...\Normal001-MRA.mha: No supported format found.
	at io.scif.services.DefaultFormatService.getFormatList(DefaultFormatService.java:351)
	at io.scif.services.DefaultFormatService.getFormat(DefaultFormatService.java:317)

What am I missing? I carefully looked in the forum, but can’t understand my mistake.

Thanks for your help,

FC

NB: By MHA I mean radiographic image files, such as the ones freely available here:
http://insight-journal.org/midas/community/view/21
(scroll down to the bottom of the page, and select a folder, such as ‘Normal-109’ for instance, then ‘T2’)

Hi @fcunilim,

could you please try if

ImagePlus imp = IJ.openImage(filename);

works for your mha file? You may need to add a dependency to imagej-legacy or imagej-ij for that though…

Cheers,
Robert

Hi Robert,

I tried what you said: openImage returns null.

I also tried to modify (with the same result) my pom.xml to add the dependency you suggested:

<dependency>
    <groupId>net.imagej</groupId>
    <artifactId>imagej-legacy</artifactId>
</dependency>

I can’t find any artifact called ‘imagej-ij’, though.

Note that MHA files contain 3D data (i.e. XYZ voxels), not sure if they can be opened through an ‘openImage’ method.

FC

Hi
You should checkout SCIFIO library,
That’s principally for IO stuff.
There’s good tutorial there too

Same result again and again:

[ERROR] null
io.scif.FormatException: C:\...\Normal001-MRA.mha: No supported format found.
	at io.scif.services.DefaultFormatService.getFormatList(DefaultFormatService.java:350)
	at io.scif.services.DefaultFormatService.getFormat(DefaultFormatService.java:316)
	at io.scif.services.DefaultDatasetIOService.canOpen(DefaultDatasetIOService.java:82)
	at io.scif.io.DatasetIOPlugin.supportsOpen(DatasetIOPlugin.java:64)
	at org.scijava.io.IOService.getOpener(IOService.java:55)
	at org.scijava.io.DefaultIOService.open(DefaultIOService.java:66)
	at fc.FooBar.App.main(App.java:16)

pom.xml:

<parent>
  <groupId>org.scijava</groupId>
  <artifactId>pom-scijava</artifactId>
  <version>27.0.1</version> <!-- latest version, june 2019 -->
</parent>
	<repositories>
		<repository>
			<id>scijava.public</id>
			<url>https://maven.scijava.org/content/groups/public</url>
		</repository>
	</repositories>
  <dependencies>
<dependency>
  <groupId>io.scif</groupId>
  <artifactId>scifio</artifactId>
</dependency>
  </dependencies>

FC

Consider opening your file with the ImageJ software, to see if it can actually be opened.
I think SCIFIO is unable to parse your metadata!

Hi all,

I just tried opening this MHA file with Fiji and it works fine:
http://insight-journal.org/midas/bitstream/view/6323

image

Then, I tried to open it from a maven/ImageJ project in IntelliJ. IJ.openImage() doesn’t work as @fcunilim reported earlier. Furthermore, with the Fiji dependencies mentioned above one can try this:

import io.scif.img.IO;
import io.scif.img.SCIFIOImgPlus;
// ...
SCIFIOImgPlus image = IO.open("C:/structure/data/Normal001-DTI.mha");

It doesn’t work. The error message is:

[ERROR] Failed to open ImgPlus for source: C:/structure/data/Normal001-DTI.mha
io.scif.img.ImgIOException: io.scif.FormatException: C:/structure/data/Normal001-DTI.mha: No supported format found.
	at io.scif.img.ImgOpener.createReader(ImgOpener.java:425)
	at io.scif.img.ImgOpener.openImgs(ImgOpener.java:145)
	at io.scif.img.ImgOpener.openImgs(ImgOpener.java:113)
	at io.scif.img.IO.openImgs(IO.java:80)
	at io.scif.img.IO.open(IO.java:397)
	at net.haesleinhuepf.imagej.zoo.measurement.exp.Playground.main(Playground.java:12)
Caused by: io.scif.FormatException: C:/structure/data/Normal001-DTI.mha: No supported format found.

So I assume it’s not properly supported by SCIFIO.

If you look in the fiji plugin @fcunilim mentioned initially, you can see that its method load() would actually support loading the file but is private:
https://github.com/fiji/IO/blob/master/src/main/java/sc/fiji/io/MetaImage_Reader.java#L135

If one copies the whole Java class and makes this method public, then you can open the MHA file with this code:

ImagePlus imp = new MetaImage_Reader().load("C:/structure/data/","Normal001-DTI.mha", "Normal001-DTI.mha", true);
imp.show();

I’ve sent a pull-request to the Fiji repository to fix this API bug.

Let me know if anyone sees an alternate solution!

Cheers,
Robert

1 Like

Nice work!
In the mean, I suggest you checkout this tutorial on SCIFIO

Hi Robert,

Thank you, but I can’t copy/paste MetaImage_Reader.java and use-it as-is, as the compiler complains there is no ‘ImagePlus’ class in imagej-common. I tried using the latest version, to no avail.

imagej-common contains an ‘ImgPlus’ class, not ‘ImagePlus’. How come MetaImge_Reader uses ImagePlus?

Could you please provide a pom.xml file that is suitable to reproduce your fix? This would help me.
Thank you!

FC

1 Like

Nevermind, I got it to compile!

I just used the same pom.xml dependencies as the MetaImage_Reader author.

Thanks again.

1 Like

Hey @fcunilim,

good to hear to made it run. Just a note: If you want to find out in which dependency a class lives, you can use the maven search:
https://maven.scijava.org/#nexus-search;classname~ImagePlus

Maybe that helps next time :wink:

Cheers,
Robert

1 Like

Quick note that @haesleinhuepf updated the IO_ component of Fiji to make the MetaImage_Reader.load method public, and I released a new version sc.fiji:IO_:4.1.0 including this change. So hopefully it becomes easier to read MHA files directly now.

1 Like