deephealthproject / pyecvl

Python wrapper for the ECVL.
MIT License
4 stars 2 forks source link

Update docs with info on displaying images #69

Closed simleo closed 2 years ago

simleo commented 2 years ago

As pointed out by the ECVL team during the DeepHealth Winter School, if users want to display images that have been converted to numpy arrays, some transformations are likely required. This is due to the fact that python libraries that display numpy arrays as images use different conventions than those used by ECVL: the formers expect an RGB image in yxc layout, while ECVL, by default, uses BGR and xyc. We should add a note to the docs, suggesting to apply ChangeColorSpace and RearrangeChannels to the image.

NOTE:

I initially suspected this had something to do with the fact that ECVL images -- and thus numpy arrays obtained by converting them -- have a column-major (aka fortran-style) internal memory layout (see strides computation), while numpy arrays by default have a row-major (or c-style) layout:

>>> import numpy as np
>>> import pyecvl.ecvl as ecvl
>>> img = ecvl.Image([2, 3, 4], ecvl.DataType.uint8, "xyc", ecvl.ColorType.RGB)
>>> img_a = np.array(img)
>>> img_a.shape
(2, 3, 4)
>>> img_a.strides
(1, 2, 6)
>>> np.isfortran(img_a)
True
>>> a = np.ones([2, 3, 4], dtype=np.uint8)
>>> a.strides
(12, 4, 1)
>>> np.isfortran(a)
False

However, this is actually not an issue, since numpy also supports column-major layouts (as shown above) and that's internal: the contents of the array don't change, but only how N-dimensional indices are internally handled to address the elements. The reason for rotated / flipped images is the difference in the channel order convention, as stated above.

CC: @prittt @lauracanalini

prittt commented 2 years ago

Thank you very much @simleo for the update.