Q: Can someone explain why imagesc and imwrite differ?
Show older comments
Hello,
Looked at quite a lot places but could not find an answer for this one. Could someone explain why the three images are different? Also, what is the best way to save the image shown from the imagesc(), and is this image the real representation of the data (given that I have used map in imwrite)?
I don't understand why this fails to represent the image from the figure..
NOTE: the smallest picture is the one that is shown in the matlab file explorer tree when the file is selected!
Code used: in imwrite)?
slice = imagesc(func);
map = colormap('gray');
imwrite(slice,map,'slice.png');

Answers (1)
Image Analyst
on 4 Feb 2017
0 votes
imwrite() take the array and writes it to a disk file. image(), imagesc(), and imshow() do not do that -- they display the image instead. By default, imagesc() scales the image and applies a funky colormap, while the others do not.
If you want, you can scale the image prior to saving with imadjust().
7 Comments
Nik
on 4 Feb 2017
Image Analyst
on 4 Feb 2017
I think the most accurate for a uint8 gray scale image is to use gray(256), which you can get by passing in [0,255] to imshow().
imshow(grayImage, [0,255]);
colorbar;
Of course if your image is only in the 0-30 gray level range it would be very dark so in that case you might want to use [];
imshow(grayImage, []);
Nik
on 4 Feb 2017
Image Analyst
on 4 Feb 2017
It just depends on what you want to save, and what you want to display. Let's say that you have a uint8 image and the gray levels range from 80-130. If you use imshow(grayImage, []), it will display 80 as 0 (black) and 130 as 255 (white) and everything between 80 and 130 scaled linearly. So it will look both brighter and darker and have more contrast and dynamic range than the low contrast original. If you use imshow(grayImage) or imshow(rgayImage, [0 255]) it will show gray levels as they actually are so 80 stays 80 and 130 stays 130 and the image looks like the low contrast image that it really is. If you call imwrite(grayImage) it will save the image with the actual gray levels that they are. If you send in a colormap along with the image into imwrite() that merely saves the colormap along with the image but it does not apply the colormap to change the image before it's stored. It's still stored with the original gray levels. It just keeps the map together with it so that you can recall the image later with the map that you used before you saved it.
Walter Roberson
on 4 Feb 2017
If you have a matrix of floating point values in the range 0 to 1, then im2uint16() converts to 16 bit that some imwrite() can handle in png or tiff. (JPEG as well, but if you want to do science on the image avoid JPEG).
Image Analyst
on 4 Feb 2017
What did your image start out as, before you tried to convert it to uint?
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!