Halcom 发表于 2017-5-23 22:31:19

图像拉普拉斯金字塔融合

图像拉普拉斯金字塔融合:
【1】http://blog.csdn.net/abcjennifer/article/details/7628655
【2】http://www.cnblogs.com/silence-hust/p/4193208.html
【3】http://blog.csdn.net/yiluoyan/article/details/46386107
【4】http://www.ilovematlab.cn/thread-442912-1-1.html

close all
clear
imga = im2double(imread('apple1.jpg'));
imgb = im2double(imread('orange1.jpg')); % size(imga) = size(imgb)
figure,
subplot(121),imshow(imga,[])
subplot(122),imshow(imgb,[])

imga = imresize(imga,);
= size(imga(:,:,1));

v = 230;
level = 5;
limga = genPyr(imga,'lap',level); % the Laplacian pyramid
limgb = genPyr(imgb,'lap',level);

maska = zeros(size(imga));
maska(:,1:v,:) = 1;
maskb = 1-maska;
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
        = size(limga{p});
        maskap = imresize(maska,);
        maskbp = imresize(maskb,);
        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 featheringpyr_expand( img ):function [ imgout ] = pyr_expand( img )
%PYR_EXPANDImage pyramid expansion
%   B = PYR_EXPAND( A )If A is M-by-N, then the size of B
%        is (2*M-1)-by-(2*N-1). Support gray or rgb image.
%        B will be transformed to double class.
%        Results the same w/ MATLAB func impyramid.

kw = 5; % default kernel width
cw = .375; % kernel centre weight, same as MATLAB func impyramid. 0.6 in the Paper
ker1d = [.25-cw/2 .25 cw .25 .25-cw/2];
kernel = kron(ker1d,ker1d')*4;

% expand to with 4 kernels
ker00 = kernel(1:2:kw,1:2:kw); % 3*3
ker01 = kernel(1:2:kw,2:2:kw); % 3*2
ker10 = kernel(2:2:kw,1:2:kw); % 2*3
ker11 = kernel(2:2:kw,2:2:kw); % 2*2

img = im2double(img);
sz = size(img(:,:,1));
osz = sz*2-1;
imgout = zeros(osz(1),osz(2),size(img,3));

for p = 1:size(img,3)
        img1 = img(:,:,p);
        img1ph = padarray(img1,,'replicate','both'); % horizontally padded
        img1pv = padarray(img1,,'replicate','both'); % horizontally padded
       
        img00 = imfilter(img1,ker00,'replicate','same');
        img01 = conv2(img1pv,ker01,'valid'); % imfilter doesn't support 'valid'
        img10 = conv2(img1ph,ker10,'valid');
        img11 = conv2(img1,ker11,'valid');
       
        imgout(1:2:osz(1),1:2:osz(2),p) = img00;
        imgout(2:2:osz(1),1:2:osz(2),p) = img10;
        imgout(1:2:osz(1),2:2:osz(2),p) = img01;
        imgout(2:2:osz(1),2:2:osz(2),p) = img11;
end

end[ pyr ] = genPyr( img, type, level )
function [ pyr ] = genPyr( img, type, level )
%GENPYR generate Gaussian or Laplacian pyramid
%   PYR = GENPYR(A,TYPE,LEVEL) A is the input image,
%        can be gray or rgb, will be forced to double.
%        TYPE can be 'gauss' or 'laplace'.
%        PYR is a 1*LEVEL cell array.
% Yan Ke @ THUEE, xjed09@gmail.com

pyr = cell(1,level);
pyr{1} = im2double(img);
for p = 2:level
        pyr{p} = pyr_reduce(pyr{p-1});
end
if strcmp(type,'gauss'), return; 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
        pyr{p} = pyr{p}-pyr_expand(pyr{p+1});
end
[ imgout ] = pyr_reduce( img ):
function [ imgout ] = pyr_reduce( img )
%PYR_REDUCEImage pyramid reduction
%   B = PYR_REDUCE( A )If A is M-by-N, then the size of B
%        is ceil(M/2)-by-ceil(N/2). Support gray or rgb image.
%        B will be transformed to double class.
%        Results the same w/ MATLAB func impyramid.

kernelWidth = 5; % default
cw = .375; % kernel centre weight, same as MATLAB func impyramid. 0.6 in the Paper
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));
end

end[ img ] = pyrReconstruct( pyr )
function [ img ] = pyrReconstruct( pyr )
%PYRRECONSTRUCT Uses a Laplacian pyramid to reconstruct a image
%   IMG = PYRRECONSTRUCT(PYR) PYR should be a 1*level cell array containing
%   the pyramid, SIZE(PYR{i}) = SIZE(PYR{i-1})*2-1

for p = length(pyr)-1:-1:1
        pyr{p} = pyr{p}+pyr_expand(pyr{p+1});
end
img = pyr{1};

end





linuvzg 发表于 2022-4-4 14:32:03


感谢楼主,好人一生平安
页: [1]
查看完整版本: 图像拉普拉斯金字塔融合