tlambert03 / nd2

Full-featured nd2 (Nikon NIS Elements) file reader for python. Outputs to numpy, dask, and xarray. Exhaustive metadata extraction
https://tlambert03.github.io/nd2
BSD 3-Clause "New" or "Revised" License
54 stars 15 forks source link

error while loading nd2 image #91

Closed jboulanger closed 2 years ago

jboulanger commented 2 years ago

Description

Loading a nd2 image leads to corrupted data. Rows are shifted by 1 pixels and there is a residual error even after shifing rows back compared to the image decoded by bioformats and a tif exported from NIS Elements.

Data accessible from here: https://cloud.mrc-lmb.cam.ac.uk/s/jJMMtqAaRALnnTq

What I Did

import nd2
import matplotlib.pyplot as plt
import numpy as np
import javabridge
import bioformats 
import tifffile
javabridge.start_vm(class_path=bioformats.JARS,run_headless=True)
fname = 'test.nd2'
imgs = [
    nd2.imread(fname),
    np.stack([line[np.arange(k,k+line.shape[0])%line.shape[0]] for k,line in enumerate(nd2.imread(fname))]),
    bioformats.load_image(fname, rescale=False),
    tifffile.imread(fname.replace('.nd2','.tif'))]
fig, ax = plt.subplots(1,len(imgs),figsize=(20,40))
titles = ['nd','nd2 shifted','bioformats','tiff']
for img,a,t in zip(imgs,ax,titles):    
    a.imshow(img)
    a.axis('off')
    a.set_title(t)

[np.abs(img.astype(float)-imgs[3].astype(float)).max() for img in imgs]

image

Max absolute error with tiff reference: [2707.0, 1564.0, 0.0, 0.0]

plt.imshow(np.abs(imgs[1].astype(np.float32)-imgs[3].astype(np.float32)),vmax=100)
plt.colorbar()

image

tlambert03 commented 2 years ago

thanks for the issue and the data @jboulanger. I think you're running into the same thing as #88 ... which was just fixed in #90

I'm not sure why nd2 sometimes has internally inconsistent metadata (where the widthPx is sometimes not a clean multiple of widthBytes * bytesPerPixel)... but your dataset appears to be one of them.

On the main branch, with #90 merged, nd2.imread appears to work fine for your file:

Screen Shot 2022-09-09 at 10 38 51 AM

I'll cut a new release soon

jboulanger commented 2 years ago

thanks

tlambert03 commented 2 years ago

releasing now... will take 30-60 mins to be available on pip. Closing this "optimistically" ... but feel free to reopen if the new version doesn't fix your issue

jboulanger commented 2 years ago

Just tested again on the image, the original image (tif or loaded with bioformat) had a width of 4801 but the one opened with the new version has an extra column at the end (width=4802).

tlambert03 commented 2 years ago

Yeah, I can add something to crop that I suppose. I think that at this point, it becomes a bit of a matter of choice with how to deal with the fact that the image metadata itself says that the data on disk is 4802 pixels. Let me try to learn a bit more from LIM (the company that created the nd2 format) about these relatively rare cases where the metadata is internally inconsistent, to see how they would "officially" have it be interpreted (rather than just mimicking bioformats choice, which may also just be one person's decision). thanks for letting me know!

jboulanger commented 2 years ago

If it helps deciding: the TIFF file was exported directly from NIS Elements where is the image is also 4801 x 4801.

tlambert03 commented 2 years ago

Yeah thanks, and the nis viewer also opens it at 4801

tlambert03 commented 2 years ago

merged a better fix in #92, and it will be in v0.4.2 which is building now

jboulanger commented 2 years ago

Amazing!