Contact info


Mosaicking Tutorial
The original tutorial has been modified with the authors permission.

Keywords: Matlab, Image Mosaicking, Mosaicking, 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 hour to complete.

Motivation and Audience

This tutorial has been created to help you mosaic images using Matlab. Readers of this tutorial assumes the reader has the following background and interests:

  • Know how to use Matlab and modify m files
  • Also know how to use the help command in Matlab
  • Knowledge of Photoshop, Paint, Microsoft Photo Editor 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

US-based vendors to obtain material to complete this tutorial include

To complete this tutorial, you'll need the following items

Matlab Mathworks 5.X$500 1


This section gives step-by-step instructions along with photos on how to mosaic images in Matlab. The photos at the beginning of this page can be used as examples.


1. Create a folder on your desktop called Mosaic.

2. Save 2 images (such as the ones at the top of this page)  and the mosaic.m and ginput2.m files into the folder you just created. (MosaicKit)

3. Open Matlab program on the personal computer you are working on.

 4. Open the mosaic.m file by typing open mosaic

  5. Change the script so that it uses the images you want to mosaic. In our example they are called Left.jpg and Right.jpg. Save the changes and close the window.

6. In the Matlab command window type mosaic

7. A new window will appear entitled 'Figure No. 1'. Select two points, in the first image, that would match two other points on the second image.  Red crosses would mark the desired points.


8. Use the mouse again to click on the second image. Be sure to select the points on the same order you did the first ones.


9. Review the mosaicked image in Figure No. 2. If the image is distorted, at odd angles, or it is not satisfactory in general, run the script one more time. 


10. Go to File > Save As, and save the image as *.jpg

11. Open the image with the software of your choice (Photoshop, Paint, or any other) and make any changes if desired.

12. Repeat steps 4 through 11 to mosaic two more images. You could use the image you just created to add one more sequence to it.

The source code to image mosaicking in Matlab is provided below:

To be compiled with Matlab Editor/Debugger
Note: download the MosaicKit rather than cutting and pasting from below.
Use unzipping software to unzip the file. All the necessary files are included.

Example of mosiac.m script for mosaicing images in Matlab:

I1 = double(imread('left.jpg')); % loads image 1
[h1 w1 d1] = size(I1);
I2 = double(imread('right.jpg')); % load images 2
[h2 w2 d2] = size(I2); 

figure; % creates a new figure window subplot(1,2,1);
% divides current figure into 1(row) by 2(columns), it focuses on the 1st subfigure image(I1/255); % displays the image axis image; % displays the axis on the image hold on;
title('first input image');
[X1 Y1] = ginput2(2); % gets two points from the user
% takes the previous figure window and focuses on the 2nd subfigure 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]; 

% warps 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),:) =

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

mosaic.m Code Description

The mosaic.m script operates as follows:
First, it converts the values of the images from unsigned int to double. This is done
because most of the MATLAB operations use double as default type. Then, it asks for the points to be matched. Once it has this information it performs a transformation based on an estimate. The value of each pixel is then chosen using a bilinear interpolation. Finally, the mosaicked image is displayed.

Final Words

The objective of this tutorial was to teach the reader how to stitch together images by mosaicking them in Matlab. We have created some more examples to give you a better idea of what you can do with it. By now you should be able to mosaic any two images.

To give you an idea about the applications of the system, it could be utilized for disaster migration, by providing just one image of all the affected zones. It could also be used to create rode maps, or monitor changes on a given area.

See more examples

Click here to email me