Project 1: Image Filtering and Hybrid Images

Hybrid image created from combining high and low frequencies.

When I first thought about how to tackle this project, I came with two ways of seperating and applying the filter. I first thought of seperating the image into 3 2d arrays and running the filter on each color. After this I would combine it into a picture again. The other way would be to leave it whole and as I iterated through the 3d array image I would apply filter at each level of the array. The first way seemed my more intuitive so I stood with it. After seperating the color, I chose to do a zero padding around each image as you will need it to maintain the size of the image. Instead of writing the filter function 3 times, I chose to create a seperate filter function to handle all the filtering. My initial thought was 4 for loops, but that seemed much too strenous and not smart enough. After playing around with the width and height of different filters, I realized that the padding and the iteration could be broken down into a single variable for x and one for y. Then after some minor adjucements to that variable I could determine the padding and how to iterate through it. I chose to iterate through the image one filter image size at a time, to save me alot of for loops and lines of code. After the color array is done filtered, I got rid of the extra padding. After all the colors are filtered I combined them together to make a image.

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
function output = my_imfilter(image, filter)
%Red image seperated
imageRed = image(:,:,1);
%Added padding
paddedRed = padarray(imageRed,[xNum yNum]);
%Applying filter
filterRed = applyFilter(paddedRed, filter);

%Green image seperated
imageGreen = image(:,:,2);
%Added padding
paddedGreen = padarray(imageGreen,[xNum yNum]);
%Applyingfilter
filterGreen = applyFilter(paddedGreen, filter);

%Blue image seperated
imageBlue = image(:,:,3);
%Added padding
paddedBlue = padarray(imageBlue,[xNum yNum]);
%Applyingfilter
filterBlue = applyFilter(paddedBlue, filter);

%Adding them all together
outp = cat(3, filterRed, filterGreen, filterBlue);
output = outp;

end

function imageA = applyFilter(modImage, filter)

%Variables used to help calculate iteration size and padding
xNum = fix(size(filter, 1)/2);
yNum = fix(size(filter, 2)/2);

%Set a temp image to store the new filtered pixels
mTemp = zeros(size(modImage));

%Iterating through the color array and applying the filter
%Iterating one matrix size at a time
for i = (xNum+1): (size(modImage, 1) - (xNum))
    for j =(yNum+1): (size(modImage, 2) - (yNum))
        temp = filter.*modImage(i-xNum:i+xNum,j-yNum:j+yNum);
        mTemp(i,j)=sum(temp(:));
    end
end


%Get rid of padding
imageB=mTemp(xNum+1:end-xNum,yNum+1:end-yNum);
imageA= imageB;
end

For hybrid images, I created the low frequencies by blurring the image with a gaussian filter. While for high frequencies, I first blur the image and then remove the blur from the image. I often compare flip the two images as I have found that there is usually one that works alot better then the other.

Example Images