cv2.IMREAD_ANYDEPTH changes shape of a .tiff image

28 Views Asked by At

I want to open an image using cv2 in the original bit depth and eventually split the channels for analysis. Using the cv2.IMREAD_ANYDEPTH tag works but changes im.shape. What is happening and how can I avoid this behavior? This is being done in Colab.

import cv2

im = cv2.imread(image.tiff)

print(im.dtype)
print(im.shape)

im = cv2.imread(image.tiff, cv2.IMREAD_ANYDEPTH)

print(im.dtype)
print(im.shape)

The output:

uint8
(957, 641, 3)
uint16
(957, 641)

According to various posts this should not happen. Still not sure why the channels are being lost.

1

There are 1 best solutions below

1
john On

Seems that this issue can be totally avoided using the following different tag.

im = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)

This preserves the dtype and shape.