TomographicImaging / CILViewer

A simple Viewer for 3D data built with VTK
Apache License 2.0
9 stars 6 forks source link

TIFF reading axis inversion? Possibly a numpy2vtkImage bug #257

Open paskino opened 2 years ago

paskino commented 2 years ago

I create a numpy array and then save it to disk as TIFF image. For that I use 2 different ways to fractionate the data:

  1. use numpy slicing -> saving with pillow -> displaying with ImageJ
  2. use numpy slicing -> convert numpy 2 vtkImageData -> use vtkTIFFWriter -> display with imageJ

Here the data displayed. It is clear that the Y axis is inverted in the 2 images.

I suppose the culprit is in the numpy2vtkImage function.

image

The file to produce the dataset is as follows.


import os
import unittest

import numpy as np
import vtk
from ccpi.viewer.utils.conversion import (Converter, cilRawResampleReader, cilMetaImageResampleReader,
                                          cilNumpyResampleReader, cilNumpyMETAImageWriter,
                                          vortexTIFFResampleReader)
import warnings

bits = 8
shape = (5, 10, 6)
size = shape[0] * shape[1] * shape[2]
input_3D_array = np.reshape(np.arange(size), newshape=shape)\
    .astype(dtype=eval(f"np.uint{bits}"))

# Create TIFF Files
twriter = vtk.vtkTIFFWriter()
vtkfnames = []
pilfnames = []
arr = input_3D_array
from PIL import Image
for i in range(arr.shape[0]):
    fname = 'vtk_tiff_test_file_{:03d}.tiff'.format(i)
    vtkfnames.append(os.path.abspath(fname))
    # Using vtk the Y axis gets reversed
    vtk_image = Converter.numpy2vtkImage(np.expand_dims(arr[i,:,:], axis=0))
    twriter.SetFileName(vtkfnames[-1])
    twriter.SetInputData(vtk_image)
    twriter.Write()
    fname = 'pil_tiff_test_file_{:03d}.tiff'.format(i)
    pilfnames.append(os.path.abspath(fname))
    im = Image.fromarray(arr[i])
    im.save(pilfnames[-1])
lauramurgatroyd commented 2 years ago

I think this is relevant / needs sorting out at the same time: https://github.com/vais-ral/CILViewer/issues/196