MATLAB Answers

Imwrite creates 8bit tif file despite 16bit input

11 views (last 30 days)
I have several 16bit images in a 3D array A. I want to save those images as one tiff file. However when using imwrite the images are stored as 8bit file.
The code is:
imwrite(A(:,:,1),'folderpath/test123.tif');
for n = [2:1:amount_of_images]
imwrite(A(:,:,n),'folderpath/test123.tif','WriteMode','append');
end
In the target folder one 8bit tif file appears, which is copletly white since all values in the original image are above 2^8-1
In the "current folder section" on the left the tiff file is also declared as 8bit. Judging by the documentation imwrite should recognise the 16bit, right? What am I doing wrong?
P.S. I am using R2019b

  2 Comments

Malte Römer-Stumm
Malte Römer-Stumm on 6 Aug 2020
When I type class(A) in the command window, it just says double. Is that what you are asking for?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Aug 2020
  • If A is a grayscale or RGB color image of data type double or single, then imwrite assumes that the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values. If the data in A is single, convert A to double before writing to a GIF or TIFF file.
If your data is in the range 0.0 to 65535.0 (double) then you should be using uint16() to convert it to 0 to 65535 uint16, in which case the earlier clause would apply:
  • If A is of data type uint16 and the output file format supports 16-bit data (JPEG, PNG, and TIFF), then imwrite outputs 16-bit values. If the output file format does not support 16-bit data, then imwrite returns an error.
If your data is in the range 0.0 to 1.0 but you know has 16 bits worth of information, then you should use im2uint16() to create a uint16 array, in which case the above paragraph would apply.
It is quite common for people to routinely double() the results of imread() for processing purposes. In most cases, users should instead use im2double() which would automatically scale to the 0 to 1 range.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by