Image Mosaicing with Matlab
Keywords: Matlab, Image Mosaicing, Mosaicing, Image Stitching
These 2 photos depict examples of a scene that can be stitched together in Matlab. The big picture problem is to mosaic them together correctly. Solving this partially or completely is important because it will allow command and control (C2) teams to assess a disaster situation more accurately and quickly. This tutorial shows you how to stitch images together in Matlab and takes approximately 1 to 3 hours to complete, depending on how many images you wish to stitch together.
 
Motivation and Audience
This tutorial's motivation is to give next year's design team, and all others interested, a know-how of mosaicing images in Matlab. Readers of this tutorial assumes the reader has the following background and interests:
Know how to open a Matlab script
Also know how to use the help command in Matlab
Knowledge of Photoshop, SnagIt or similar programs is a plus
Desire to stitch together images to gain full view of a scene
In addition, would like to learn more about Matlab commands and scripts
The rest of the tutorial is presented as follows:
Parts List and Sources
Construction

Programming

Final Words
Parts List and Sources
US-based vendors to obtain material to complete this tutorial include:
Mathworks

TechSmith
To complete this tutorial, you'll need the following items:
PART DESCRIPTION VENDOR PART PRICE QTY
Matlab software Mathworks Matlab vers. 5.x ??? 1
SnagIt software TechSmith SnagIt 6.0 (trial) free for 30 days 1
Construction
This section gives step-by-step instructions along with photos to mosaic images in Matlab. The photos at the beginning of this page can be used as examples.
Step 1
Select 2 images (such as the ones at the top of this page) to mosaic. Preselect 2 matching points on these images to match up within the Matlab program.
Step 2
Name the files appropiately and save them within the Matlab folder on the personal computer you are working on.
(example: above left photo is titled "left.jpg" and above right photo is titled "right.jpg".)
Step 3
Open Matlab program on the personal computer you are working on.
Step 4
Make sure the mosaic.m script is also saved within the Matlab folder on the personal computer you are working on.
Step 5
In the Matlab command window, select the File menu, and select run script. A window will appear asking you for the file name of the script you wish to run. Select Browse, and find the mosaic.m script, highlight it, click on Open, and click on OK.
Step 6
A new window will appear entitled 'Figure No. 1'.  The first input image will appear (in our example: 'left.jpg'). The mouse will move the crosshair lines. Click on the first point which you preselected in Step 1. A red cross will appear at that point with the number 1 next to it.
Step 7
Use the mouse again to click on the 2nd pre-selected point from step 1. A red cross with the number 2 will appear next to that point. Also, the second input image (in our example, 'right.jpg') will also now appear in the Figure 1 window.
Step 8
Click on the matching point within the second input image for point 1. A red cross with a number 1 will appear at that point in the second input image.
Step 9
Use the mouse again to click on the matching point within the second input image for point 2.
When this is done, a new window will appear entitled Figure No. 2. This is the mosaic image of the 2 input images.
Also, the window entitled Figure No. 1 will have a red cross with a 1 and a red cross with a 2 next to it in both the first input image and second input image, as shown below.
Step 10
Review the mosaic image in Figure No. 2.
If the image is distorted, at odd angles, or in general not satisfactory to what you had hoped to achieve, pre-select different points within the images, and repeat Steps 1 through 9 again.
If the image is satisfactory, click on the File menu within the Figure No. 2 window. Click on Save As, and name the file. This will only save this file as an .fig file for Matlab. ***Keep the Figure No. 2 window open***
Step 11
Use software such as SnagIt to capture the mosaic image, with or without the graph lines, and save it to a .jpg, .gif, .bmp, or other type of file format.
The source code to image mosaicing in Matlab is provided below:

To be compiled with Matlab Editor/Debugger
Note: download
warping.zip rather than cutting and pasting from below.
Use unzipping software to unzip the warping file. The mosaic m file is included.
Example of mosiac.m script for mosaicing images in Matlab:
% load input images
I1 = double(imread('left.jpg'));
[h1 w1 d1] = size(I1);
I2 = double(imread('right.jpg'));
[h2 w2 d2] = size(I2); 

% show input images and prompt for correspondences
figure; subplot(1,2,1); image(I1/255); axis image; hold on;
title('first input image');
[X1 Y1] = ginput2(2); % get two points from the user
subplot(1,2,2); image(I2/255); axis image; hold on;
title('second input image');
[X2 Y2] = ginput2(2); % get two points from the user 

% estimate parameter vector (t)
Z = [ X2'; Y2' ; Y2' -X2' ; 1 1 0 0; ; 0 0 1 1 ]';
xp = [ X1 ; Y1 ];
t = Z \ xp; % solve the linear system
a = t(1); % = s cos(alpha)
b = t(2); % = s sin(alpha)
tx = t(3);
ty = t(4); 

% construct transformation matrix (T)
T = [a b tx ; -b a ty ; 0 0 1]; 

% warp incoming corners to determine the size of the output image (in to out)
cp = T*[ 1 1 w2 w2 ; 1 h2 1 h2 ; 1 1 1 1 ];
Xpr = min( [ cp(1,:) 0 ] ) : max( [cp(1,:) w1] ); % min x : max x
Ypr = min( [ cp(2,:) 0 ] ) : max( [cp(2,:) h1] ); % min y : max y
[Xp,Yp] = ndgrid(Xpr,Ypr);
[wp hp] = size(Xp); % = size(Yp) 

% do backwards transform (from out to in)
X = T \ [ Xp(:) Yp(:) ones(wp*hp,1) ]'; % warp 

% re-sample pixel values with bilinear interpolation
clear Ip;
xI = reshape( X(1,:),wp,hp)';
yI = reshape( X(2,:),wp,hp)';
Ip(:,:,1) = interp2(I2(:,:,1), xI, yI, '*bilinear'); % red
Ip(:,:,2) = interp2(I2(:,:,2), xI, yI, '*bilinear'); % green
Ip(:,:,3) = interp2(I2(:,:,3), xI, yI, '*bilinear'); % blue 

% offset and copy original image into the warped image
offset =  -round( [ min( [ cp(1,:) 0 ] ) min( [ cp(2,:) 0 ] ) ] );
Ip(1+offset(2):h1+offset(2),1+offset(1):w1+offset(1),:) =
double(I1(1:h1,1:w1,:)); 

% show the result
figure; image(Ip/255); axis image;
title('mosaic image');









mosaic.m Fuller Code Description
The mosaic script operates as follows (fill in the blank) 
Final Words
This tutorial's objective was to teach the reader how to stitch together images by mosaicing them using Matlab. Complete (choose: construction details, source code and program descriptions) for (fill in the blank). Once the concepts were conveyed the reader could successfully mosaic their own images together. .
Speculating future work, derived from this tutorial, includes improving timeliness of the mosaicing process. In the big picture, the problem of disaster mitigation can be aided greatly by images of a disaster scene stitched together and given to command and control teams.
Click here to email me