您的位置:首页 > 编程语言 > MATLAB

Matlab读写TIFF格式文件

2016-03-04 21:41 561 查看

1、简介

通常情况下,使用MATLAB做图像处理后,使用下面的命令就可以保存处理结果为图片。

imwrite(im,'im.bmp');


而如果需要保存的图像为single或者double类型,或保存的图像超过RGB三个通道时,则不能使用imwrite来直接进行,此时需要将矩阵保存为TIFF格式的图片。

matlab支持LibTIFF库作为TIFF图像读写的工具,因此只要学习如果使用LibTIFF提供的matlab接口就可以完成TIFF图像的读写任务。

使用TIFF保存图像时使用的详细的TAG信息非常的多,也很复杂,这里不做过多详细的说明,仅做出通常使用的示范。若想要更多的了解请阅读 Tiff online文档

2、基本操作范例

首先介绍TIFF影像的保存。

1、待保存的影像矩阵

% ··· 预处理得到待保存的影像: im


2、通过构建一个Tiff对象生成待读取的影像,通过第二个参数表示写(‘w’)和添加(‘a’)模式

t = Tiff('myfile.tif','w');


TIFF影像通过IFD(Image File Directory)组织一幅影像的数据和元数据。具体说明如下:

When you create a new TIFF file, the Tiff constructor creates a file containing an image file directory (IFD). A TIFF file uses this IFD to organize all the data and metadata associated with a particular image. A TIFF file can contain multiple IFDs. The Tiff object makes the IFD it creates the current IFD. Tiff object methods operate on the current IFD. You can navigate among IFDs in a TIFF file and specify which IFD is the current IFD using Tiff object methods.

3.设置TIFF tags这里相当于设置图片的头文件信息,方法是给tagstruct对象赋值。这里比较关键也是容易出错的地方

% 影像大小信息(这两项比较简单)
tagstruct.ImageLength = size(im,1) % 影像的长度
tagstruct.ImageWidth = size(im,2)  % 影像的宽度

% 颜色空间解释方式,详细见下文3.1节
tagstruct.Photometric = 1

% 每个像素的数值位数,single为单精度浮点型,对于32为系统为32
tagstruct.BitsPerSample = 32
% 每个像素的波段个数,一般图像为1或3,但是对于遥感影像存在多个波段所以常常大于3
tagstruct.SamplesPerPixel = 4
tagstruct.RowsPerStrip = 16
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky
% 表示生成影像的软件
tagstruct.Software = 'MATLAB';
% 表示对数据类型的解释
tagstruct.SampleFormat = 3
% 设置Tiff对象的tag
t.setTag(tagstruct)

% 以准备好头文件,开始写数据
t.write(nmf);
% 关闭影像
t.close


3、tagstruct详解

设置tagsstruct是保存TIFF格式影像比较麻烦的问题,一些tagsstruct比较直观,还有一些需要更进一步的了解才能正确设置,否则就无法正确使用TIFF格式,下面将详细介绍比较麻烦的几个tag。

3.1、Photometric Interpretation (颜色空间解译方式)

取值(前面几项比较常用,后面表示不同的颜色空间,根据需要取用):

PHOTOMETRIC_MINISWHITE = 0;  % 最小值表示白色
PHOTOMETRIC_MINISBLACK = 1;  % 最小值表示黑色
PHOTOMETRIC_RGB = 2;         % RGB
PHOTOMETRIC_PALETTE = 3;     % 颜色表
PHOTOMETRIC_MASK = 4;        % 包含透明通道
PHOTOMETRIC_SEPARATED = 5;
PHOTOMETRIC_YCBCR = 6;
PHOTOMETRIC_CIELAB = 8;
PHOTOMETRIC_ICCLAB = 9;
PHOTOMETRIC_ITULAB = 10;
PHOTOMETRIC_LOGL = 32844;
PHOTOMETRIC_LOGLUV = 32845;


3.2、RowsPerStrip

IFD Image

Code 278 (hex 0x0116)

Name RowsPerStrip

LibTiff name TIFFTAG_ROWSPERSTRIP

Type SHORT or LONG

Count 1

Default 2**32 - 1

Description

The number of rows per strip. (每个strip包含的row个数。)TIFF影像可以通过组织strip来满足快速的随机访问和高效的I/O缓存。

RowsPerStrip and ImageLength 两个参数确定了整幅影像的strips个数,公式如下:

StripsPerImage = floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip).

StripsPerImage 不是一个属性,它仅仅是一个TIFF读取时可能需要使用到的值,因为它确定了影像的StripOffsets 和StripByteCounts值。

3.3、Sample Format (每个像素数值的解译方式)

SampleFormat

Tag = 339 (153.H)

Type = SHORT

N = SamplesPerPixel

可能的取值:

1 = unsigned integer data

2 = two’s complement signed integer data

3 = IEEE floating point data [IEEE] 当我们保存single类型数据的时候使用的就是这个值

4 = undefined data format

扩展值:

5 = Seperated, usually CMYK.

6 = YCbCr

8 = CIE L*a*b* (see also specification supplements 1 and 2)

9 = CIE L*a*b*, alternate encoding also known as ICC L*a*b* (see also specification supplements 1 and 2)

The TIFF-F specification (RFC 2301) defines:

10 = CIE L*a*b*, alternate encoding also known as ITU L*a*b*, defined in ITU-T Rec. T.42, used in the TIFF-F and TIFF-FX standard (RFC 2301). The Decode tag, if present, holds information about this particular CIE L*a*b* encoding.

The DNG specification adds these definitions:

32803 = CFA (Color Filter Array)

34892 = LinearRaw

需要注意的是:

SampleFormat 字段确定的是数据的类型(枚举值),而不是每个像素的大小,像素大小由BitsPerSample字段确定。SampleFormat默认取值为1。

小结

除了上面部分介绍的tags以外,还有其他tags此处并未做介绍,使用上面的信息已经可以满足最基本的存储要求,然而如果需要对影像进行压缩或做进一步处理的话,需要更多的了解TIFF关于压缩方面的设置。总之,TIFF影像的保存操作主要是设置正确的tags得到结果,所以如果需要进一步的拓展,可以查阅下面的链接,进一步了解。

参考链接:

Matlab: Exporting to Images

LibTIFF - TIFF Library and Utilities

TIFF Tag PhotometricInterpretation

TIFF Tag RowsPerStrip

Tiff online文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: