Converting Map Figure into TIFF file without background
5 views (last 30 days)
Show older comments
Hello Community!
So I have a 300 by 500 by 1 data set that corresponds to a scalar colormap plot. In the figure, I hid all of the titles, axes, etc., so that the figure only shows the colormap. I would like to save this plot such that the image retains its 300 by 500 pixel dimensions, and it is lossless.
However, when saving as a TIFF file, MATLAB adds a white background, making the image larger in size.
I tried using imwrite (which allows me to save images without the background), however I am unable to save as a TIFF because my data type is incompatible. Upon converting to a double type data, the resulting TIFF image showed a binary map. I have tried other file types, but they are too lossy (I can only choose bitdepth of 8, when 16 or higher would be preferred).
Any suggestions or advice would be greatly appreciated!
2 Comments
Accepted Answer
Walter Roberson
on 1 Nov 2017
Floating point tiff: https://www.mathworks.com/matlabcentral/answers/7184-how-can-i-write-32-bit-floating-point-tifs-with-nans and there is worked-out code at http://www.mathworks.com/matlabcentral/fileexchange/35684-multipage-tiff-stack
However, you mention colormap, from a 300 by 500 by 1 data set. That implies you are using pseudocolor and that you want the color image saved. You probably did not build of colormap of more than 256 colors, but you might have.
Under your circumstances what I would do is use mat2gray() to scale your data to the range 0 to 1.
If your colormap is 256 entries or less, use im2uint8() on the scaled data, and then you can imwrite() that, passing in a copy of your color map as the second parameter.
If your colormap is more than 256 entries, use im2uint16() on the scaled data, and then you can imwrite() that, passing in a copy of your color map as the second parameter. I find that PNG is a bit better for working with colormaps that are more than 256 but less than 65536 entries, as PNG is fine with writing just as many entries as needed (some other formats need one entry for each possible binary value.)
What does not seem to be possible is to create a color image while storing your original float data -- not unless one creates multiple data-sets in the same file and uses one for display and the other for the data. (TIFF does have a pseudocolor mode but it is for indexed images with integer values stored.)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!