请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 6483|回复: 1

图像拉普拉斯金字塔融合

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 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

  1. close all
  2. clear
  3. imga = im2double(imread('apple1.jpg'));
  4. imgb = im2double(imread('orange1.jpg')); % size(imga) = size(imgb)
  5. figure,
  6. subplot(121),imshow(imga,[])
  7. subplot(122),imshow(imgb,[])

  8. imga = imresize(imga,[size(imgb,1) size(imgb,2)]);
  9. [M,N] = size(imga(:,:,1));

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

  14. maska = zeros(size(imga));
  15. maska(:,1:v,:) = 1;
  16. maskb = 1-maska;
  17. blurh = fspecial('gauss',30,15); % feather the border
  18. maska = imfilter(maska,blurh,'replicate');
  19. maskb = imfilter(maskb,blurh,'replicate');

  20. limgo = cell(1,level); % the blended pyramid
  21. for p = 1:level
  22.         [Mp Np ~] = size(limga{p});
  23.         maskap = imresize(maska,[Mp Np]);
  24.         maskbp = imresize(maskb,[Mp Np]);
  25.         limgo{p} = limga{p}.*maskap + limgb{p}.*maskbp;
  26. end
  27. imgo = pyrReconstruct(limgo);
  28. figure,imshow(imgo) % blend by pyramid
  29. imgo1 = maska.*imga+maskb.*imgb;
  30. figure,imshow(imgo1) % blend by feathering
复制代码
pyr_expand( img ):
  1. function [ imgout ] = pyr_expand( img )
  2. %PYR_EXPAND  Image pyramid expansion
  3. %   B = PYR_EXPAND( A )  If A is M-by-N, then the size of B
  4. %        is (2*M-1)-by-(2*N-1). Support gray or rgb image.
  5. %        B will be transformed to double class.
  6. %        Results the same w/ MATLAB func impyramid.

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

  11. % expand [a] to [A00 A01;A10 A11] with 4 kernels
  12. ker00 = kernel(1:2:kw,1:2:kw); % 3*3
  13. ker01 = kernel(1:2:kw,2:2:kw); % 3*2
  14. ker10 = kernel(2:2:kw,1:2:kw); % 2*3
  15. ker11 = kernel(2:2:kw,2:2:kw); % 2*2

  16. img = im2double(img);
  17. sz = size(img(:,:,1));
  18. osz = sz*2-1;
  19. imgout = zeros(osz(1),osz(2),size(img,3));

  20. for p = 1:size(img,3)
  21.         img1 = img(:,:,p);
  22.         img1ph = padarray(img1,[0 1],'replicate','both'); % horizontally padded
  23.         img1pv = padarray(img1,[1 0],'replicate','both'); % horizontally padded
  24.        
  25.         img00 = imfilter(img1,ker00,'replicate','same');
  26.         img01 = conv2(img1pv,ker01,'valid'); % imfilter doesn't support 'valid'
  27.         img10 = conv2(img1ph,ker10,'valid');
  28.         img11 = conv2(img1,ker11,'valid');
  29.        
  30.         imgout(1:2:osz(1),1:2:osz(2),p) = img00;
  31.         imgout(2:2:osz(1),1:2:osz(2),p) = img10;
  32.         imgout(1:2:osz(1),2:2:osz(2),p) = img01;
  33.         imgout(2:2:osz(1),2:2:osz(2),p) = img11;
  34. end

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

  8. pyr = cell(1,level);
  9. pyr{1} = im2double(img);
  10. for p = 2:level
  11.         pyr{p} = pyr_reduce(pyr{p-1});
  12. end
  13. if strcmp(type,'gauss'), return; end

  14. for p = level-1:-1:1 % adjust the image size
  15.         osz = size(pyr{p+1})*2-1;
  16.         pyr{p} = pyr{p}(1:osz(1),1:osz(2),:);
  17. end

  18. for p = 1:level-1
  19.         pyr{p} = pyr{p}-pyr_expand(pyr{p+1});
  20. end
复制代码
[ imgout ] = pyr_reduce( img ):
  1. function [ imgout ] = pyr_reduce( img )
  2. %PYR_REDUCE  Image pyramid reduction
  3. %   B = PYR_REDUCE( A )  If A is M-by-N, then the size of B
  4. %        is ceil(M/2)-by-ceil(N/2). Support gray or rgb image.
  5. %        B will be transformed to double class.
  6. %        Results the same w/ MATLAB func impyramid.

  7. kernelWidth = 5; % default
  8. cw = .375; % kernel centre weight, same as MATLAB func impyramid. 0.6 in the Paper
  9. ker1d = [.25-cw/2 .25 cw .25 .25-cw/2];
  10. kernel = kron(ker1d,ker1d');

  11. img = im2double(img);
  12. sz = size(img);
  13. imgout = [];

  14. for p = 1:size(img,3)
  15.         img1 = img(:,:,p);
  16.         imgFiltered = imfilter(img1,kernel,'replicate','same');
  17.         imgout(:,:,p) = imgFiltered(1:2:sz(1),1:2:sz(2));
  18. end

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

  5. for p = length(pyr)-1:-1:1
  6.         pyr{p} = pyr{p}+pyr_expand(pyr{p+1});
  7. end
  8. img = pyr{1};

  9. end
复制代码






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

0

主题

1

帖子

0

金钱

新手上路

Rank: 1

积分
4
发表于 2022-4-4 14:32:03 | 显示全部楼层

感谢楼主,好人一生平安
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-28 18:10 , Processed in 0.214913 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表