Save image as displayed without changing color scale?

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

What code are you using to save it and what data type and data range is the data in the image?
I use imwrite to save the image data, and data range of the data in the image is around 0 - 3000. but there are a few bright sparks in the image (20000-50000),which automatically scales up the color bar.
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.
Here is an example of my code:
image = imread('sca_r1.png');
final = ...(image);
imagesc(final)
colormap gray;
axis equal;axis off;
set( gca,'Visible','off');
set(gca,'position',[0 0 1 1],'units','normalized');
imwrite(final,'test.png','mode','lossless');
The detailed analysis is omitted above.
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.
well, I would not describe it as a problem, the all analysis process has nothing to do with the color scale of the image. all I need is to fix the range of colorbar when the image is saved. The original data for the image is text data of a matrix, so basically is indexed image, so the contrast can be adjusted by changing the range of colorbar.
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.

Sign in to comment.

Answers (1)

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

I 've tried normalize the image to PNG in the range of 0-255, it doesn't help. The colorbar is still autoscaled from the darkest pixel to the brightest pixel. Let me make the question a bit more clear, I have a 1000x1000 pixel images.The pixel values of the all image is around 3000, but there are a few sparks have pixel values above 50000. so when the image is plotted by imagesc, the upper value of the colorbar autonmatically scales up to 50000. consequently the whole image is completely dark, except the few sparks. Even though I can manually re-scale the colorbar to make the contrast good enough to see the features in image,the saved PNG still in the default colorbar range.
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.
Thanks for your advice, it helps a lot. one more question, when I plot the image using imagesc, it seems the image is compressed. let's say the original image data is 5000x5000 pixels, when it's ploted and saved as a PNG image, it's only 561x420 pixels.Is there a way to save the image without compressing it? Pixel information is important for my later analysis.
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.
No, saving raw data still have the scale problem. what I am doing now is plot the image, manually change the range of the colorbar until I see clear features on the image, then save it as PNG right from the plot frame. but the plotted image has been compressed.
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.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 26 Apr 2017

Commented:

on 27 Apr 2017

Community Treasure Hunt

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

Start Hunting!