Clear Filters
Clear Filters

Error in sharpening image.How can I solve it?

5 views (last 30 days)
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');
  2 Comments
Nayana R
Nayana R on 23 Oct 2018
Edited: Walter Roberson on 23 Oct 2018
This is my error message.Can you please help me to solve this?
Error using imsharpen
Expected input number 1, A, to be real.
Error in imsharpen>parse_inputs (line 209)
validateattributes(A,validImageTypes,{'nonsparse','real'},mfilename,'A',1);
Error in imsharpen (line 79)
[A, radius, amount, threshold] = parse_inputs(varargin{:});
Error in MyEnhancementTest2 (line 75)
b = imsharpen(B2);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Oct 2018
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.
  4 Comments
Nayana R
Nayana R on 25 Oct 2018
Edited: Walter Roberson on 25 Oct 2018
Thank you Walter Roberson. I want to know what is exactly happening in my code this part. Can you please explain me this code line by line? I hope you will help me this time too. Thank you so much again for helping me.
% 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);
Walter Roberson
Walter Roberson on 25 Oct 2018
A1 is not defined. Context would suggest that it is either fft(A) or fft2(A)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!