In this write-up I will give an overview of the filtering function I wrote and the hybrid images created with it. I will describe the alogirthm I implemented and provide examples of my hybrid images.
Hybrid image of Marilyn Monroe and Albert Einstein
I implemented a filtering algorithm "my_imfilter", which replicates the behavior of the
built-in Matlab function "imfilter". First it creates a padded version of the input image
with reflected values along the edges and creates a zero-filled matrix for the output image.
It then iterates through each pixel and color channel in the input image. In every iteration,
a slice is copied from the padded input image with the same dimensions as the filter. This matrix is multiplied
element-wise by the filter. The result of that operation is a matrix with all of the image-filter products, so
the products are then summed and set as the value in the output image.
I found that the element-wise multiplication followed by the summing operation was much faster than
manually iterating over the filter matrix. This was my first interaction with Matlab, and this discovery
made me realized just how much faster it is to use internal functions/operators.
function output = my_imfilter(image, filter)
rows = size(filter, 1);
cols = size(filter, 2);
rowMid = floor(rows/2) + 1;
colMid = floor(cols/2) + 1;
% Reflect edges
paddedImage = padarray(image, [rowMid colMid], 'symmetric');
% Create an empty output matrix
output = zeros(size(image, 1), size(image, 2), size(image, 3));
% Rows
for iImg=1:size(image, 1)
% Columns
for jImg=1:size(image, 2)
% Channels
for c=1:size(image, 3)
sample = paddedImage(iImg+1:iImg+rows, jImg+1:jImg+cols, c);
prod = filter .* single(sample);
output(iImg, jImg, c) = sum(sum(prod));
end
end
end
end
Below are the outputs from the algorithm for various filters.
3x3 box filter | 25x25 Gaussian filter | High-pass (laplacian) filter | Horizonal edge (Sobel) filter |
Marilyn Monroe and Albert Einstein hybrid image at various scales |
Cat and dog hybrid image at various scales |