[Basic knowledge] introduction and reading of tiff format pictures

1. What is tiff format

The TIFF format is also called TIF, belongs to 位图格式, the full name is Tag Image File Format.

TIFF format and JPEG and PNG are all mainstream and 高位彩色图像格式.

Friends who have used scanners or faxes should remember that many brands and their default files have the .tiff suffix. Because the original design purpose of TIFF was around 1980, scanner manufacturers reached a common and unified scanned image file format , rather than each manufacturer using its own proprietary format to cause confusion.

  1. TIFF is a relatively flexible image format. Its full name is Tagged Image File Format, and its file extension is TIF or TIFF. This format supports 256-color, 24-bit true color, 32-bit color, 48-bit color and other color bits, and also supports RGB, CMYK and YCbCr and other color modes, and supports multiple platforms.
  2. TIFF files can be uncompressed, larger in size, or compressed, and support RAW, RLE, LZW, JPEG, CCITT3 groups and 4 groups and other compression methods.
  3. TIFF format (Tag Image File Format) is a graphics format widely used on Macintosh, which has the characteristics of complex graphics format and large storage information. A large number of textures in 3DS and 3DS MAX are in TIFF format. The maximum color depth of TIFF is 32bit, and it can be stored using the LZW lossless compression scheme.
  4. The TIFF format produces very high-quality images and is often used for publishing and printing . It can display millions of colors (although grayscale images are limited to 256 colors or shades), and is typically used for larger image files than GIF or JPEG formats. Saving in this format is helpful if you are going to edit the image in a program that did not create it, since it is recognized by various programs.
  5. Used to exchange files between applications and computer platforms. TIFF is a flexible bitmap image format supported by nearly all painting, image editing, and page layout applications. Also, almost all desktop scanners can produce TIFF images.

Two, the composition of tiff

This part of the content is referenced from: https://blog.csdn.net/tony5243/article/details/123410603, only for learning records

TIFF (Tagged Image File Format) is a flag-based graphics that 指针organizes data by connections. TIFF differs from other markup languages ​​except for image data 还可以记录其他信息. 其他的图像格式可以嵌入到TIFF中为其所用. There are four types of TIFF:

  • TIFF-B, two-color;
  • TIFF-G, black and white grayscale;
  • TIFF-P, color graphics with palette;
  • TIFF-R, suitable for graphics in RGB colors.

TIFF consists of four parts, which are

  • Image file header Image File Header (IFH),
  • Image file directory Image File Directory (IFD),
  • Directory entry Directory Entry (DE),
  • image data.

The image 8字节的文件头starts with the file header pointing to the first file directory . The file directory contains 图像的各种信息, also contains 一个指向实际图像数据的指针.

1. Image file header (IFH)

Byte 0-1: byte order flag, the value is IIor MM. II means little endian, also known as little-endian. MM means that the big byte comes first, and it becomes big-endian again.

Byte 2-3: TIFF flag, generally42

Byte 4-7: The offset of the first IFD. It can be at any position, but it must be on a word boundary, that is to say, it must be an integer multiple of 2.

2. Image File Directory (IFD)

Since there can be multiple images in a TIFF file, and there are several images 一个IFD只标识一个图像的所有属性in a TIFF file , there will be several IFDs .

Byte 0-1: Indicates how many IFDs this IFD contains DE. 每个DE只标识了图像的一个属性,那么这幅图像有N个属性就会有N个DE. Assume here that the number is n

Byte 2-(n*12+1): n DEs

Byte (n 12+2)-(n 12+5): the offset of the next IFD,如果没有(只有一幅图像)则置为0

3. Directory entry (DE)

Byte 0-1: TAG , that is, the label number of the attribute , arranged in the file directory 升序.

Byte 2-3: data type.

Byte 4-7: Quantity. Pass过类型和数量可以确定存储此TAG的数据需要占据的字节数

Byte 8-11: If occupied 字节数少于4, then 数据直接存于此. If 超过4个, then what is stored here is 指向实际数据的指针
insert image description here
Note: ASCII type is a text type, which is 7-bit ASCII code plus 1 binary 0; Rational is a fraction type, consisting of two longs, the first is the numerator, and the second is the denominator.

4. Image data

This data may be compressed or uncompressed. If it is compressed, there are many kinds of compression algorithms. Therefore, the image data is the most complicated part of the TIFF file, and no software can translate all the compression algorithms yet.

Case Analysis

View tiff data with a hexadecimal image viewer as follows:
insert image description here

  • File header: Byte 0000-0007

**Byte 0-1:** values ​​are all 49, the corresponding ASCII code is II, that is, the small byte comes first

**Byte 2-3:** value is 42, TIFF flag bit

**Byte 4-7:** The offset of the first IFD is 8e, that is, the first IFD starts from 8e. The tif file has only one image and thus only one IFD.

  • Image file directory: Byte 008e-015f

**Byte 008e-008f: **The value is 11, that is, there are 17 directory entries DE, each DE occupies 12 bytes, and 17 DEs occupy 204 bytes in total;

**Byte 0090-015b:** 17 DEs with a total of 204 bytes, see the analysis of each DE below

**Byte 015c-015f: **The value is 0, that is, there is no second image (IFD)

  • Directory entry: Byte 0090-015f
    insert image description here

3. The difference between tiff and jpg formats

1. Different subjects

  1. tif: is a flexible bitmap format, mainly used to store images including photos and artwork.

  2. jpg: A common image format developed by the Joint Photographic Experts Group.

Two, different characteristics

  1. tif: The storage of image information is flexible and changeable, it can support many color systems, and it is independent of the operating system , so it has been widely used.

  2. jpg: Use lossy compression to remove redundant images and color data, and can display very rich and vivid images while obtaining extremely high compression ratios, that is, better image quality can be obtained with less disk space

3. Different advantages

  1. tif: In various geographic information systems , photogrammetry , and remote sensing applications, images are required to have geocoding information , such as the coordinate system where the image is located, the scale, the coordinates of points on the image, latitude and longitude, length units, and angle units.

  2. jpg: It can realize progressive transmission , that is, the outline of the image is transmitted first, and then the data is gradually transmitted to continuously improve the image quality, so that the image can be displayed from hazy to clear.

4. How to read tiff images

1. python-opencv

The encoding format of tif images is generally yes 16bit. When using python-opencv to read tif files, in order 保留其编码格式, we need to use the following method:

import numpy as np
import cv2

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

输出结果为:uint16

The last parameter of the imread function in opencv is explained as follows:

  • When the parameter>0, opencv reads 3通道的彩色图( 灰度图也会被默认转化成彩色图), and the encoding format will be converted into8bit

  • At that time参数=0 , what opencv read was that 1通道灰度图the encoding format would be converted into8bit

  • At that time参数<0 , opencv will 按照原图的格式读取, and the encoding format will 16bitreturn

2. tifffile

Another way to read tif images is to use the tifffile library. In python, you can first install it through pip:

pip install tifffile
import tifffile as tiff
import numpy as np

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

Guess you like

Origin blog.csdn.net/All_In_gzx_cc/article/details/127433271