CS 143 / Project 1 / Image Filtering and Hybrid Images

An example of a processed hybrid image.

This project's goal is to write an image filter processing function in Matlab to create hybrid images which combine two different images by applying low-frequency and high-frequency filters respectively. As a result, you get a hybrid image that at a close distance, the high frequecny filtered image becomes more salient and at a longer distance, the low frequecny filter image becomes visible.
Although, Matlab's own 'imfilter' function works flawlessly with GPU and 64 bit accerlation support, the goal of the project is to design own filter algorithms to get acquainted with Matlab functions. The example of an hybrid image is shown on the right. If you step further away and view the image, the high pass filtered 'cat' image will fade away and be able to see the low-pass filtered image.

code and algorithms used

To calculate a high-pass filtered image, the original image was low-pass filtered and substracted from itself.
H(I) = I - L(I)
A hybrid image was a sum of a low-pass filtered image and a high-pass filtered image.
Hybrid(I1, I2) = L(I1) + H(I2)


low_frequencies = my_imfilter(image1, filter, 'mirror');

high_frequencies = image2 - my_imfilter(image2, filter, 'mirror');

hybrid_image = low_frequencies + high_frequencies;
Code using two different frequencies for low and high pass filters.

%% Filtering and Hybrid Image construction
cutoff_frequency = 7; 
cutoff_frequency2 = 2; 

filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);
filter2 = fspecial('Gaussian', cutoff_frequency2*4+1, cutoff_frequency2);

low_frequencies = my_imfilter(image1, filter, 'mirror');

high_frequencies = image2 - my_imfilter(image2, filter2, 'mirror');

hybrid_image = low_frequencies + high_frequencies;
For the filter implmentation, as suggested in the homework description, I used 'padarray', 'im2col', and 'col2im' functions in Matlab. It can support the following features.
  1. support grayscale and color images
  2. support arbitrary shaped filters, as long as both dimensions are odd (e.g. 7x9 filters but not 4x5 filters)
  3. pad the input image with zeros or reflected image content and
  4. return a filtered image which is the same resolution as the input image.

% Handles the cases where the filter sizes are not given as odd numbers
if (~mod(size(filter, 1), 2) || ~mod(size(filter, 2), 2))
    error('The row and column size of the filter have to be in odd numbers.');
end

% The default option is padding with zeros
if (nargin < 3)
    option = 'zero';
end

r_margin = (size(filter, 1) - 1) / 2;
c_margin = (size(filter, 2) - 1) / 2;

result = zeros(size(image));

for ii = 1:size(image,3)    
	% pad the image array either symmetrically or with zeros
    if strcmp(option, 'zero')
        input = padarray(image(:, :, ii), [r_margin c_margin]);
    elseif strcmp(option, 'mirror')
        input = padarray(image(:, :, ii), [r_margin c_margin], 'symmetric');
    end
    % calculate the filter using im2col, col2im, and dot product
    filtered = filter(:)'*im2col(input, size(filter), 'sliding');
    result(:,:,ii) = col2im(filtered, [1 1], [size(image, 1) size(image, 2)]);
end

output = result;

Results

my_imfilter test


Identiy

Small Blur

Large Blur

Large Blur w/ symmetrical
(mirrored) padding

Sobel

High Pass (discrete Laplacian)

High Pass (alternative)

Dog (low) and Cat (High) (cf = 7)


'Dog' image w/ low frequencies

'Cat' image w/ high frequencies

Hybrid Image

Bird (low) and Plane (High) (cf = 7)


'Bird' image w/ low frequencies

'Plane' image w/ high frequencies

Hybrid Image

Bird (low) and Plane (High) (cf = 5)


'Bird' image w/ low frequencies

'Plane' image w/ high frequencies

Hybrid Image

Plane (low) and Bird (High) (cf = 7)


'Plane' image w/ low frequencies

'Bird' image w/ high frequencies

Hybrid Image

Einstein (low) and Marilyn (High) (cf = 7, single cutoff freq.)


'Einstein' image w/ low frequencies

'Marilyn' image w/ high frequencies

Hybrid Image

Einstein (low) and Marilyn (High) (cf = 5, single cutoff freq.)

When the cut frequency gets smaller, Einstein appears quicker as smaller the image becomes. But, You can still see Marilyn in the third image.


'Einstein' image w/ low frequencies

'Marilyn' image w/ high frequencies

Hybrid Image

Einstein (low) and Marilyn (High) (cf1 = 5, cf2 = 3)

When the cut frequency 2 gets smaller, Marilyn disappears quicker as smaller the image becomes. In the third image, you can see Einstein better


'Einstein' image w/ low frequencies

'Marilyn' image w/ high frequencies

Hybrid Image

Motorcycle (low) and Bicycle (High) (cf1 = 7, cf2 = 3)


'Motorcycle' image w/ low frequencies

'Bicycle' image w/ high frequencies

Hybrid Image

Motorcycle (low) and Bicycle (High) (cf1 = 5, cf2 = 2)


'Motorcycle' image w/ low frequencies

'Bicycle' image w/ high frequencies

Hybrid Image

Bicycle (low) and Motorcycle (High) (cf1 = 7, cf2 = 3)


'Bicycle' image w/ low frequencies

'Motorcycle' image w/ high frequencies

Hybrid Image

Fish (low) and Submarine (High) (cf1 = 7, cf2 = 3)


'Fish' image w/ low frequencies

'Submarine' image w/ high frequencies

Hybrid Image

Submarine (low) and Fish (High) (cf1 = 7, cf2 = 3)


'Submarine' image w/ low frequencies

'Fish' image w/ high frequencies

Hybrid Image

A complete analysis of two different cutoff frequencies in high and low pass filters
('Bicycle' and 'Motorcycle' image was used)

click to view