CS143 HW-1 Hybrid Images

@author rmcverry

Included Items:

*included are a variety of hybrid images created from the data directory.

Auxiliary Code:

Project Description

Homework 1 was designed to introduce us to image manipulation within the MATLAB environment. The objective was to take make a hybrid image from two images. The stencil code provided us with the general setup, the means to align two images via two anchor points, and the means to crop the raw hybrid image so that we can output a clean hybrid image. We were assigned two things. First, we were instructed to produce Laplacian and Gaussian pyramids for inputted each image. Second, we were instructed to use these pyramids to produce a hybrid image. In addition to programming a hybrid image maker, we were expected to use our code to produce at least three hybrid images from the images that were provided in the data directory.

Approach

A hybrid image is a special sum of two images. It is the composition of the low frequencies from the first image, with the high frequencies of the second. There are a number of ways we can access the frequency domain of an image, but for this project we were asked to use linear filters and sub-sampling. The method is relatively simple. We first blur the image using a Gaussian blur, and then subtract that blur from the original image. The result is that the highest frequencies remain; this image approximates the Laplacian of the Gaussian of the original image. The next step is to down-sample the Gaussian blur. We then repeat this N times, saving the Gaussian for our Gaussian pyramid and the approximate Laplacian for the Laplacian pyramid. We have therefore split each image into its frequencies, as each image in the Laplace Pyramid stores a different set of frequencies: the highest frequencies with be found with large images at the top of the pyramid, and lowest frequencies will be found at the bottom of the pyramid.

The second great takeaway is that when we can rebuild an original image if we are given the image's Laplacian pyramid of size N and N+1 down sampled image from the Gaussian pyramid. We do this by up-sampling the scaled down image and then apply a Gaussian blur. We then add to it the associated Laplacian image pixel-wise, and perform another up-sample to continue the process. This process can be modified to meet our objective, by putting together two facts. First, remember that a Laplacian pyramids separates an image by frequency. Second, note that the hybrid image is the composition of the low frequencies from one image with the high frequencies from the second. So Given a Laplacian pyramid of size N, and a cut off frequency of C. We can build a new Laplacian pyramid starting with the C low frequency Laplacian images from the first image, and finish N - C high frequency Laplacian images from the second. We can rebuild this hybrid Laplacian pyramid using the process described previously, and the result will be a hybrid image.

Code Overview

The bulk of the heavy lifting for this project is done in two .m function files: pyramids.m, which builds the Guassian and Laplacian pyramids, and hybridImage.m which builds the final hybrid-image from the newly created pyramids. Also included is printPyramid.m, printGaus.m, and printLapl, which are short functions that I wrote to print out the pyramids. Both pyramids.m, and hybridImage.m are annotated and can be found in the /code directory. Some keys aspects from each function are examinded below.

pyramids.m

gf = fspecial('gaussian', 9,2);
gaus = cell(n + 1,1);
lapl = cell(n + 1,1);
for depth = 1:n
imagega = imfilter(image, gf,'symmetric','same');
gaus{depth,1} = imagega;
imagelp = zeros(size(imagega));
imagelp(:,:) = image(:,:) - imagega(:,:);
lapl{depth,1} = imagelp;
image = imresize(image,.5);
end
gaus{n+1,1} = image;
lapl{n+1,1} = image;

Building the Gaussian and Laplacian was very simple in matlab. I chose to use a cell-array which allowed me to store different size images inside the same array. This design choice reduced the complexity of my code. The code also represents the algorithm as described in the paragraphs above.

Building the Hybrid Laplacian Pyramid (hybridImage.m)

for k = 0:sz-1
if k <= cutoff1
hybridlapl(sz-k,1) = laplacian1(sz-k,1);
end
if k >= cutoff2
hybridlapl(sz-k,1) = laplacian2(sz-k,1);
end
end

Given my design decisions, it was very simple to grab the low frequncies from the first laplacian, and the higher frequiences from the second to build a new laplacian pyramid.

Hybrid Image from the Laplacian Pyramid (hybridImage.m)

image = cell2mat(hybridlapl(sz,1));
for k = 1:sz-1
image = imresize(image,2);
image = imfilter(image, gf,'symmetric','same'); r
lap = cell2mat(hybridlapl(sz-k));

    ...

image(:,:) = image(:,:) + lap(:,:);
end

Again, using a cell array helps make this process really simple.

Image Gallery

Car - Marilyn cutoff of 6

Car - Rhino cutoff of 6

Einstein Cat cutoff of 6

Examples of my Guassian and Laplacian Pyramid printouts