Saving gray scale image

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

 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

I am sorry it gives error like this. Still I could not figure out.
Error using rgb2gray>parse_inputs (line 80)
MAP must be a m x 3 array.
Error in rgb2gray (line 52)
isRGB = parse_inputs(X);
Error in PMA17exppulse (line 71)
imwrite(rgb2gray(I1), filename, 'png');
It seems you do not have an image to start with but instead a 2d array with some values. When you pass this to imagesc, the function will display a scaled image where your max value is white and min value is black, or vice versa. You can get this scaled data as follows:
figure
I_new = mat2gray(I1,[min(I1(:)) max(I1(:))])
imshow(I_new)
I guess that is what you want to export?
Babu Sankhi
Babu Sankhi on 29 Jul 2020
Edited: Babu Sankhi on 29 Jul 2020
my starting image is test(:,:,1) and the one which I want to save as gray image png are attached above.
Can you help me now?
Just to be clear. Your variable "test" has one layer, as it is 2D. These are therefore equal
test(:,:,1) == test(:,:,:) == test
The fact that you write test(:,:,1) is the reason for my confused answer. I thought the original image was RGB.
I already gave you everything you need, but here is the working code.
%load variables
out = load('test.mat');
testI = out.bab;
out = load('sat.mat');
sat = out.babu;
I1 =medfilt2(testI-sat); %some new 2d array
%make 2d array into actual grayscale image
I_new = mat2gray(I1,[min(I1(:)) max(I1(:))]);
%save
filename=sprintf('kalyan%02d.png', k);%saves all files
imwrite(I_new, filename, 'png');
Yeah, I gave him essentially the same thing (expect for the third argument you have in imwrite which is not needed), but he said he did not want that. So now I'm beginning to think he wants the image with the tickmarks on the axis outside the image. To get those he should use saveas() which saves a bitmapped figure, with tick marks, axis labels, titles, any graphics drawn in the overlay, etc.
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
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().
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
Edited: 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?
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.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 28 Jul 2020

Commented:

on 30 Jul 2020

Community Treasure Hunt

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

Start Hunting!