Example of retaining 16 bit encoding format when reading TIF image by Python


The encoding format of TIF image is generally 16bit. When using Python OpenCV to read TIF file, in order to keep its encoding format, we need to use the following methods:

import numpy as np
import cv2

img = cv2.imread('demo.tif', -1)

The output result is: uint16

For the imread function in opencv, the final parameters are explained as follows:

When the parameter > 0, opencv reads 3-channel color image (gray image will also be converted to color image by default), and the encoding format will be converted to 8bit

When parameter = 0, opencv reads 1-channel gray image, and the encoding format will be converted to 8bit

When the parameter is less than 0, opencv will read according to the format of the original image, and the encoding format will return 16bit

Another way to read TIF images is to use the tifile library to read them. In Python, you can first install them through pip

pip install tifffile

Then you can read the TIF image with the following code:

import tifffile as tiff
import numpy as np

a = tiff.imread('demo.tif')

The above example of retaining its 16bit encoding format when reading TIF pictures by Python is the whole content shared by Xiaobian. I hope it can give you a reference and support developer.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]