Clear Filters
Clear Filters

How can I write noise removal and blur deblur codes in a single matlab code using fft?

16 views (last 30 days)
I was able to write the noise removal code, but I need to add blur and deblur to it. How should I do this?

Accepted Answer

Hassaan
Hassaan on 25 Dec 2023
Edited: Hassaan on 25 Dec 2023
Performs noise removal, blurring, and deblurring operations using the Fast Fourier Transform (FFT) in MATLAB.
This code assumes that you have an image matrix image loaded into your MATLAB workspace. The code adds noise to this image, performs blurring using a Gaussian kernel, and then attempts to deblur the image using the Fourier transform. The imnoise function is used to add Gaussian noise to the image, and the blurring is done by creating a Gaussian kernel and applying it in the frequency domain.
% Assuming you have a variable `image` with your data
% Add some noise to the image
noisy_image = imnoise(image, 'gaussian', 0, 0.01);
% Perform FFT on the noisy image
fft_noisy_image = fft2(noisy_image);
% Generate a Gaussian blur kernel
kernel_size = 21; % Size of the kernel
sigma = 5; % Standard deviation of the Gaussian blur
[x, y] = meshgrid(round(-kernel_size/2):round(kernel_size/2), round(-kernel_size/2):round(kernel_size/2));
gaussian_kernel = exp(-x.^2/(2*sigma^2)-y.^2/(2*sigma^2));
gaussian_kernel = gaussian_kernel / sum(gaussian_kernel(:));
% Pad the kernel to the size of the image
padded_kernel = zeros(size(noisy_image));
padded_kernel(1:kernel_size, 1:kernel_size) = gaussian_kernel;
% Perform FFT on the padded kernel
fft_gaussian_kernel = fft2(padded_kernel);
% Apply the Gaussian blur in the frequency domain
fft_blurred_image = fft_noisy_image .* fft_gaussian_kernel;
% Inverse FFT to obtain the blurred image
blurred_image = ifft2(fft_blurred_image);
% Deblurring (for simplicity, we are using the reciprocal of the Gaussian kernel)
% In practice, this might need regularization to prevent amplifying noise
fft_deblurred_image = fft_blurred_image ./ fft_gaussian_kernel;
% Inverse FFT to obtain the deblurred image
deblurred_image = ifft2(fft_deblurred_image);
% Display the images
subplot(2,2,1), imshow(image), title('Original Image');
subplot(2,2,2), imshow(noisy_image), title('Noisy Image');
subplot(2,2,3), imshow(blurred_image, []), title('Blurred Image');
subplot(2,2,4), imshow(deblurred_image, []), title('Deblurred Image');
Remember that this simple deblurring method assumes that you know the exact Gaussian blur that was applied to the image and does not include any regularization. In real scenarios, the deblurring process can be much more complex, and techniques such as Wiener filtering or constrained least squares filtering are used to achieve better results.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Dec 2023
This is what you are trying to obtain:
O_img = imread('MATLAB.jpg');
% Create a blur kernel, e.g., Gaussian noise type of blur:
B_kernel = fspecial('gaussian', [13, 13], 3); % Adjust kernel size and sigma you want
% Convert the original image to double:
O_img = im2double(O_img);
% Apply FFT to the original image and the blur kernel:
FFT_O_img = fft2(O_img);
FFT_B_kernel = fft2(B_kernel, size(O_img, 1), size(O_img, 2));
% Convolution in Frequency domain to get blurs into an image:
FFT_B_img = FFT_O_img .* FFT_B_kernel;
% Inverse FFT to get the blurred image:
B_img = ifft2(FFT_B_img);
% Original vs. blurred images:
figure;
subplot(211); imshow(O_img); title('Original Image');
subplot(212); imshow(abs(B_img), []); title('Blurred Image');

Tags

Products

Community Treasure Hunt

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

Start Hunting!