Project 1: Image Filtering and Hybrid Images

Hybrid image of Cesear and Andy Serkis from Rise of the Planet of the Apes (2011).

In this project, an image filtering function is implemented to create hybrid images. Hybrid images are static images that change in interpretation as a function of the viewing distance. The basic idea is that high frequency tends to dominate perception when it is available, but, at a distance, only the low frequency (smooth) part of the signal can be seen. By blending the high frequency portion of one image with the low-frequency portion of another, you get a hybrid image that leads to different interpretations at different distances.

The things have been done within this project are listed in the following;

  1. my_imfilter() which imitates the default behavior of the built-in imfilter() Matlab function is implemented.
  2. An extension to my_imfilter() is added to pick the boundary option as implemented in imfilter().
  3. Exceptions such as giving filters with even dimension, giving non-defined boundary options are handled.
  4. Hybrid images with given dataset are created.
  5. my_imfilter() is tested with both grayscale and color images.
  6. Ideal cut-off frequencies are identified for each image pairs.

my_imfilter() Function Details

Here is the details of my_imfilter() function.

  1. Check parameter counts. If there are less than three parameters, then, set boundary padding option to 'zero' padding.
  2. 
    if nargin < 3
       bound_opt = 'zero';
    end
    
  3. Check boundary option type. If given boundary option parameter is different from 'zero', 'symmetric', 'replicate' or 'circular', then, raise a warning and set the parameter to the default 'zero' padding.
  4. 
    if ~( strcmpi(bound_opt,'zero') || strcmpi(bound_opt,'symmetric') || ...
    	strcmpi(bound_opt,'replicate') || strcmpi(bound_opt,'circular') )
        warning(['Invalid boundary option "' bound_opt '". Using "zero" boundary option.']);
        bound_opt='zero';
    end
    
  5. Check the dimensions of filter. If both width and height of the given filter are not odd value, then, raise an error and terminate the code.
  6. 
    if any(mod(size(filter),2)==0)
        error(['Invalid filter size: ' num2str(size(filter))]); % Terminate the code.
    end
    
  7. Create a padded image copy with given image and boundary option parameters. pad_image() function will be described in the following.
  8. 
    p_image=pad_image(image,filter,bound_opt); % padded image
    
  9. Create an output matrix in same size with given image.
  10. 
    im_size=size(image);
    output=zeros(im_size); % filtered image
    
  11. Utilize filter according to given image. If a color image is given, then, concatenate filter three times for each color space. If it is grayscale image, maintain filter as in given format.
  12. 
    if numel(im_size)==3
       filter= cat(3,filter,filter,filter);
    end
    
  13. Apply filter to the image with respect to following formula, where, h is filtered image, g is the filter and f is the padded image.
  14. 
    [f_y,f_x]=size(filter);
    for i=1:im_size(1)
        for j=1:im_size(2)
            output(i,j,:)=sum( sum( p_image(i:i+f_y-1,j:j+f_x-1,:).*filter ) );
        end
    end
    

pad_image() Function Details

This function pads any given image according to any given filter in different size. The funtion provides four different boundary padding options such as default 'zero' padding, 'symmetric', 'replicate' and 'circular' paddings.


f_y=(size(filter,1)-1)/2; % padding size at the top and bottom of the given image.
f_x=(size(filter,2)-1)/2; % padding size at the left and right of the given image.

% create padded image with respect to given image's color dimension.
im_size=size(image);
if numel(im_size)==2
    p_image=zeros( im_size(1)+2*f_y, im_size(2)+2*f_x);
    p_image(f_y+1:end-f_y,f_x+1:end-f_x)=image;  
else
    p_image=zeros( im_size(1)+2*f_y, im_size(2)+2*f_x,3);
    p_image(f_y+1:end-f_y,f_x+1:end-f_x,:)=image;  
end
  1. zero padding: This is the default padding option. Pads zero values of the given image.
  2. symmetric padding: Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border.
  3. replicate padding: Input array values outside the bounds of the array are assumed to equal the nearest array border value.
  4. circular padding: Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic. Hence, pixels from bottom are copied into upper padding and top pixels are copied into bottom padding pixels. Same operation is applied to left and right padding pixels.
  5. 
    switch bound_opt
        case 'symmetric'
            p_image(1:f_y,:,:)=flipud( p_image(f_y+1:2*f_y,:,:) );
            p_image(end-f_y+1:end,:,:)=flipud( p_image(end-2*f_y+1:end-f_y,:,:) );
            p_image(:,1:f_x,:)=fliplr( p_image(:,f_x+1:2*f_x,:) );
            p_image(:,end-f_x+1:end,:)=fliplr( p_image(:,end-2*f_x+1:end-f_x,:) );
        case 'replicate'
            p_image(1:f_y,:,:)= repmat(p_image(f_y+1,:,:),[f_y 1]);
            p_image(end-f_y+1:end,:,:)= repmat(p_image(end-f_y,:,:),[f_y 1]);
            p_image(:,1:f_x,:)= repmat(p_image(:,f_x+1,:),[1 f_x]);
            p_image(:,end-f_x+1:end,:)= repmat(p_image(:,end-f_x,:),[1 f_x]);
        case 'circular'
            p_image(1:f_y,:,:)=p_image(end-2*f_y+1:end-f_y,:,:);
            p_image(end-f_y+1:end,:,:)=p_image(f_y+1:2*f_y,:,:);
            p_image(:,1:f_x,:)=p_image(:,end-2*f_x+1:end-f_x,:);
            p_image(:,end-f_x+1:end,:)=p_image(:,f_x+1:2*f_x,:);
    end
    
Padded image samples with boundary options of 1. zero, 2. symmetric, 3. replicate and 4. circular paddings in left to right order. Filter size is 51x51 in this example.

Creation of Hybrid Images and Example Hybrid Images

As stated above, the basic idea is that high frequency tends to dominate perception when it is available, but, at a distance, only the low frequency (smooth) part of the signal can be seen. By blending the high frequency portion of one image with the low-frequency portion of another, you get a hybrid image that leads to different interpretations at different distances.

Here is some example hybrid images created with given data samples. In each examples, first image pairs are given. Then, low frequencies of left image and high frequencies of right image are shown in left to right order. Finally, hybrid of two images in different scales are presented in second row of image table. For each example cutoff frequencies are presented. Also, a distance between Matlab imfilter() and my_imfilter() metric is calculated by summing absulute values of pixel by pixel differences in all color dimensions of both hybrid images. Hybrid images generated by using only my_imfilter() and Matlab's imfilter() functions. Results show that my_imfilter() function is imitating Matlab's imfilter() function very closely. Symmetric padding is choosen for all of the filtering operations.

Example 1: cutoff-frequency= 7, distance to Matlab imfilter()= 0.0062057 pixels.

Example 2: cutoff-frequency= 4, distance to Matlab imfilter()= 0.002752 pixels.

Example 3: cutoff-frequency= 3, distance to Matlab imfilter()= 0.016734 pixels.

Example 4: cutoff-frequency= 8, distance to Matlab imfilter()= 0.0040283 pixels.

Example 5: cutoff-frequency= 7, distance to Matlab imfilter()= 0.0056374 pixels.

Example 6: cutoff-frequency= 6, distance to Matlab imfilter()= 0.0015248 pixels.
Cesear and Andy Serkis from Rise of the Planet of the Apes (2011).

Example 7: cutoff-frequency= 7, distance to Matlab imfilter()= 0.0021846 pixels.
Grayscale version of Example 1.