Search code examples
imagematlabimage-processingsurfmosaic

Correct image size in image mosaic


I'm implementing image mosaic in Matlab using SURF.the problem is

outputView = imref2d(size(img1)*2);
Ir = imwarp(img2,tform,'OutputView',outputView); 

it produces

enter image description here

i want it something like this

enter image description here

if i change

outputView = imref2d(size(img1)*2);

to

outputView = imref2d(size(img1));

matlab crops the second image so it can fit in first image size after transforming.


Solution

  • Notice that when you warp the image with respect to the target plane, many of the pixels in this new plane are equal to 0. A very rudimentary algorithm is to simply threshold your image so that you find values above 0 then find the largest bounding box that encompasses the non-zero pixels... then crop:

    [rows,cols] = find(Ir(:,:,1) > 0);
    topLeftRow = min(rows);
    topLeftCol = min(cols);
    bottomRightRow = max(rows);
    bottomRightCol = max(cols);
    
    Ir_crop = Ir(topLeftRow:bottomRightRow, topLeftCol:bottomRightCol, :);