imglib / imglib2-algorithm

Image processing algorithms for ImgLib2
http://imglib2.net/
Other
22 stars 20 forks source link

Unable to use CellImg as input for some algorithms #59

Closed MyleneSimon closed 6 years ago

MyleneSimon commented 6 years ago

Hi,

I am trying to write some simple code to try out imglib2, basically opening an image, process it using one of the algorithms and save it. When I want to open the image as a CellImg, I then get the following error when I call one of the Gauss or Thresholder for example:

Exception in thread "main" java.lang.IllegalStateException: Tried to create a new SCIFIOCellImg without a Reader to use for opening planes.
Call setReader(Reader) before invoking create()
    at io.scif.img.cell.SCIFIOCellImgFactory.create(SCIFIOCellImgFactory.java:159)
    at io.scif.img.cell.SCIFIOCellImgFactory.create(SCIFIOCellImgFactory.java:71)
    at net.imglib2.img.ImgFactory.create(ImgFactory.java:74)
    at net.imglib2.algorithm.gauss.AbstractGauss.<init>(AbstractGauss.java:110)
    at net.imglib2.algorithm.gauss.GaussFloat.<init>(GaussFloat.java:110)
    at net.imglib2.algorithm.gauss.Gauss.inFloatInPlace(Gauss.java:313)
    at net.imglib2.algorithm.gauss.Gauss.inFloatInPlace(Gauss.java:271)
    at net.imglib2.algorithm.gauss.Gauss.inFloatInPlace(Gauss.java:256)

Here is the code producing the error:

File file = new File( "/path/to/input/DrosophilaWing.tif" );  
String path = file.getAbsolutePath();

File fileO = new File( "/path/to/output/DrosophilaWing.tif" );

SCIFIOConfig config = new SCIFIOConfig();
config.imgOpenerSetImgModes( ImgMode.CELL );

ImgOpener imgOpener = new ImgOpener();

System.out.println("Opening input image");
final Img< T > image = (Img<T>) imgOpener.openImg( path, config );

System.out.println(image.factory().getClass());

System.out.println("Convolve image");
Gauss.inFloatInPlace( 2, image);

System.out.println("Save image");
ImgSaver imgSaver = new ImgSaver();
imgSaver.saveImg(fileO.getAbsolutePath(), image);

Am I missing something? If I don't put the SCIFIOConfig when opening the image, it is opened as an ArrayImg and I don't get any error.

Thanks!

ctrueden commented 6 years ago

Thanks for the report. Ran out of time today to investigate, but I'll try to reproduce tomorrow.

hanslovsky commented 6 years ago

Copying my reply from the imagej forum:

Based on the stacktrace that you shared, Gauss.java:313 acquires an ImgFactory (more specifically a SCIFIOCellImgFactory in the failing case) from your input and passes it to the constructor of GaussFloat which in turn calls the super constructor AbstractGauss. The exception message states that setReader needs to be called before invoking create(). This means that you cannot call Gauss.inFloatInPlace with an Img that produces a SCIFIOCellImgFactory.

Instead, I suggest you use Gauss3.gauss( double sigma, RandomAccessible< S > source, RandomAccessibleInterval< T > target ). Here, you will need to

  1. extend the input via any of Views.extend... to make sure that the source is defined where necessary, and
  2. create the target img, e.g. via ArrayImgs.doubles.

example usage (based off your code/variables):

final Img< T > image = (Img<T>) imgOpener.openImg( path, config );
RandomAccessible< T > source = Views.extendMirrorSingle( image );
RandomAccessibleInterval< DoubleType > target = ArrayImgs.doubles( Intervals.dimensionsAsLongArray( image ) );
Gauss3.gauss( sigma, source, target );

Update: The @Deprecated tag in the Gauss class also supports my suggestion to use Gauss3 instead.

hanslovsky commented 6 years ago

I am going to close this because this is not an issue with imglib2-algorithm.