Save image as displayed without changing color scale?
Show older comments
Hello there! I am using Matlab to process my image data. but when I save the processed image to file, the colorscale changes and it's too dim to see the features in image. Is there a way to fix the colorscale when save image to file? Appreciated for any suggestions.

7 Comments
Adam
on 26 Apr 2017
What code are you using to save it and what data type and data range is the data in the image?
Raul
on 26 Apr 2017
Adam
on 26 Apr 2017
It would still help to know the exact code though. e.g. are you saving the map with it or just the data? I assume it is an indexed image rather than true RGB since you talk about the colourbar.
Adam
on 26 Apr 2017
Well, it all depends what this line is supposed to mean:
final = ...(image);
The rest of the lines between that and the imwrite instruction have no bearing on it and without the above line you are basically just reading and image and then writing the same image back out again. Does doing that give you the same image as you would expect? If it does then whatever the above line is supposed to mean is where the problem is.
Raul
on 26 Apr 2017
Adam
on 26 Apr 2017
imwrite just writes the raw data. It is unaffected by the colourbar or any other settings you have when you view that data on an axes. The only thing that would affect it is if you save the colourmap with the image.
Answers (1)
Image Analyst
on 26 Apr 2017
Try to normalize it to PNG in the range 0-255.
% Normalize final
final = uint8(255.0 * mat2gray(final));
% Now do the write.
imwrite(final,'test.png');
Note: PNG is inherently lossless and imwrite() ignores the 'mode', 'lossless' for PNG files, so I omitted it.
6 Comments
Raul
on 26 Apr 2017
Image Analyst
on 26 Apr 2017
imwrite() will write an image with values between 0 and 255. The 3000 value should get mapped to 0 (if 3,000 is the min value), and 50000 will get mapped to 255 (if 50,000 is the brightest). Once you have the image in the range 0-255 both imshow() and imagesc() should show the entire range of 0-255, though imagesc() will apply some funky colormap by default until you change it. You can use a normal gray colormap.
If your image is an indexed image ("so basically is indexed image") then the "intensity" of the image is not really an intensity - it's an index into a colormap. In that case, to apply the colormap to change the pixels, you need to use ind2rgb().
If you want the currently displayed image, including the effect of any colormap or zoom level, then you can use getframe(). That will give you the displayed image, colormap and all, as an RGB image. It will not give you the underlying image, so if your image is uint16 or double, you will not get that but you will get a 8*3 = 24 bit RGB image exactly as displayed.
That said, I'm still not exactly sure what you want.
Raul
on 27 Apr 2017
Adam
on 27 Apr 2017
If you are saving the raw data then how you plot it is irrelevant and unconnected to the saving, it should still save the same size as the original.
Raul
on 27 Apr 2017
Adam
on 27 Apr 2017
Well if you are saving a plot then that isn't the raw data and it will be pixelised according to screen resolution and size of the figure etc.
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!