glencoesoftware / bioformats2raw

Bio-Formats image file format to raw format converter
GNU General Public License v2.0
76 stars 34 forks source link

Downsampling fails with MRC file created from tif2mrc #198

Closed blowekamp closed 1 year ago

blowekamp commented 1 year ago

The following is the exception I am encountering:

$ rm -rf test.zarr/ && ~/scratch/bioformats2raw-0.6.1/bin/bioformats2raw out.mrc   --compression=null --downsample-type AREA test.zarr --progress
[0/0] 100% │███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████│ 2/2 (0:00:00 / 0:00:00) 
[0/1]   0% │                                                                                                                                                         │ 0/2 (0:00:00 / ?) 
2023-04-03 08:21:04,680 [pool-1-thread-4] ERROR c.g.bioformats2raw.Converter - Failure processing chunk; resolution=1 plane=1 xx=0 yy=0 zz=1 width=150 height=150 depth=1
org.opencv.core.CvException: cv::Exception: OpenCV(3.4.2) /Users/jason/Projects/openpnp/opencv/opencv/opencv-3.4.2/modules/imgproc/src/resize.cpp:3862: error: (-215:Assertion failed) func != 0 in function 'resize'

    at org.opencv.imgproc.Imgproc.resize_0(Native Method)
    at org.opencv.imgproc.Imgproc.resize(Imgproc.java:2921)
    at com.glencoesoftware.bioformats2raw.OpenCVTools.opencvDownsample(OpenCVTools.java:172)
    at com.glencoesoftware.bioformats2raw.OpenCVTools.downsample(OpenCVTools.java:115)
    at com.glencoesoftware.bioformats2raw.Converter.getTileDownsampled(Converter.java:1046)
    at com.glencoesoftware.bioformats2raw.Converter.getTile(Converter.java:1078)
    at com.glencoesoftware.bioformats2raw.Converter.processChunk(Converter.java:1179)
    at com.glencoesoftware.bioformats2raw.Converter.lambda$saveResolutions$4(Converter.java:1387)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:750)

This does not occur with the "SIMPLE" downsampled, but the others based on openCV do.

I was unable to directly create a MRC file to reproduce the error. But the following produces the out.mrc which creates the error:

Python:

import SimpleITK as sick
img = sitk.Image([257,257], sitk.sitkUInt8)
sitk.WriteImage(img, "slice.tiff")

Note: It appears the size needs to be >256, as a tiff file of 256x256 seems to work OK.

Using 3dmod's tif2mrc:

$ tif2mrc slice.tiff slice.tiff out.mrc && header out.mrc
Opening slice.tiff for input
Opening slice.tiff for input
Min = 0, Max = 0, Mean = 0

 RO image file on unit   1 : out.mrc     Size=        130 K

 Number of columns, rows, sections .....     257     257       2
 Map mode ..............................    0   (bytes - signed in file)   
 Start cols, rows, sects, grid x,y,z ...    0     0     0     257    257      2
 Pixel spacing (Angstroms)..............   1.000      1.000      1.000    
 Cell angles ...........................   90.000   90.000   90.000
 Fast, medium, slow axes ...............    X    Y    Z
 Origin on x,y,z .......................    0.000       0.000       0.000    
 Minimum density .......................   0.0000     (  -128.00     in file)
 Maximum density .......................   0.0000     (  -128.00     in file)
 Mean density ..........................   0.0000     (  -128.00     in file)
 tilt angles (original,current) ........   0.0   0.0   0.0   0.0   0.0   0.0
 Space group,# extra bytes,idtype,lens .        0        0        0        0

     1 Titles :
tif2mrc: Converted to MRC format.                       03-Apr-23  08:22:03    
melissalinkert commented 1 year ago

Thanks for reporting this, @blowekamp.

This isn't specific to MRC, and can be more easily reproduced with:

$ bin/bioformats2raw --downsample-type=LINEAR "test&sizeX=257&sizeY=257&pixelType=int8.fake" test.zarr

and

$ bin/bioformats2raw --downsample-type=LINEAR "test&sizeX=256&sizeY=257&pixelType=int8.fake" test.zarr

Both of these work though:

$ bin/bioformats2raw --downsample-type=LINEAR "test&sizeX=256&sizeY=256&pixelType=int8.fake" test-int8.zarr
$ bin/bioformats2raw --downsample-type=LINEAR "test&sizeX=256&sizeY=257&pixelType=uint8.fake" test-uint8.zarr

which suggests that it's a combination of pixel type and image size. I don't have an immediate idea of why happens, so we'll need to investigate further.

blowekamp commented 1 year ago

Thanks you for looking into this.

Oddly using 3dmod's newstack command to convert this sample to uint8 it worked. However, the real data I have still produces the same error after conversion to uint8.

$ header out_uint8.mrc

 RO image file on unit   1 : out_uint8.mrc     Size=      19823 K

 Number of columns, rows, sections .....    4969    4085       1
 Map mode ..............................    0   (byte)                     
 Start cols, rows, sects, grid x,y,z ...    0     0     0    4969   4085      1
 Pixel spacing (Angstroms)..............   1.000      1.000      1.000    
 Cell angles ...........................   90.000   90.000   90.000
 Fast, medium, slow axes ...............    X    Y    Z
 Origin on x,y,z .......................    0.000       0.000       0.000    
 Minimum density .......................   9.0000    
 Maximum density .......................   255.00    
 Mean density ..........................   69.923    
 tilt angles (original,current) ........   0.0   0.0   0.0   0.0   0.0   0.0
 Space group,# extra bytes,idtype,lens .        0        0        0        0

     2 Titles :
tif2mrc: Converted to MRC format.                       03-Apr-23  11:53:49    
NEWSTACK: Images copied                                  3-Apr-23  11:54:07    

$ ~/scratch/bioformats2raw-0.6.1/bin/bioformats2raw out_uint8.mrc  --compression=null --downsample-type AREA test.zarr --progress
[0/0] 100% │█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████│ 20/20 (0:00:00 / 0:00:00) 
[0/1]   0% │                                                                                                                                                         │ 0/6 (0:00:00 / ?) 
2023-04-03 11:56:18,839 [pool-1-thread-1] ERROR c.g.bioformats2raw.Converter - Failure processing chunk; resolution=1 plane=0 xx=0 yy=1024 zz=0 width=1024 height=1018 depth=1
org.opencv.core.CvException: cv::Exception: OpenCV(3.4.2) /Users/jason/Projects/openpnp/opencv/opencv/opencv-3.4.2/modules/imgproc/src/resize.cpp:3881: error: (-215:Assertion failed) func != 0 && cn <= 4 in function 'resize'

    at org.opencv.imgproc.Imgproc.resize_0(Native Method)
    at org.opencv.imgproc.Imgproc.resize(Imgproc.java:2921)
    at com.glencoesoftware.bioformats2raw.OpenCVTools.opencvDownsample(OpenCVTools.java:172)
    at com.glencoesoftware.bioformats2raw.OpenCVTools.downsample(OpenCVTools.java:115)
    at com.glencoesoftware.bioformats2raw.Converter.getTileDownsampled(Converter.java:1046)
    at com.glencoesoftware.bioformats2raw.Converter.getTile(Converter.java:1078)
    at com.glencoesoftware.bioformats2raw.Converter.processChunk(Converter.java:1179)
    at com.glencoesoftware.bioformats2raw.Converter.lambda$saveResolutions$4(Converter.java:1387)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:750)
melissalinkert commented 1 year ago

However, the real data I have still produces the same error after conversion to uint8.

Can you check that bioformats2raw is actually recognizing the converted data as uint8? grep 'Type="uint8"' test.zarr/OME/METADATA.ome.xml should result in a match if uint8 is being detected correctly.

Potentially related issue on the OpenCV side: https://github.com/opencv/opencv/issues/7862

blowekamp commented 1 year ago

Can you check that bioformats2raw is actually recognizing the converted data as uint8? grep 'Type="uint8"' test.zarr/OME/METADATA.ome.xml should result in a match if uint8 is being detected correctly.

The ome.xml contains Type="int8". So the pixel type is not being detected correctly.

Potentially related issue on the OpenCV side: opencv/opencv#7862 That sounds like what has been encountered.

melissalinkert commented 1 year ago

The ome.xml contains Type="int8". So the pixel type is not being detected correctly.

That's a Bio-Formats issue then, and we'd need to see the original data to investigate. If you're comfortable sharing publicly, you can post a download link here, otherwise you can message the OME team privately with a link.

Potentially related issue on the OpenCV side: https://github.com/opencv/opencv/issues/7862 That sounds like what has been encountered.

Since it doesn't look like there is a version of OpenCV that supports these data types, probably the best we can do at the moment is issue a more informative warning or error in bioformats2raw.

blowekamp commented 1 year ago

Please find a synthetic image attached of [0-99] pixels as a10x10 uint8 mrc file.

$ tif2mrc -B 0 slice.tiff test_uint8.mrc
Opening slice.tiff for input
Min = 0, Max = 99, Mean = 49.5

$ header test_uint8.mrc 

 RO image file on unit   1 : test_uint8.mrc     Size=          1 K

 Number of columns, rows, sections .....      10      10       1
 Map mode ..............................    0   (byte)                     
 Start cols, rows, sects, grid x,y,z ...    0     0     0      10     10      1
 Pixel spacing (Angstroms)..............   1.000      1.000      1.000    
 Cell angles ...........................   90.000   90.000   90.000
 Fast, medium, slow axes ...............    X    Y    Z
 Origin on x,y,z .......................    0.000       0.000       0.000    
 Minimum density .......................   0.0000    
 Maximum density .......................   99.000    
 Mean density ..........................   49.500    
 tilt angles (original,current) ........   0.0   0.0   0.0   0.0   0.0   0.0
 Space group,# extra bytes,idtype,lens .        0        0        0        0

     1 Titles :
tif2mrc: Converted to MRC format.                       04-Apr-23  10:10:24    

test_uint8.mrc.zip

sbesson commented 1 year ago

Closing as the upcoming bioformats2raw release should address both issues: incorrect pixel type detection for MRC by Bio-Formats and unsupported OpenCV downsampling