MATLAB: How to convert high pass filtered image to grayscale image and save

enhancementhighpass filter

I am using Matlab R2018a and i am using a x ray image (skull) as my input image.I want to convert my final image (B2) to grayscale image.Can you please suggest a method for this?
And also i want to save my final result.When i am using saveas it gives me an error.Can you please help me to solve this issue? Here is the code that i have run so far.
close all;
clear all;
clc;
img = imread('055.bmp');
img2 = imnoise(img,'salt & pepper',0.025);
img3 = img2;
for c = 1 : 3
img3(:, :, c) = medfilt2(img2(:, :, c), [5, 5]);%add median filter
end
I=rgb2gray(img3); % convert the image to grey
A = fft2(double(I)); % compute FFT of the grey image
A1=fftshift(A); % frequency scaling
% Gaussian Filter Response Calculation
[M, N]=size(A); % image size
R=15; % filter size parameter
X=0:N-1;
Y=0:M-1;
[X, Y]=meshgrid(X,Y);
Cx=0.3*N;
Cy=0.3*M;
Lo=exp(-((X-Cx).^2+(Y-Cy).^2)./(2*R).^2);
Hi=1-Lo; % High pass filter=1-low pass filter
% Filtered image=ifft(filter response*fft(original image))
J=A1.*Lo;
J1=ifftshift(J);
B1=ifft2(J1);
K=A1.*Hi;
K1=ifftshift(K);
B2=ifft2(K1);
%----visualizing the results----------------------------------------------
figure(1)
subplot(2,2,1)
imshow(I);colormap gray
title('Original image')
subplot(2,2,2)
imshow(img3);colormap gray
title('Median Filterd image')
subplot(2,2,3)
imshow(abs(A1),[-12 300000]), colormap gray
title('fft of original image')
subplot(2,2,4)
imshow(abs(B2),[12 290]), colormap gray
title('High pass filtered image')
figure(2)
subplot(1,3,1);
imshow(img)
title('Original image')
subplot(1,3,2);
imshow(img3)
title('Median filtered image')
subplot(1,3,3);
imshow(abs(B2),[12 290])
title('High pass filtered image')

Best Answer

I've not tried to understand your code, but it appears to me that B2 is derived from I which is already a greyscale image. Therefore, isn't B2 already greyscale?
If not, you seem to know already which function to use, it's rgb2gray which you already use in your code. So, it's unclear why you're asking the question.
As for saving the image, use imwrite. saveas is for saving the figure (as a fig file), not the raw image displayed in the figure.