1. 程式人生 > >Matlab讀寫TIFF格式檔案

Matlab讀寫TIFF格式檔案

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文件