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

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')

 Réponse acceptée

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.

5 commentaires

Thank you Guillaume. I used B2 = cat(3, B2,B2,B2); to convert my image to rgb and i want to contrast my image using imadjust(B2);.But i am getting an error like "Specified syntax IMADJUST(I) is only supported for 2-D grayscale images. Additional input arguments are required for use with RGB images." .
I tried the following code.Can you please suggest any solution for this issue?
B2 = cat(3, B2,B2,B2);
a = imadjust(B2);
b = histeq(B2);
c = adapthisteq(B2);
% Display the three contrast adjusted images as a montage.
montage({a,b,c},'Size',[1 4])
title("Original Image and Enhanced Images using imadjust, histeq, and adapthisteq")
Dear Guillaume, i tried imwrite to save my image 'B2' which is the final output after highpass filtering in my prevoius coed.But the saved image is a gray color there is no any picture in it. I convert it in to rbg image and tried to save it.But the output is same gray background. I hope you will help me to solve this issue..
I don't see the point of converting your greyscale B2 to RGB. It uses 3 times the memory for no benefit. In any case, if you want to convert it to RGB do it after you've done your contrast adjustment, not before.
When you save B2 what is its class and intensity range? What is
class(B2)
min(B2(:))
max(B2(:))
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?
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by