Project 1: Image Filtering and Hybrid Images

Cat-Dog hybrid image

Tasks

  1. my_imfilter.m
  2. proj1.m

my_imfilter.m

The first task that I completed when doing this project was the my_imfilter.m function, which was to take in an image and a filter, and do a convolution with those inputs. It was also necessary to adjust the image with padding because otherwise, the outer edge of the filtered picture would end up being left as zeros (since my filtered picture was initiated as a zero matrix). I added padding to my image using the padarray() function with the 'symmetric' function so that it reflects over the edge.

After padding the image appropriately and creating a zero array, I iterated over each of the x-y coordinates in the 2D image matrix for each of the color channels. At each position, I did a dot product with the filter, going the length and width of the filter in the image and multiplying the values at each corresponding location. I made sure to only hit the indices in the image that were the number of rows or coloums minus the length or width of the filter, so as to ensure that I didn't go out of bounds. I took this value and stored it into the zero/output matrix in the middle of where the filter was in relation to where it started (imagerow + ceil(filtersize(row) / 2), imagecol + ceil(filtersize(col) / 2)).

proj1.m

For the second task, I had to create a hybrid of two images. I used the demo images that were provided in the data folder and mixed and matched images that were oriented in similar ways. I took one image and used a low frequency filter - which was just a Gaussian blur with some cutoff frequency - on it to get just the low frequencies of the image. Then, for the other image, I again got the low frequency filtered version of the image, and subtracted that from the original image to get just the high frequencies of the image. I Then used imfuse(img1, img2, 'blend') in order to combine the two images. I also adjusted the cutoff frequency for each image combination to see what provided the best results.

Example heading

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.

Code snippet from my_imfilter.m


%example code
sizeFilterRow = ceil(size(filter, 1) / 2);
sizeFilterCol = ceil(size(filter, 2) / 2);

endMatrix = zeros(size(image, 1), size(image, 2));
imageMatrix = padarray(image, [sizeFilterRow, sizeFilterCol], 'symmetric');
size(imageMatrix, 1);
size(imageMatrix, 2);
filterMatrix = filter;


for color = 1:3
    for imageRow = 1:size(imageMatrix, 1) - size(filterMatrix, 1) - 1
        for imageCol = 1:size(imageMatrix, 2) - size(filterMatrix, 2) - 1
            filteredValue = 0;
            for filterRow = 1:size(filterMatrix, 1)
                for filterCol = 1:size(filterMatrix, 2)
                    filVal = filterMatrix(filterRow, filterCol);
                    imVal = imageMatrix(imageRow + filterRow, imageCol + filterCol, color);
                    filteredValue = filteredValue + filVal * imVal;

                end
            end
            endMatrix(imageRow, imageCol, color) = filteredValue;
        end
    end
end

output = endMatrix;

Results in a table

Low frequency dog, high frequency cat, hybrid, hybrid scales; cutoff = 7
Low frequency motorcycle, high frequency bicycle, hybrid, hybrid scales; cutoff = 6
Low frequency Marilyn, high frequency Einstein, hybrid, hybrid scales; cutoff = 7
Low frequency bird, high frequency plane, hybrid, hybrid scales; cutoff = 4
Low frequency submarine, high frequency fish, hybrid, hybrid scales; cutoff = 4

I noticed that for many of the images, after combining the two images, the hybrid tended to get a little darker overall. For a few images, I tried adding a 0.5 to the low frequency image, which seemed to help for the fish-submarine image. However, overall, it seemd that creating a hybrid picture worked for these examples.