Pyramid and Pyramid Blending

9
Exercise Chapter 3: Image Processing Do Yen Exercise of Pyramids and Wavelets Exercise 3.19: Construct an image pyramid. Background: 2 steps to create an image pyramid: – Apply a low-pass filter to the original image (many kinds of kernel) – Create a new image whose resolution in each dimension is half of the original image (Discard one row / column every other one) We use the low-pass filter to filter out the details information of the image, that is not necessary for the down sample of image. Code function [ pyr ] = genPyr( img, pyramidType, filterType, level ) pyr = cell(1,level); switch pyramidType 1

Transcript of Pyramid and Pyramid Blending

Page 1: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

Exercise of Pyramids and Wavelets

Exercise 3.19: Construct an image pyramid.

Background:

2 steps to create an image pyramid:

– Apply a low-pass filter to the original image (many kinds of kernel)

– Create a new image whose resolution in each dimension is half of the original image (Discard one row / column every other one)

We use the low-pass filter to filter out the details information of the image, that is not necessary for the down sample of image.

Code

function [ pyr ] = genPyr( img, pyramidType, filterType, level )pyr = cell(1,level); switch pyramidType case 'gaussian' pyr{1} = img; for p = 2:level pyr{p} = pyr_reduce(pyr{p-1}, filterType); end for p = level-1:-1:1 % adjust the image size osz = size(pyr{p+1})*2-1;

1

Page 2: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

pyr{p} = pyr{p}(1:osz(1),1:osz(2),:); end case 'laplacian' pyr{1} = img; for p = 2:level pyr{p} = pyr_reduce(pyr{p-1}, filterType); end for p = level-1:-1:1 % adjust the image size osz = size(pyr{p+1})*2-1; pyr{p} = pyr{p}(1:osz(1),1:osz(2),:); end for p = 1:level-1 prediction = pyr_expand(pyr{p+1}); osz = size(pyr{p}); prediction = prediction(1:osz(1),1:osz(2),:); inputL = pyr{p}; lapla = inputL - prediction; pyr{p} = lapla; endendend

function [ imgout ] = pyr_reduce( img, filterType )%PYR_REDUCE Image pyramid reduction switch filterType case 'block' kernel = ones(2,2)/4; case 'burt' cw = .375; % kernel centre weight, same as MATLAB func impyramid. ker1d = [.25-cw/2 .25 cw .25 .25-cw/2]; kernel = kron(ker1d,ker1d'); case 'tap' ker1d = [1 6 15 20 15 6 1]; ker1d = ker1d./sum(ker1d); kernel = kron(ker1d,ker1d'); end % cw = .375; % kernel centre weight, same as MATLAB func impyramid. 0.6 % ker1d = [.25-cw/2 .25 cw .25 .25-cw/2]; % kernel = kron(ker1d,ker1d'); img = im2double(img); sz = size(img); imgout = []; for p = 1:size(img,3) img1 = img(:,:,p); imgFiltered = imfilter(img1,kernel,'replicate','same'); imgout(:,:,p) = imgFiltered(1:2:sz(1),1:2:sz(2)); endend

2

Page 3: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

function [ imgout ] = pyr_expand( img ) imgout = zeros(osz(1),osz(2),size(img,3)); imgout = imresize(img, 2, 'bicubic');end

Results with 3 kinds of kernel

2x2 block filtering

0.2500 0.2500

0.2500 0.2500

Burt and Adelson’s binomial kernel 1/16 ( 1, 4, 6, 4, 1)

0.0625 0.2500 0.3750 0.2500 0.0625

3

Page 4: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

High-quality seven tap filtero 0.0156 0.0938 0.2344 0.3125 0.2344 0.0938 0.0156

The quality of the pyramid depends on the detail remaining in the image after down-sampling. Low-pass leaves a fair amount of high-frequency detail. With a seven-tap filter, the low pass is kept the most, so it’s the best decimation filters.

Exercise 3.20

Write a program that takes as input two color images and a binary mask image and produces the Laplacian pyramid blend of the two images

Code:

4

Page 5: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

close allclearimga = im2double(imread('apple1.jpg'));imgb = im2double(imread('orange1.jpg')); % size(imga) = size(imgb)imga = imresize(imga,[size(imgb,1) size(imgb,2)]);[M N ~] = size(imga); v = 230;level = 5;limga = genPyr(imga,'laplacian','burt',level); % the Laplacian pyramidlimgb = genPyr(imgb,'laplacian','burt',level); maska = zeros(size(imga));maska(:,1:v,:) = 1;maskb = 1-maska;maskaPyr = genPyr(maska,'gaussian','burt',level);maskbPyr = genPyr(maskb,'gaussian','burt',level); % blurh = fspecial('gauss',30,15); % feather the border% maska = imfilter(maska,blurh,'replicate');% maskb = imfilter(maskb,blurh,'replicate'); limgo = cell(1,level); % the blended pyramidfor p = 1:level [Mp Np ~] = size(limga{p});% maskap = imresize(maska,[Mp Np]);% maskbp = imresize(maskb,[Mp Np]); maskap = maskaPyr{p}; maskbp = maskbPyr{p}; limgo{p} = limga{p}.*maskap + limgb{p}.*maskbp; endimgo = pyrReconstruct(limgo);figure,imshow(imgo) % blend by pyramidimgo1 = maska.*imga+maskb.*imgb;figure,imshow(imgo1) % blend by feathering

function [ img ] = pyrReconstruct( pyr ) for p = length(pyr)-1:-1:1 prediction = pyr_expand(pyr{p+1}); osz = size(pyr{p}); prediction = prediction(1:osz(1),1:osz(2),:); pyr{p} = pyr{p}+ prediction;endimg = pyr{1}; end

1. Construct the Laplacian pyramid for each image: Function genPyr above

5

Page 6: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

limga = genPyr(imga,'laplacian','burt',level); % the Laplacian pyramidlimgb = genPyr(imgb,'laplacian','burt',level);

2. Construct the Gaussian pyramid for the two mask images (the input image and its complement).

maska = zeros(size(imga));maska(:,1:v,:) = 1;maskb = 1-maska;maskaPyr = genPyr(maska,'gaussian','burt',level);maskbPyr = genPyr(maskb,'gaussian','burt',level);

3. Multiply each Laplacian image by its corresponding mask and sum the images

for p = 1:level [Mp Np ~] = size(limga{p});% maskap = imresize(maska,[Mp Np]);% maskbp = imresize(maskb,[Mp Np]); maskap = maskaPyr{p}; maskbp = maskbPyr{p}; limgo{p} = limga{p}.*maskap + limgb{p}.*maskbp; end

4. Reconstruct the final image from the blended Laplacian pyramid

imgo = pyrReconstruct(limgo);

Some results :

Input:

Output: Pyramid blending

6

Page 7: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

Just cut in the middle of the two images

Other results:

7

Page 8: Pyramid and Pyramid Blending

Exercise Chapter 3: Image ProcessingDo Yen

Output:

Using blending gave us more natural image.

8