Project 1: Image Filtering and Hybrid Images

Algorithm for my_imfilter

Goals Summary for my_imfilter

  1. Filter color and grayscale images.
  2. Allow for variable size filter dimensions of odd lengths.
  3. Return an image of the same size as the original.
  4. Get the dot product of each neighborhood to filter each pixel of the image.

my_imfilter Algorithm and Reasoning

  1. Get the number of channels for the image.
    This allows the algorithm to filter grayscale and color images.
    The value is stored as a variable to be used when filtering.
  2. Calculate the number of rows and columns needed as padding.
    Since we know that there will always be an odd number of rows and columns in each filter, we can calculate the number of total padding rows and columns by subtracting 1 from each side of the filter. If we divide this number by 2, we will get the number of rows and columns of padding needed on each side of the image.
  3. Create the padded matrix by making a matrix with the necessary number of rows and columns and filling it with the image.
    We will use this to perform calculations for each neighborhood with the filter. This matrix will not store the result of the dot product because in some scenarios it will compound the results to something different than the expected convolution.
  4. Perform the dot product of each neighborhood from the padded matrix and the filter. Output the data in a matrix of the same dimensions of the padded matrix used in calculations.
    This will give us the result of the filtering with the padding.
  5. Crop the padding from the resulting image.
    This makes the output the same size as the input. That way it will be easier to perform future steps like removing blur.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Example of code with highlighting

The javascript in the highlighting folder is configured to do syntax highlighting in code blocks such as the one below.


%example code showing how to use my_imfilter to create a hybrid image

image1 = im2single(imread('../data/dog.bmp'));
image2 = im2single(imread('../data/cat.bmp'));

filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);

low_frequencies = my_imfilter(image1, filter);
high_frequencies = image2 - my_imfilter(image2, filter);

hybrid_image = low_frequencies + high_frequencies;

Results of Filtering Algorithm

Hybrid Image of Cat and Dog (cuttoff frequency = 7)

Hybrid Image of Bike and Motorcycle (cuttoff frequency = 7)

This my_imfilter method implemented is capable of performing filtering operations on a wide variety of images and filters. In the examples above, we are performing Gaussian blurs on each original image (and subtracting it for the high frequency image). When we add the high and low frequency image, we get a hybrid image that appears differently depending on how far away the viewer is from the image.

my_imfilter is also capable of performing filtering on a wide variety of filters so long as the widths are the odd.