In: Electrical Engineering
I blurred a grayscale image using a 5x5 average filter and then I am supposed to deblur using an Inverse filter using Constrained Division. Is there a better way to do this?
The error that pops up is this:
Error using ./ Complex integer arithmetic is not supported.
Error in test2 (line 12) fbw = fftshift(fft2(blur))./bw;
When I blur the image using a butterworth filter it works just fine yet when I blur using the average filter my code won't work. Below is my attempt:
clear I = imread('buffalo.png'); H = fspecial('average',[5 5]) ; blurred=imfilter(I,H); blur=im2uint8(mat2gray((blurred))); imshow(blur)
d = 0.01; bw = blurred; bw(find(bw<d)) = 1; fbw = fftshift(fft2(blur))./bw; ba = abs(ifft2(fbw)); unb01 = im2uint8(mat2gray(ba));
im=imread('Sharon2.jpg');
y=rgb2gray(im);
z=imresize(y,[512 512]);
x=imrotate(z,-270);
figure,
imshow(x);
% Define blur kernel
hsize = 17;
sigma = 5; % Whatever
kernel = fspecial('gaussian', hsize, sigma)
% Read in sample image.
grayImage = imread('cameraman.tif'); % Sample image.
[rows, columns, numberOfColorChannels] = size(grayImage)
w = floor(hsize/2);
for col = w+1 : columns - w
for row = w+1 : rows - w
% code to filter one pixel at location (row, col)
% Get a window around the target pixel that we
% will consider when blurring.
row1 = row - w;
row2 = row1 + hsize - 1;
col1 = col - w;
col2 = col1 + hsize - 1;
subImage = double(grayImage(row1:row2, col1:col2));
% Blur the pixel at row, column.
tempImage = subImage .* kernel;
newPixelValue = uint8(sum(tempImage(:)));
% Put blurred value back into original image
grayImage(row, col) = newPixelValue;
end
end
imshow(grayImage, []);
title('Blurred Image', 'FontSize', 30);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
grayImage = imread('Sharon2.jpg'); % Sample image.
z=imresize(grayImage,[512 512]);
x=imrotate(z,-270);
y=rgb2gray(x);
figure,
imshow(y);
% Define blur kernel
hsize = 17;
sigma = 5; % Whatever
kernel = fspecial('gaussian', hsize, sigma);
% Read in sample image.
grayImage = imread('Sharon2.jpg'); % Sample image.
z=imresize(grayImage,[512 512]);
x=imrotate(z,-270);
y=rgb2gray(x);
figure,
imshow(y);
% Get a window around the target pixel that we
% will consider when blurring.
row1 = 100 - floor(hsize/2);
row2 = row1 + hsize - 1;
col1 = 150 - floor(hsize/2);
col2 = col1 + hsize - 1;
subImage = double(grayImage(row1:row2, col1:col2));
% Blur the pixel at row 100, column 150.
tempImage = subImage .* kernel;
newPixelValue = uint8(sum(tempImage(:)));
% Put blurred value back into original image
grayImage(100, 150) = newPixelValue;
figure,
imshow(tempImage);
% for col = 1 : columns
% for row = 1 : rows
% % code to filter one pixel at location (row, col)
% end
% end