The alogrithm behind linear image filtering is relatively simple. Given two inputs matrices (image and filter). For every pixel in image, we take a submatrix of same size as filter, consists of pixels surrounding it. Then do a inner product between the submatrix and the filter, the sum of all the products will be the corresponding value in output matrix. To take care of the edge of image, we need to pad zeros surrounding the image before filter it.
In this project, we have to handle both gray-scale and color images. However, for color images we can handle each channel independently, which means we just need to do exactly same operations three times as we do to gray-scale image. Here's my code of my_imfilter.
function output = my_imfilter(image, filter)
% get size info
im_row = size(image, 1);
im_col = size(image, 2);
depth = size(image, 3);
f_row = size(filter, 1);
f_col = size(filter, 2);
row_padding = floor(f_row/2);
col_padding = floor(f_col/2);
% padding
image = vertcat(zeros(row_padding, im_col, depth), image); % up
image = vertcat(image, zeros(row_padding, im_col, depth)); % down
image = horzcat(zeros(im_row + 2*row_padding, col_padding, depth), image); % left
image = horzcat(image, zeros(im_row + 2*row_padding, col_padding, depth)); % right
% filter
output = zeros(im_row, im_col, depth);
if depth == 3
filter = filter(:,:,ones(1,3));
end
for row = 1 : im_row
for col = 1 : im_col
element = image(row:row+f_row-1, col:col+f_col-1, :) .* filter;
for d = 1 : depth
output(row, col, d) = sum(sum(element(:, :, d)));
end
end
end
end
Here are the result images from proj1_test_filtering.
The idea of hybrid images is to take low frequencies and high frequencies from two images respectively, then add them up to form a new image. The procedure includes three steps as following:
low_frequencies = my_imfilter(image1, filter);
high_frequencies = image2 - my_imfilter(image2, filter);
hybrid_image = low_frequencies + high_frequencies;
For different pair of images, the proportion of low frequencies/high frequencies is controlled by cutoff_frequency, which determines the standard deviation of the Gaussian filter. Here are five hybrid images with different cutoff_frequencies.