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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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
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