Вы находитесь на странице: 1из 8

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 case 'gaussian' pyr{1} = img; for p = 2:level pyr{p} = pyr_reduce(pyr{p-1}, filterType); end

Exercise Chapter 3: Image Processing Do Yen

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 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; end end end 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');

Exercise Chapter 3: Image Processing Do Yen


imgout(:,:,p) = imgFiltered(1:2:sz(1),1:2:sz(2)); end end 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 Adelsons binomial kernel 1/16 ( 1, 4, 6, 4, 1) 0.0625 0.2500 0.3750 0.2500 0.0625

Exercise Chapter 3: Image Processing Do Yen

High-quality seven tap filter o 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 downsampling. Low-pass leaves a fair amount of high-frequency detail. With a seven-tap filter, the low pass is kept the most, so its 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

Exercise Chapter 3: Image Processing Do Yen


close all clear imga = 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 pyramid limgb = 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 pyramid 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 imgo = pyrReconstruct(limgo); figure,imshow(imgo) % blend by pyramid imgo1 = 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; end img = pyr{1}; end

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

Exercise Chapter 3: Image Processing Do Yen


limga = genPyr(imga,'laplacian','burt',level); % the Laplacian pyramid limgb = 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 nal image from the blended Laplacian pyramid


imgo = pyrReconstruct(limgo);

Some results : Input:

Output: Pyramid blending

Exercise Chapter 3: Image Processing Do Yen

Just cut in the middle of the two images

Other results:

Exercise Chapter 3: Image Processing Do Yen

Output:

Using blending gave us more natural image.