Project 1: Image Filtering and Hybrid Images

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.

Filtering Algorithm

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.

Code

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

Results

Below are the outputs from the algorithm for various filters.

3x3 box filter 25x25 Gaussian filter High-pass (laplacian) filter Horizonal edge (Sobel) filter

Creating the hybrid image

A hybrid image is created by applying a low-pass filter to one image and a high-pass filter to another. The two are then blended together to form one image. The low-pass filter can by any blurring filter, in this case a Gaussian with the cutoff threshold set to 6. The low-frequency version of the image is left as a result. The high-frequency image is created by applying a low-pass filter and subtracting it from the original image. The resulting image is zero-centered, so 0.5 is added to each value.
Einstein after low-pass filter
Marilyn after high-pass filter

Hybrid image results

Marilyn Monroe and Albert Einstein hybrid image at various scales
Cat and dog hybrid image at various scales