Saving gray scale image

100 vues (au cours des 30 derniers jours)
Babu Sankhi
Babu Sankhi le 28 Juil 2020
Commenté : Babu Sankhi le 30 Juil 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

Réponse acceptée

jonas
jonas le 28 Juil 2020
Modifié(e) : jonas le 28 Juil 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 commentaires
Image Analyst
Image Analyst le 30 Juil 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 le 30 Juil 2020
ok thank you analyst.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 28 Juil 2020
Modifié(e) : Image Analyst le 29 Juil 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 commentaires
Image Analyst
Image Analyst le 29 Juil 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 le 29 Juil 2020
Modifié(e) : Babu Sankhi le 29 Juil 2020
I am sorry, I mean the saved image is not like attached image ( image I want .png).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Red dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by