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

# Exercise Chapter 3: Image Processing Do Yen

## 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

## 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

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).

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

## 4. Reconstruct the nal image from the blended Laplacian pyramid

imgo = pyrReconstruct(limgo);

Other results:

Output:

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2021 Scribd Inc.