Project 1: Image Filtering and Hybrid Images

Example of a hybrid image.

Overview

This project uses Matlab to programmatically apply filters to images. The project was fairly straightforward--the greatest challenge was working with Matlab as I have very minimal experience with it.

Algorithm

The algorithm first breaks down an image into its individual RGB channels. For each channel, a 2-dimensional convolution is applied using the channel and the filter. The results of each channels convolution are concatenated into a final image.

Separating & Combinging Channels

    if (length(size(image)) == 3) 
        redChannel = image(:, :, 1);
        greenChannel = image(:, :, 2);
        blueChannel = image(:, :, 3);

        output = cat(3, my_conv2(redChannel, filter), my_conv2(greenChannel, filter), 
        	my_conv2(blueChannel, filter));
    else
        output = my_conv2(image, filter);
    end
2D Convolution

The convolution works by first creating an empty padded image. The padding is done so that filtering may still be accurately applied to edge/boundary pixels. The original image is then copied to be centered within the padded image. Each nonzero pixel within the padded image is then applied with the filter and the sum of that filter application is written to the pixel's location in the output image.

 
    imageHeight = size(image, 1);
    imageWidth = size(image, 2);
    
    filterHeight = size(filter, 1);
    filterWidth = size(filter, 2);
    
    % make even then split
    paddingHeight = (filterHeight - 1) / 2;
    paddingWidth = (filterWidth - 1) / 2;
    
    paddedImage = zeros(imageHeight + paddingHeight * 2, imageWidth + paddingWidth * 2);
    
    % copy original image to padded version
    for i = 1 : imageHeight
        for j = 1 : imageWidth
            paddedImage(i + paddingHeight, j + paddingWidth) = image(i, j);
        end
    end
    
    output = zeros(imageHeight, imageWidth);
    
    % apply filter and save to output
    for i = 1 : imageHeight
        for j = 1 : imageWidth
            appliedFilter = applyFilterAtIndexes(paddedImage, filter, i, paddingHeight * 2, j, paddingWidth * 2);
            output(i, j) = sum(sum(appliedFilter));
        end
    end 

Results

Sample Filters

Below are some simple test filters applied to a cat image

Original Image Identity Filter Blur Filter Large Blur Filter
Sobel Filter Laplacian Filter High Pass Filter
Intermediate Images

In order to create a hybrid image, we must first construct high and low frequency images. Sample images have been generated and are displayed below.
Original Image Original Image
Low Frequencies High Frequencies

Hybrid Image Creation

Merging the high and low frequency images together, we create a hybrid image. A sample hybrid image is displayed below at various scales.