Project 1: Image Filtering and Hybrid Images

Example of a hybrid image

For this project I implemented the my_imfilter method and then used that method to generate hybrid images. These images look like one picture up close then like another from a distance. The above image is an example of such a hybrid image. It looks like a submarine upclose and like a fish from a distance.

The Algorithm

my_imfilter has two arguments, the input image and the filter. The algorithm works by first padding the image on the top and bottom by half of one less than the filter's height and the sides by half of one less than the filter's width. Then it convolves each pixel and its neighbors with the given filter. It puts the convolved pixels into an output image. Below is the code for my implementation of my_imfilter.

filter = single(filter);

x0 = size(image, 1);
y0 = size(image, 2);

fx = ((size(filter,1)-1)/2);
fy = ((size(filter,2)-1)/2);

impad = padarray(image,[fx, fy], 'symmetric');
output = zeros(size(impad,1),size(impad,2));
output = cat(3,output,output,output);

for z = 1:size(image,3)
    for x = (1+fx):(size(image,1)+fx)
        for y = (1+fy):(size(image,2)+fy)
            xx = (x-fx):(x+fx);
            yy = (y-fy):(y+fy);
            im = impad(:,:,z);
            im = im(xx,yy);
            p = filter.*im;
            s = sum(p(:));
            output(x,y,z) = s;   
        end
    end
end

output = imcrop(output,[fy+1 fx+1 y-fy-1 x-fx-1]);

To achieve the hybrid image effect, you first use my_imfilter to generate a low pass filtered image and a high pass filtered image. A low pass filtered image is acheived by using a gaussian filter in my_imfilter. A high pass filtered image is achieved by first running a low pass filter then subtracting that output from the original image. Finally add the highpass and the lowpass filtered images together.

Results in a table

Here are some more example outputs.