1. 程式人生 > >Maltab 儲存32位的tiff影象

Maltab 儲存32位的tiff影象

Matlab支援對多幅TIFF影象的寫入(write),但是,不支援32位的操作。換句話說,如果執行下面的程式碼:

imwrite(uint32(data), 'test.tif')

那麼程式會報錯:

Error using writetif (line 92)
Writing uint32 image data to a TIFF file is not supported with IMWRITE.  Use Tiff instead.  Type "help Tiff" for more
information.

Error in imwrite (line 472)
        feval(fmt_s.write, data, map, filename, paramPairs{:});

提示我們使用Tiff來實現。

(1) 對於單幅Tiff影象,例如data是一幅二維影象(50*60)

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

t.setTag('ImageLength', 50); 
t.setTag('ImageWidth', 60); 
t.setTag('Photometric', Tiff.Photometric.MinIsBlack);
t.setTag('BitsPerSample', 32); % 32 bit
t.setTag('SamplesPerPixel', 1);
t.setTag('PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);
t.write
(data); t.close;

(2) 對於寫入多幅tiff影象,例如data是一個三維影象(50*60*100)

for j = 1:1:100 % 100層
    if j == 1
        t = Tiff('test.tif','w');
    else
        t = Tiff('test.tif', 'a');
    end
    t.setTag('ImageLength', 50);
    t.setTag('ImageWidth', 60);
    t.setTag('Photometric', Tiff.Photometric.MinIsBlack);
    t.
setTag('BitsPerSample', 32); t.setTag('SamplesPerPixel', 1); t.setTag('PlanarConfiguration', Tiff.PlanarConfiguration.Chunky); t.write(data(:,:,j)); t.close; end