Saving gray scale image

조회 수: 63 (최근 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
Image Analyst
Image Analyst 2020년 7월 30일
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 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
Image Analyst
Image Analyst 2020년 7월 29일
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 2020년 7월 29일
편집: Babu Sankhi 2020년 7월 29일
I am sorry, I mean the saved image is not like attached image ( image I want .png).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 3-D Volumetric Image Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by