# How can I convert high pass filtered image to grayscale image and save?

8 views (last 30 days)
Nayana R on 20 Nov 2018
Commented: Guillaume on 20 Nov 2018
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;
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')

Guillaume on 20 Nov 2018
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.
Nayana R on 20 Nov 2018
When I tried to do contrast adjustment it gives an error as 'Expected input number 1, I or RGB, to be real.'.That is why i tried to convert highpass filtered image to RGB.How can I solve this issue and I want to save my final result(contrast adjusted image).can you please suggest a method to solve this issue?
Guillaume on 20 Nov 2018
From the error, I gather that your image has some complex values probably as the result of your fft and ifft. You need to investigate why you still have complex values. Converting to RGB won't help you if the input is garbage.
You could convert your complex image to real by removing the complex part (with real), taking the magnitude of the complex number (with abs) or any other method that you deem appropriate but first you need to understand why you have complex numbers.