IJ2 save an image (with lots of frames) from Command

imagej2
scifio
scijava
Tags: #<Tag:0x00007fb87ed58cf0> #<Tag:0x00007fb87ed58b60> #<Tag:0x00007fb87ed585c0>

#1

Hi all

I have images with 10000+ frames that I need to save from commands. I was using

import io.scif.img.ImgSaver;

new ImgSaver().saveImg(...);

This worked fine when I ran from a ‘test’ ImageJ from eclipse. However when I ran my plugin from Fiji I got

java.lang.UnsupportedOperationException: Cannot instantiate more than one LegacyService

So I switched to…

@Parameter
DatasetService ds;

@Parameter
DatasetIOService io;
...
io.save(ds.create(im), imName);

Which works in Fiji. However it takes 20 minutes to save, as opposed to seconds the first way. (as I mentioned the image has 10000 frames, the second call seems to be slowly saving the image frame by frame).

So what is the proper way to save the output of a Command??


#2

You can avoid that by feeding the SciJava Context to the ImgSaver constructor. Otherwise, the ImgSaver internally creates a second context.

This is definitely the better way to go. Higher level, with context injection handled for you.

Wow! Did you profile to find out why? Is it something in the DatasetIOService layer on top of ImgSaver?

Note that this is going to change soon with the introduction of a (tentatively named!) ImageIOService that works with the unified (tentatively named!) RichImage (ImgPlus+Dataset unification/generalization) being developed at the currently ongoing hackathon at Janelia.


#3

Hi Curtis

Thanks for your quick reply. So it seems that I was wrong. Both ImgSaver(), and io.save() are slow. I profiled with JProfiler. Screen shot of the results are below. According to my results, it looks like getIFDOffsets might be the problem?? It is called twice each time through and looks to be the biggest time hog.


#4

And here is the test I used

@Test
    public void testSaveLongSeries() {
        
        ImageJ ij=new ImageJ();
        
        Img<DoubleType> bigStack=ij.op().create().img(new FinalDimensions(20,20,10000), new DoubleType());
        
        System.out.println("starting save");
        
        /*try {
            new ImgSaver().saveImg("test.tif", bigStack);
        } catch (ImgIOException | IncompatibleTypeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("error! error! error!");
        }*/
        
        try {
            ij.dataset().save(ij.dataset().create(bigStack), "test2.tif");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("finishing save");
        
    }

#5

Thanks @bnorthan for these details. Can you file an issue at https://github.com/scifio/scifio/issues ? I am very sorry I don’t have time to dig into it right now.

SCIFIO is going to see some major iterations soon: 1) unification of Dataset + ImgPlus into a single metadata-rich structure (my current project!); and 2) “blockization” of SCIFIO: using blocks instead of planes for its fundamental units of work (started last year, and critical to fully generalize everything to scalable N-D). My first thought was that maybe we should wait to explore TIFF speed improvements until after those iterations, but actually the needed changes might be orthogonal. I can’t work on it in the near future, but it is very important for this to work well. Depending how things go at LOCI in coming months, we may be able to put a student on it, which will improve matters.


Scifio - introduction to basic concepts?
#6

Thanks Curtis – I filed an issue. In the mean time do you have any suggestion on how to save the image?? I could write a utility to just save it as a raw file. Does something like that allready exist?? Just an easy way to dump dimensions, type and pixels to a temp file?


#7

Maybe just use IJ1’s API for now? The IJ1 TIFF saver is fast and saves much of the most crucial metadata:

IJ.saveAs(imp, "Tiff", path + ".tif");

#8

Thank you. This looks very useful!

@ctrueden, for saving imglib2 based images to disk, do you currently recommend to transform into ImagePlus or would you think it already makes sense to use scifio directly on an Img?


#9

I would certainly try SCIFIO directly first. If it is too slow, then you can continue using ImageJ1. Do what works.

BTW, the issue that @bnorthan filed back in June last year is here: scifio/scifio#310. And unfortunately, there isn’t much to report as far as progress. However, the funding situation is improving, so hopefully SCIFIO will see more development in 2018.


Scifio - introduction to basic concepts?