MATLAB Answers

Saving gray scale image

41 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;
figure(3);
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');
end
But saved images are not gray. Can you please help me ??
Thank you

  0 Comments

Sign in to comment.

Accepted Answer

jonas
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().

  8 Comments

Show 5 older comments
Babu Sankhi
Babu Sankhi on 29 Jul 2020
I am sorry I really confused you both.
Thanks Jonas your code worked now,that is what I want to be saved.
Thank you very much for your great help
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);
imshow(testI);
title('testI Image', 'FontSize', 20);
subplot(2, 2, 2);
imshow(testI);
title('sat Image', 'FontSize', 20);
subplot(2, 2, 3);
imshow(I_new);
title('I_new Image', 'FontSize', 20, 'Interpreter', 'none');
subplot(2, 2, 4);
imshow(recalledImage);
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.
else
fprintf('%s is RGB.\n', filename);
end
end

  3 Comments

Babu Sankhi
Babu Sankhi on 29 Jul 2020
Thank you analyst,
It still saves different image (black and white) that I don't want.The two dimensional testI(:,:,k).mat and sat.mat files attahced above. The png gray color map which I want to be saved is also attached above.
Can you please help me now?
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
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!

Translated by