Q: Can someone explain why imagesc and imwrite differ?

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');

Réponses (1)

Image Analyst
Image Analyst le 4 Fév 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 commentaires

Nik
Nik le 4 Fév 2017
Thanks for the clarification! Then, what would be the best visual representation of the data, most accurate? (since imagesc(),as you said, applies funky colormap and I apply colormap to the imwrite)
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
Nik le 4 Fév 2017
using the imshow(grayImage, []) brithens it a little bit, but still the one saved when opened with a photo view program shows it quite dark. I guess I will have to spend more time reading the docs. Thank you!
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.
Nik
Nik le 4 Fév 2017
Modifié(e) : Nik le 4 Fév 2017
Well, what I am doing is reproducing an image from a model. When I tried first time to save the image, it showed "matlab.graphs.primitive.Image" error. Then I found that I can convert the image either to uint8 or 16, but 16 was not possible, so I used 8, in order to write it to the hard drive. Which shows the far right image, the darker one. So I was wondering, if the way I am showing the unsaved image (middle pic) or the way I am saving it to the hard drive (most right one) causes the difference. If I got you correct, when I save an image with a mask 'x', I have to display it with the exact same mask. That then could explain why the image on the most left is shown that way, closer to middle, if you can call it, actual image. Thank you for your time, trying to explain what is going on!
EDIT: But if I am concerned that the image displayed is not exactly the same as the one meant to be saved, I can always do the absdiff() between them, to see if there is any. Should have done that earlier,before posting, sorry for the hassle!
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).
What did your image start out as, before you tried to convert it to uint?

Connectez-vous pour commenter.

Question posée :

Nik
le 4 Fév 2017

Commenté :

le 4 Fév 2017

Community Treasure Hunt

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

Start Hunting!

Translated by