Clear Filters
Clear Filters

Saving gray scale image

95 views (last 30 days)
Babu Sankhi
Babu Sankhi on 28 Jul 2020
Commented: Babu Sankhi on 30 Jul 2020
Hi all,
I want to save my image in gray scale. Of course I can display the gray image by using code;
imagesc ((testI(:,:,1)));colormap gray
But I want those images to be saved and I tried this way;
for k=1
I1 =medfilt2(testI(:,:,k)-sat);%actual
filename=sprintf('kalyan%02d.png', k);%saves all files
imagesc(I1); axis image; colorbar; colormap(gray);
imwrite(I1, filename, 'png');
But saved images are not gray. Can you please help me ??
Thank you

Accepted Answer

jonas on 28 Jul 2020
Edited: jonas on 28 Jul 2020
Using a single color channel to convert your image to grayscale is not optimal. The gray tone is usually a combination of red, blue and green. Better use rgb2gray() instead
imwrite(rgb2gray(I1), filename, 'png');
If you really want to save the gray image based on a single channel, then just pass that channel as input to imwrite().
Image Analyst
Image Analyst on 30 Jul 2020
Well my code would have worked too, but I'm not sure our code did anything different than what you did. Jonas did scale it from 0-1, or 0-255 when read back in from the saved file, so maybe that's what you were wondering about. You wanted it to be scaled rather than the actual values. Though using imagesc() or using brackets in imshow(I_new, []) would also show it as scaled, but without changing the actual filtered values.
Also the code can be simplified by removing unnecessary arguments which are just sending in the defaults:
% Load variables from mat files.
out = load('test.mat');
testI = out.bab;
out = load('sat.mat');
sat = out.babu;
% Take the median filter of the difference image.
I1 = medfilt2(testI - sat); % testI and sat should be double.
% Make 2d array into double grayscale image in the range 0-1.
I_new = mat2gray(I1);
% Save output image to disk.
k = 1; % Whatever.
filename = sprintf('kalyan%02d.png', k); % Create output filename.
imwrite(I_new, filename); % Save file to disk.
% imwrite() will convert the image in the range 0-1 to 0-255 as you'll see in the recalled image.
% Read it back in to see if it's the same since you were concerned about that.
recalledImage = imread(filename);
% Display everything:
subplot(2, 2, 1);
title('testI Image', 'FontSize', 20);
subplot(2, 2, 2);
title('sat Image', 'FontSize', 20);
subplot(2, 2, 3);
title('I_new Image', 'FontSize', 20, 'Interpreter', 'none');
subplot(2, 2, 4);
title('Recalled Image', 'FontSize', 20);
Make sure the values are double before you subtract them because it you subtract uint8 images, any values that would be negative will get clipped to 0, so you'd need to cast to double. But it depends on what you want, maybe you want the absolute value of the images in which case you can use imabsdiff()
I1 = medfilt2(imabsdiff(testI, sat));
in which case you don't need to cast to double since that's done internally by imabsdiff().
Babu Sankhi
Babu Sankhi on 30 Jul 2020
ok thank you analyst.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 Jul 2020
Edited: Image Analyst on 29 Jul 2020
What is sat? A scalar? I1 should be gray scale because you took it as the red channel of testI. In fact, this code shows they are gray scale:
testI = imread('Peppers.png'); % Read in sample RGB image.
sat = 10;
for k = 1 % Red channel ONLY
I1 =medfilt2(testI(:,:,k)-sat);%actual
filename=sprintf('kalyan%02d.png', k);%saves all files
imagesc(I1); axis image; colorbar; colormap(gray);
imwrite(I1, filename);
% Recall
recalledI = imread(filename);
[rows, columns, numberOfColorChannels] = size(recalledI);
if numberOfColorChannels == 1
fprintf('%s is grayscale.\n', filename); % This is what prints.
fprintf('%s is RGB.\n', filename);
Image Analyst
Image Analyst on 29 Jul 2020
I really don't know what you want. You say it saves a gray scale image and that you don't want that but then the "image I want.png" is a gray scale image. Plus, your original variables bab and babu in the mat files are also gray scale. Plus there is no colormap at all saved in the mat files, just two images.
Babu Sankhi
Babu Sankhi on 29 Jul 2020
Edited: Babu Sankhi on 29 Jul 2020
I am sorry, I mean the saved image is not like attached image ( image I want .png).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!