Matlab读写超过4GB的图像

Author Avatar
Notouch
发表:2025-03-04 23:14:39
修改:2025-03-04 23:14:39

💬 Brief introduction to the TIFF format

A TIFF file is, well, a Tagged Image File Format (TIFF). As the name implies, the file itself consists of a whole bunch of ‘tags’ and corresponding values. For example, all TIFF files are expected to have the ImageWidth and ImageLength tags, whose values are the width and length (height) of the image stored within.

A core feature of this format lies in how image data is organized in a physical file. TIFF allows for the image data to be stored in chunks as either ‘Tiles’ or ‘Strips’. You could think of a strip as a tile whose width is the same as the image. The format stores offsets for each of these strips or tiles in an image header. This allows for efficient random access of any chunk in the image. The original TIFF format specification calls for use of 32 bit file offset values. I guess you can imagine where I am going with that bit of information. 32 bits implies a maximum value of 2^32 for any offset. Note that these are offsets from the very beginning of the file. In effect, the 32 bit requirement limits the size of the largest possible TIFF file to be under 4 gigabytes. This was fine for a long time (The earliest TIFF release was in 1986), but we now have more data producers hitting this limit.

* * *

💦 读取图片

* * *

Matlab中imread()支持读取超过4GB的tiff图像。当然也支持LibTIFF自带的读取函数:

函数

Function

read

读取整个 TIFF 图像

readEncodedStrip

从指定条带读取数据

readEncodedTile

从指定图块读取数据

💨 写入图片

LibTIFF版本4.0.0开始,其偏移值格式使用64位而不是32位的,故后面也叫做Bigtiff,理论上最大可以支持17,179,869,184‬GB的图片。

🚵 实现思路

Matlab中以 obj.TiffObject = Tiff(obj.Filename, ‘w8’)方式开始写入Bigtiff;

然后结合blockproc函数分块写入图像。

🙌 代码实现

实现16位灰度图像写入。

🤹‍ 参考Steve on Image Processing and MATLAB构造ImageAdapter类

👩‍👩‍👧‍👧 Demo实现

🙆 参考链接

LibTIFF
bigTiffWriter
Steve on Image Processing and MATLAB

🤽‍ 以上。(づ●─●)づ

评论