CS 143 / Project 1 / Image Filtering and Hybrid Images

Figure 1: Original images of a cat and dog.

Figure 2: End result hybrid image of a cat and dog.

In this project we sought out to create hybrid images from two base images. As an example we took an image of a cat and dog (see in Figure 1) and passed them both through high/low frequency image filters. Then when we composed the two resulting images a hybrid image is produced that shows characteristics of both original images. Figure 2 shows the resulting hybrid image of the cat and dog shown at different sizes, the largest size on the left shows the original cat and as the image gets smaller the dog starts to appear.

Core Image Filter Function

By abstracting away the actual filtering and sub-image functions we get a clean readable version of the imfilter function, displayed below.


function output = my_imfilter(image, filter)

[m n dim] = size(image);
output = zeros(m,n,dim);

for i=1:m
    for j=1:n
        subimg = get_subimage(image, [i j], size(filter));
        output(i,j,:) = apply_filter(subimg, filter);
    end
end

Sub-Image Function

In order to apply a defined filter we must first get the sub-image pixels that surround each pixel in the original image. To do this we must provide our get_subimage function with the pixel location we want to get a sub-image from and the size of the of the sub-image (which is the size of the filter itself). The filter size must be odd or else we will get undefined results. If we attempt to get a sub-image around an edge point the non-existing pixels will be zero.


function subimage = get_subimage(image, center, resultSize)
i = center(1);
j = center(2);
mInc = (resultSize(1)-1)/2;
nInc = (resultSize(2)-1)/2;
[mMax nMax dim] = size(image);

subimage = zeros(resultSize(1), resultSize(2), dim);
m = 1;
for p=(i-mInc):(i+mInc)
   n = 1;
   for q=(j-nInc):(j+nInc)
      if (p < 1 || q < 1 || p > mMax || q > nMax)
        subimage(m,n) = 0;
      else
        subimage(m,n,:) = image(p,q,:);
      end
      n = n + 1;
   end
   m = m + 1;
end

Apply Filter Function

Once we have a subimage it is trivial to apply the filter to it, we just sum up the pixels multiplied by the filter.


function point = apply_filter(subimg, filter)
% Given a MxN subimage and an MxN filter, get the weighted sum point.

[mi ni dim] = size(subimg);

point = zeros(1,dim);
for i=1:mi
    for j=1:ni
        for d=1:dim
            point(1,d) = point(1,d) + subimg(i,j,d)*filter(i,j);
        end
    end
end

Results in a table

low-freq
high-freq
hybrid
small hybrid

The above table shows the two high/low frequency filtered images and then their resulting hybrid image. In the larger hybrid images you can clearly see the original high-freq image, where as the smaller version of the same hybrid image shows the low-freq image.