4.9 | 8 ratings Rate this file 42 Downloads (last 30 days) File Size: 6.69 KB File ID: #35684
image thumbnail

Save and load data as multi-frame TIFF format



16 Mar 2012 (Updated )

Export various types of data such as single, double or 16bit integer as TIFF format

| Watch this File

File Information

[X,Y,Z] = peaks(100);
X = single(X);
Y = single(Y);
Z = single(Z);
Z_index = uint8((Z - min(Z(:))) * (255 / (max(Z(:)) - min(Z(:)))));
Z_color = uint8(ind2rgb(Z_index, hsv(256)*256));
Z_color_multiframe = reshape([Z_color(:)*0.2 Z_color(:)*0.6 Z_color(:)], 100, 100, 3, 3);
Z_color_noisy = uint8(single(Z_color) + rand(100, 100, 3).*50);
% 8-bit, grayscale image
clear options;
saveastiff(uint8(Z_index), 'Z_uint8.tif');

% Lossless LZW compression
clear options;
options.comp = 'lzw';
saveastiff(uint8(Z_index), 'Z_uint8_LZW.tif', options);

% Ask a question if the file is already exist
clear options;
options.ask = true;
saveastiff(uint8(Z_index), 'Z_uint8_LZW.tif', options);

% Allow message printing.
clear options;
options.message = true;
saveastiff(uint8(Z_index), 'Z_uint8_LZW.tif', options);

% 16-bit, grayscale image
clear options;
saveastiff(uint16(Z_index), 'Z_uint16.tif');

% 32-bit single, grayscale image
clear options;
saveastiff(Z, 'Z_single.tif');

% RGB color image
clear options;
options.color = true;
saveastiff(Z_color, 'Z_rgb.tif', options);

% Save each R, G and B chanels of the color image, separately.
clear options;
saveastiff(Z_color, 'Z_rgb_channel.tif');

% Save the multi-frame RGB color image
clear options;
options.color = true;
saveastiff(Z_color_multiframe, 'Z_rgb_multiframe.tif', options);

% Save the noise-added RGB color image
clear options;
options.color = true;
saveastiff(Z_color_noisy, 'Z_rgb_noisy.tif', options);

% 32-bit single, 50x50x50 volume data
clear options;
saveastiff(single(rand(50, 50, 50)), 'volume_50x50x50.tif');

% Append option is ignored if path dose not exist.
clear options;
options.append = true;
saveastiff(Z_index, 'Z_uint8_append.tif', options);

% You can append any type of image to an existing tiff file.
clear options;
options.append = true;
saveastiff(single(rand(10, 10, 3)), 'Z_uint8_append.tif', options);
options.color = true;
saveastiff(Z_color_multiframe, 'Z_uint8_append.tif', options);

% Standard TIFF can not save 4GB files.
clear options;
options.message = true;
saveastiff(single(zeros(24000, 24000)), 'BigTiff(2GB+2GB).btf');
options.append = true;
saveastiff(single(zeros(24000, 24000)), 'BigTiff(2GB+2GB).btf', options); % Error: Maximum TIFF file size exceeded

% Save 4GB files using 'big' option.
clear options;
options.big = true; % Use BigTIFF format
options.comp = 'lzw';
saveastiff(single(zeros(24000, 24000)), 'BigTiff(2GB+2GB).btf', options);
options.append = true;
saveastiff(single(zeros(24000, 24000)), 'BigTiff(2GB+2GB).btf', options); % 4GB Big TIFF file

% Load multiframe tiff
multiframe = loadtiff('volume_50x50x50.tif');
complexframe = loadtiff('Z_uint8_append.tif');
bigtiff = loadtiff('BigTiff(2GB+2GB).btf');

Required Products MATLAB
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
21 Sep 2014 Eyal

Append only works when ask is enabled...

15 Sep 2014 Urs Utzinger

Change/add following code for big tiff support in saveastiff.m. Big tiff uses 64 bit addressing and allows for files >2 Gbyte.

Comment Section:
% options.big
% : true or FALSE,
% use big tiff format

Argument parsing section:

if isfield(options, 'big') == 0
options.big = false;

Open output file section:

if ~options.append
if ispc
tempfile = '~$temporal.tif';
tempfile = '._temporal.tif';
if s.bytes > 2^32-1 || options.big
t = Tiff(tempfile, 'w8');
t = Tiff(tempfile, 'w');

18 Mar 2014 Dmitry Sorokin  
08 Mar 2014 Jose Rosado  
19 Oct 2013 Shalin Mehta

This function provides approx. 10x speed up on a windows machine as compared to imwrite(..'append') loop. But, on mac (I presume also on Unix) the call to fileattrib (fileattrib(tempfile, '+h +w', '', 's');) throws an exception. saveastiff catches this exception and deletes the temp file without saying anything.

The exception occurs because +h (hidden) attribute is not valid on mac. Removing +h and -h from the calls to fileattrib fixes the problem.

29 Nov 2012 YoonOh Tak

Yes, Jurgen. You need Matlab 2009b or later.

29 Nov 2012 Jurgen

Oh nvm I don't have the Tiff class :(

29 Nov 2012 Jurgen

Doens't work for me, using MATLAB 2008a. Dont get any error but there is no savefile in my current directory.

21 Nov 2012 Weicheng Kuo

Fantastic performance!! :) Can't help but create an account to recommend it!

21 Aug 2012 Casey Ta

Very nice. I had to manually add support for PackBits, but this was very easy to do (compression tag value 32773). I did happen to notice one typo on line 202 where "optoins" should be "options".

15 Jun 2012 Andrew

So much faster than imwrite(foo, ; 'writemode','append') for large (500+ page) tiffs. Thank You!

16 May 2012 Mathew Thomas  
19 Mar 2012

Support lossy JPEG Compression.
bug fixed.

06 Apr 2012

There was a bug in the example.
  [X,Y,Z] = peaks(100);
is modified to
  [X,Y,Z] = single(peaks(100));

09 May 2012

Parameter 'options' is added.

10 May 2012

bug fix

11 May 2012

Bug fix
Now you can append an image data to an existing tiff file.
And any type of data can be appended to an existing file.

11 May 2012

Bug fixed.
Now, it supports 'double' precision.

27 Aug 2012

Ttypographical error on line 202 : optoins -> options
Thank you, Casey Ta.

20 Nov 2012

Bug fix: It crashed when the file is already opened.

06 Sep 2013

bug fix

21 Oct 2013

Bug fix.
Thank you, Dr. Shalin Mehta.
I added some 'ispc' codes to avoid the exception on mac. But I'm sorry that I couldn't test this version because I don't have mac os.

16 Sep 2014

Bug fix and add 'big' option for BigTIFF format (Thanks to Urs Utzinger).

22 Sep 2014

There was a mistake in the version 2.5, and I fixed it. Thank you, Eyal.

Contact us