Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

saving data as tiff

157 views
Skip to first unread message

Mathew Thomas

unread,
Nov 2, 2011, 2:40:27 AM11/2/11
to
Hi All,

I have a 100 X 100 array (values between 0.1 and 3) in the MATLAB workspace that I am trying to save as Tiff file using the following code:
imwrite(abs_smooth,'outfile', 'tif', 'Compression','none')

Two things I noticed.
1) The size of the data reduces from 7 Mb to 1.5 Mb.
2) The values are now around the range of 255 when I load the tiff file back into MATLAB.

I am not sure what is going on here.

Thanks in advance.

Mathew

Steven_Lord

unread,
Nov 2, 2011, 9:39:07 AM11/2/11
to


"Mathew Thomas" <math...@gmail.com> wrote in message
news:j8qokr$bie$1...@newscl01ah.mathworks.com...
> Hi All,
>
> I have a 100 X 100 array (values between 0.1 and 3) in the MATLAB
> workspace that I am trying to save as Tiff file using the following code:
> imwrite(abs_smooth,'outfile', 'tif', 'Compression','none')
>
> Two things I noticed.
> 1) The size of the data reduces from 7 Mb to 1.5 Mb.

This is related to the cause of the second issue you mentioned:

> 2) The values are now around the range of 255 when I load the tiff file
> back into MATLAB.

Correct. From the Class Support section of the documentation for IMWRITE:

http://www.mathworks.com/help/techdoc/ref/imwrite.html

"The class of the image written to the file depends on the format specified.
For most formats, if the input array is of class uint8, imwrite outputs the
data as 8-bit values. If the input array is of class uint16 and the format
supports 16-bit data (JPEG, PNG, and TIFF), imwrite outputs the data as
16-bit values. If the format does not support 16-bit values, imwrite issues
an error. Several formats, such as JPEG and PNG, support a parameter that
lets you specify the bit depth of the output data.

If the input array is of class double, and the image is a grayscale or RGB
color image, imwrite assumes the dynamic range is [0,1] and automatically
scales the data by 255 before writing it to the file as 8-bit values."

Your original data, abs_smooth, was a double array. What you received when
you used IMREAD to import the data was probably uint8 data, the scaled
version of your double data that was written by IMWRITE. Since 3, the upper
limit of your original data, is outside the dynamic range [0 1] it saturated
at intmax('uint8') or 255 when it was converted to a uint8. Doubles are
larger than uint8's which explains the difference in the size of your data.


Your data doesn't fit the definitions of which I'm aware for grayscale, RGB,
or indexed images; what were you expecting to receive when you converted it
to a TIFF? With that information someone may be able to offer a suggestion
on how to accomplish that goal.

http://www.mathworks.com/help/techdoc/creating_plots/f2-10709.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Mathew Thomas

unread,
Nov 2, 2011, 12:27:13 PM11/2/11
to
Thank you for your reply. I was expecting to see the actual values that I see in the MATLAB workspace. I do some filtering on the input data and need to backproject it. But I am loosing a lot of details because of how it is being saved. For e.g., I1 is between 0 and 1.5 now. Is there any way to keep these values the same when the data is saved as a Tiff instead of it getting scaled between 0-255 ?

Thank you.
Mathew

John

unread,
Nov 2, 2011, 12:48:12 PM11/2/11
to
"Mathew Thomas" wrote in message <j8rr11$ng$1...@newscl01ah.mathworks.com>...
> Thank you for your reply. I was expecting to see the actual values that I see in the MATLAB workspace. I do some filtering on the input data and need to backproject it. But I am loosing a lot of details because of how it is being saved. For e.g., I1 is between 0 and 1.5 now. Is there any way to keep these values the same when the data is saved as a Tiff instead of it getting scaled between 0-255 ?
>
> Thank you.
> Mathew

If you absolutely need to save a floating point image in a TIFF and retrieve it as a floating point image, then you should use the Tiff class (available in R2009b) instead of IMWRITE to create the image.

For example

data = rand(25,30);
t = Tiff('myfile.tif','w');
t.setTag('Photometric',Tiff.Photometric.MinIsBlack); % assume grayscale
t.setTag('BitsPerSample',64);
t.setTag('SamplesPerPixel',1);
t.setTag('SampleFormat',Tiff.SampleFormat.IEEEFP);
t.setTag('ImageLength',size(data,1));
t.setTag('ImageWidth',size(data,2));
t.setTag('RowsPerStrip'
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
t.write(data);
t.close();

IMREAD will then reread the data as floating point.

John

unread,
Nov 2, 2011, 1:44:31 PM11/2/11
to
"John " <com.wor...@evans.john> wrote in message

Oops, that RowsPerStrip line was a cut-and-paste error. The steps should read

data = rand(25,30);
t = Tiff('myfile.tif','w');
t.setTag('Photometric',Tiff.Photometric.MinIsBlack); % assume grayscale
t.setTag('BitsPerSample',64);
t.setTag('SamplesPerPixel',1);
t.setTag('SampleFormat',Tiff.SampleFormat.IEEEFP);
t.setTag('ImageLength',size(data,1));
t.setTag('ImageWidth',size(data,2));
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
t.write(data);
t.close();

Mathew Thomas

unread,
Nov 2, 2011, 2:40:26 PM11/2/11
to
Perfect ! Thanks John and Steve.

"John " <com.wor...@evans.john> wrote in message <j8rvhv$gsr$1...@newscl01ah.mathworks.com>...

Rizwan

unread,
Apr 2, 2014, 7:13:12 AM4/2/14
to
"Mathew Thomas" wrote in message <j8s2qq$se4$1...@newscl01ah.mathworks.com>...
I am trying to run the same code but getting error "Undefined function or method 'Tiff' for input arguments of type 'char'."

Steven Lord

unread,
Apr 2, 2014, 10:10:40 AM4/2/14
to

"Rizwan " <rizwank...@yahoo.com> wrote in message
news:lhgrc8$1l5$1...@newscl01ah.mathworks.com...
My suspicion is that you're using a version of MATLAB that predates the
introduction of that feature. What version are you using?

anika hossain

unread,
Sep 17, 2015, 1:35:09 AM9/17/15
to
is there any way to save it in png/jpg/bmp?

Walter Roberson

unread,
Sep 17, 2015, 4:02:09 AM9/17/15
to
"anika hossain" wrote in message <mtdje7$71a$1...@newscl01ah.mathworks.com>...
> is there any way to save it in png/jpg/bmp?

No. None of those formats support floating point.
0 new messages