MATLAB Answers

Saving gray scale image

219 ビュー (過去 30 日間)
Babu Sankhi
Babu Sankhi 2020 年 7 月 28 日
コメント済み: Babu Sankhi 2020 年 7 月 30 日
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

採用された回答

jonas
jonas 2020 年 7 月 28 日
編集済み: jonas 2020 年 7 月 28 日
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 件のコメント
Babu Sankhi
Babu Sankhi 2020 年 7 月 30 日
ok thank you analyst.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 7 月 28 日
編集済み: Image Analyst 2020 年 7 月 29 日
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 件のコメント
Babu Sankhi
Babu Sankhi 2020 年 7 月 29 日
I am sorry, I mean the saved image is not like attached image ( image I want .png).

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by