1. 程式人生 > ><轉>Matlab讀寫TIFF格式文件

<轉>Matlab讀寫TIFF格式文件

tla dev its hot efi width esc 顏色 encoding

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得到結果,所以如果需要進一步的拓展,可以查閱下面的鏈接,進一步了解。

參考鏈接:

    1. Matlab: Exporting to Images
    2. LibTIFF - TIFF Library and Utilities
    3. TIFF Tag PhotometricInterpretation
    4. TIFF Tag RowsPerStrip
    5. Tiff online文檔

<轉>Matlab讀寫TIFF格式文件