The following is a piece of code that forms a Mosaic of two images after computing the Homography Matrix H using RANSAC pror to which SIFT was used to compute the descriptors:
% --------------------------------------------------------------------
% Mosaic
% --------------------------------------------------------------------
box2 = [1 size(im2,2) size(im2,2) 1 ;
1 1 size(im2,1) size(im2,1) ;
1 1 1 1 ] ;
box2_ = inv(H) * box2 ;
box2_(1,:) = box2_(1,:) ./ box2_(3,:) ;
box2_(2,:) = box2_(2,:) ./ box2_(3,:) ;
ur = min([1 box2_(1,:)]):max([size(im1,2) box2_(1,:)]) ;
vr = min([1 box2_(2,:)]):max([siize(im1,1) box2_(2,:)]) ;
[u,v] = meshgrid(ur,vr) ;
im1_ = vl_imwbackward(im2double(im1),u,v) ;
z_ = H(3,1) * u + H(3,2) * v + H(3,3) ;
u_ = (H(1,1) * u + H(1,2) * v + H(1,3)) ./ z_ ;
v_ = (H(2,1) * u + H(2,2) * v + H(2,3)) ./ z_ ;
im2_ = vl_imwbackward(im2double(im2),u_,v_) ;
mass = ~isnan(im1_) + ~isnan(im2_) ;
im1_(isnan(im1_)) = 0 ;
im2_(isnan(im2_)) = 0 ;
mosaic = (im1_ + im2_) ./ mass ;
figure(2) ; clf ;
imagesc(mosaic) ; axis image off ;
title('Mosaic') ;
if nargout == 0, clear mosaic ; end
end
Now I understand we need to warp the images in someway before stitching them using the computed Homography? What then, is the logic behind the definition of "box2" i.e why consider the size of the first and second dimension of im2? Also, what is the function of mass and the lines of code that follow?
The content of
box2
is simply the bounding box (corner coordinates) of the second image;box2_
is then this bounding box transformed into the coordinate system ofim1
- in which you compute the range of coordinates (ur
andvr
) where to projectim2
after transformation.The purpose of variable
mass
is just to indicate how many images cover each pixel: If only one image is nonempty at a given pixel, itsmass(...)
=1 and the result equals to the value from this image. If both images are nonempty,mass(...)
=2 results in computing their mean value.