图像拼接 - 多波段融合
程序员文章站
2022-07-14 16:52:06
...
转自:https://blog.csdn.net/smallflyingpig/article/details/61200497
概述
图像拼接一般包括warp(映射), compensation(光照补偿)和blend(融合)三部分。
warp部分主要取决于相机参数估计的准确性,光照补偿主要用于解决不同图像曝光不同所带来的输出图像的不同局部的光照差异,而blend则用于融合不同图像之间的重叠部分,一般使用线性加权的方式来得到最终的输出图像。
多波段融合(multi blend)
多波段融合的基本思想是图像可以分解为不同频率的图像的叠加(类似于傅里叶变换),在不同的频率上,应该使用不同的权重来进行融合,在低频部分应该使用波长较宽的加权信号(例如高斯核函数中sigma比较大),在高频部分应该使用较窄的加权信号(例如高斯核函数的sigma比较小),其算法如下:
- 计算输入图像的高斯金字塔。如果输入图像是A,BA,B,则计算GA0,GA1,GA2,GA3,…GA0,GA1,GA2,GA3,…和GB0,GB1,GB2,GB3,…GB0,GB1,GB2,GB3,…(如何计算高斯金字塔?)
- 计算输入图像的拉普拉斯金字塔。记为LA0,LA1,LA2,LA3,…LA0,LA1,LA2,LA3,…和LB0,LB1,LB2,LB3,…LB0,LB1,LB2,LB3,…(如何计算拉普拉斯金字塔?)
- 将处于同一级的拉普拉斯金字塔进行融合。例如在拼接缝两侧使用简单的线性融合。记输出图像为CC,则这里得到LC0,LC1…LC0,LC1…
- 将高层的拉普拉斯金字塔依次扩展直至和LC0LC0相同分辨率。我们记做LC00,LC11,LC22…LC00,LC11,LC22…
- 将4中得到的图像依次叠加,则得到最终的输出图像CC。
代码实现
使用matlab实现多波段算法如下:
function C = multi_blend(A, B);
%resize A,B,C to the same size
A_size = size(A);
B_size = size(B);
C_size = [512,512];
if(A_size ~= C_size)
A = imresize(A,C_size);
end
if(B_size ~= C_size)
B = imresize(B,C_size);
end
%gaussian kernel
kernel=fspecial('gaussian',[5 5],1);
%obtain the Gauss Pyramid
G_A0 = A;
G_A1 = conv2(G_A0,kernel,'same');
G_A1 = G_A1(2:2:size(G_A1,1),2:2:size(G_A1,2));
G_A2 = conv2(G_A1,kernel,'same');
G_A2 = G_A2(2:2:size(G_A2,1),2:2:size(G_A2,2));
G_A3 = conv2(G_A2,kernel,'same');
G_A3 = G_A3(2:2:size(G_A3,1),2:2:size(G_A3,2));
G_A4 = conv2(G_A3,kernel,'same');
G_A4 = G_A4(2:2:size(G_A4,1),2:2:size(G_A4,2));
G_A5 = conv2(G_A4,kernel,'same');
G_A5 = G_A5(2:2:size(G_A5,1),2:2:size(G_A5,2));
G_B0 = B;
G_B1 = conv2(G_B0,kernel,'same');
G_B1 = G_B1(2:2:size(G_B1,1),2:2:size(G_B1,2));
G_B2 = conv2(G_B1,kernel,'same');
G_B2 = G_B2(2:2:size(G_B2,1),2:2:size(G_B2,2));
G_B3 = conv2(G_B2,kernel,'same');
G_B3 = G_B3(2:2:size(G_B3,1),2:2:size(G_B3,2));
G_B4 = conv2(G_B3,kernel,'same');
G_B4 = G_B4(2:2:size(G_B4,1),2:2:size(G_B4,2));
G_B5 = conv2(G_B4,kernel,'same');
G_B5 = G_B5(2:2:size(G_B5,1),2:2:size(G_B5,2));
%get Laplacian Pyramid
L_A0 = double(G_A0)-imresize(G_A1,size(G_A0));
L_A1 = double(G_A1)-imresize(G_A2,size(G_A1));
L_A2 = double(G_A2)-imresize(G_A3,size(G_A2));
L_A3 = double(G_A3)-imresize(G_A4,size(G_A3));
L_A4 = double(G_A4)-imresize(G_A5,size(G_A4));
L_A5 = double(G_A5);
L_B0 = double(G_B0)-imresize(G_B1,size(G_B0));
L_B1 = double(G_B1)-imresize(G_B2,size(G_B1));
L_B2 = double(G_B2)-imresize(G_B3,size(G_B2));
L_B3 = double(G_B3)-imresize(G_B4,size(G_B3));
L_B4 = double(G_B4)-imresize(G_B5,size(G_B4));
L_B5 = double(G_B5);
%construct the mask
size0 = size(L_A0);
mask0 = zeros(size0);
mask0(:,1:size0(2)/2)=1;
mask0(:,size0(2)/2-5:1:size0(2)/2+5)=repmat(1:-0.1:0,[size0(1) 1]);
size1 = size(L_A1);
mask1 = zeros(size1);
mask1(:,1:size1(2)/2)=1;
mask1(:,size1(2)/2-5:1:size1(2)/2+5)=repmat(1:-0.1:0,[size1(1) 1]);
size2 = size(L_A2);
mask2 = zeros(size2);
mask2(:,1:size2(2)/2)=1;
mask2(:,size2(2)/2-5:1:size2(2)/2+5)=repmat(1:-0.1:0,[size2(1) 1]);
size3 = size(L_A3);
mask3 = zeros(size3);
mask3(:,1:size3(2)/2)=1;
mask3(:,size3(2)/2-5:1:size3(2)/2+5)=repmat(1:-0.1:0,[size3(1) 1]);
size4 = size(L_A4);
mask4 = zeros(size4);
mask4(:,1:size4(2)/2)=1;
mask4(:,size4(2)/2-5:1:size4(2)/2+5)=repmat(1:-0.1:0,[size4(1) 1]);
size5 = size(L_A5);
mask5 = zeros(size5);
mask5(:,1:size5(2)/2)=1;
mask5(:,size5(2)/2-5:1:size5(2)/2+5)=repmat(1:-0.1:0,[size5(1) 1]);
%obtain the output
L_C0 = L_A0 .* mask0 + L_B0 .* (1-mask0);
L_C1 = L_A1 .* mask1 + L_B1 .* (1-mask1);
L_C2 = L_A2 .* mask2 + L_B2 .* (1-mask2);
L_C3 = L_A3 .* mask3 + L_B3 .* (1-mask3);
L_C4 = L_A4 .* mask4 + L_B4 .* (1-mask4);
L_C5 = L_A5 .* mask5 + L_B5 .* (1-mask5);
C = L_C0+imresize(L_C1,size0)+imresize(L_C2,size0)+imresize(L_C3,size0)+imresize(L_C4,size0)+imresize(L_C5,size0);
figure(1);
imshow(A);
figure(2);
imshow(B);
figure(3);
imshow(uint8(C));
end
实验效果:
输入两张光照差别很大的图像:
左半部分使用左图,右半部分使用右图,进行多波段融合得到如下:
每个波段融合使用的掩膜如下(白色代表左图成分,黑色代表右图成分):
其中前四幅图为显示方便做了偏移处理。
参考
- P. Burt and E. Adelson. A multiresolution spline with application to image mosaics. ACM Transactions on Graphics, 2(4):217–236, 1983.
- Matthew Brown and David G. Lowe. Automatic Panoramic Image Stitching using Invariant Features.