Pyramid and Pyramid Blending

Download Pyramid and Pyramid Blending

Post on 31-Aug-2014

110 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

<p>Exercise Chapter 3: Image Processing Do Yen</p> <p>Exercise of Pyramids and Wavelets</p> <p>Exercise 3.19: Construct an image pyramid. Background:</p> <p>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. Codefunction [ 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</p> <p>1</p> <p>Exercise Chapter 3: Image Processing Do Yen</p> <p>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');</p> <p>2</p> <p>Exercise Chapter 3: Image Processing Do Yenimgout(:,:,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</p> <p>Results with 3 kinds of kernel 2x2 block filtering 0.2500 0.2500 0.2500 0.2500</p> <p>Burt and Adelsons binomial kernel 1/16 ( 1, 4, 6, 4, 1) 0.0625 0.2500 0.3750 0.2500 0.0625</p> <p>3</p> <p>Exercise Chapter 3: Image Processing Do Yen</p> <p>High-quality seven tap filter o 0.0156 0.0938 0.2344</p> <p>0.3125</p> <p>0.2344</p> <p>0.0938</p> <p>0.0156</p> <p>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.</p> <p>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</p> <p>Exercise Chapter 3: Image Processing Do Yenclose 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</p> <p>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</p> <p>1. Construct the Laplacian pyramid for each image: Function genPyr above</p> <p>5</p> <p>Exercise Chapter 3: Image Processing Do Yenlimga = genPyr(imga,'laplacian','burt',level); % the Laplacian pyramid limgb = genPyr(imgb,'laplacian','burt',level);</p> <p>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);</p> <p>3. Multiply each Laplacian image by its corresponding mask and sum the imagesfor 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</p> <p>4. Reconstruct the nal image from the blended Laplacian pyramidimgo = pyrReconstruct(limgo);</p> <p>Some results : Input:</p> <p>Output: Pyramid blending</p> <p>6</p> <p>Exercise Chapter 3: Image Processing Do Yen</p> <p>Just cut in the middle of the two images</p> <p>Other results:</p> <p>7</p> <p>Exercise Chapter 3: Image Processing Do Yen</p> <p>Output:</p> <p>Using blending gave us more natural image.</p> <p>8</p>