Saving gray scale image
Show older comments
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
More Answers (1)
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
on 29 Jul 2020
Edited: Babu Sankhi
on 29 Jul 2020
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
on 29 Jul 2020
Edited: Babu Sankhi
on 29 Jul 2020
Categories
Find more on Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
