MATLAB: Error in sharpening image.How to solve it

filterhigh pass frequencysharpening

I am using MatLab R2018a and i want to sharpen a x-ray image.So far I convert my image to frequency domain and filter using high pass filtering.Now I want to sharpen my image.But it give me an error when sharpening.Here is my code.I understand that I want to convert this image in two rgb or gray or binary form to sharpen image.But I don't know what is the correct form of the image.I am greatful if you can help me to find a solution.
close all;
clear all;
clc;
img = imread('003.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')
% figure(3)
% imshow(abs(B1),[12 290]), colormap gray

% title('low pass filtered image','fontsize',14)

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(2, 2, 3);
% imshow(abs(B1),[12 290]), colormap gray
% title('low pass filtered image','fontsize',14)
subplot(1,3,3);
imshow(abs(B2),[12 290])
title('High pass filtered image');
% b = imsharpen(B2);
% figure(6);
% imshow(b)
% title('Sharpened Image');

Best Answer

You read in an image, take the grayscale, and fft2 that, getting a complex array . Each of the two fft steps done for you by fft2 produces a conjugate symmetric array when operating on real data; because after the first step it would not be real data, the second step would not be conjugate symmetric. You fftshift() that in one direction but not the other, so the fft is centered in rows but not in columns. You then create a filter based upon 1/3 of the way through the fft data in both directions, and apply that filter to the fft data. The result cannot possibly be conjugate symmetric in either direction, since it is not a conjugate symmetric filter and the coefficients are not centered around either of the two principle axes. Therefore when you ifft2() the result, you cannot possibly get a real-valued result, so your B is not real valued.
You cannot imsharpen() a complex matrix.