# Thilina's Blog

## Translation Invariant Image Registration using Phase Correlation – Panorama Imaging on MATLAB

Panorama Imaging is one of most specialized techniques in photography. The ratio of this photo is 2:1 or larger in size. Nowadays even modern mobile phones also have the feature of capturing panorama images, specially accelerometer based techniques have made it easier to capture wonderful pieces of images in panoramic way. Unfortunately in my mobile phone camera, even though it has panoramic imaging, it does not have such flexible feature on creating panorama images, therefore what I do is take two pictures and stitch them using MATLAB.

In this article I will share my experience on stitching two images using phase correlation techniques. In this technique I assume that images are not either rotated or scaled with respect to others. With this assumption I can use Translation Invariant Image registration techniques to create the panorama image.

Basic methodology for the process is as follows.

• Applying a Window function to reduce edge effects of both images(I used Hanning Window)
• 2D-FFT of images • Cross power spectrum of images • Normalized Cross Correlation by applying the 2D-IFT to the Cross Power Spectrum. • Detect the peak point. • Stitch second image from peak location.
• Crop Resulting image to remove the effect from horizontal shift.

In MATLAB the function ‘hanning()’ can used to obtain 1D hanning window, by using the cross product of two hanning windows we can create a 2D hanning window for appliying in input images in prior to 2D-FFT.

```hanning1D_row = hanning(image_width);
hanning1D_col = hanning(image_height);
hanning2D = hanning1D_row*hanning1D_col;
```

Then the hanning window is multiplied in element-wise with two images for obtaining edge effect reduced images.

```image_left_flt = hanning2D.*image_left;
image_right_flt = hanning2D.*image_right;
```

The 2D Fast Fourier Transform is applied to these edge effect reduced images. And they are used to obtain Cross Power Spectrum of images.

```FFT_L = fft2(image_left_flt );
FFT_R = fft2(image_right_flt );
IMF = FFT_L*.conj(FFT_R);
CPS = IMF./abs(IMF);
```

Code for this process is as below.

```clc;
clear all;
close all;
im1 = rgb2gray(im01);
im2 = rgb2gray(im02);

F1 = fftshift(fft2(im1));
F2 = fftshift(fft2(im2));

[r1 c1 d1] = size(im1);
[r2 c2 d2] = size(im2);

F = F1.*conj(F2);
F = F./abs(F);
mag = (ifft2(F));

[X Y] = find((mag == (max(max(mag)))));

if im1(1,1)==im2(end-X,end-Y)
img = zeros(r2-X+r1,c2-Y+c1,3);
img=cast(img,'uint8');
img(1:r2,1:c2,:)=im02;
img(r2-X:r2-X+r1-1,c2-Y:c2-Y+c1-1,:)=im01;
else
img = zeros(X+r2,Y+c2,3);
img=cast(img,'uint8');
img(1:r1,1:c1,:)=im01;
img(X:X+r2-1,Y:Y+c2-1,:)=im02;
end

img=cast(img,'uint8');
imshow(img);
imout = imcrop(img);
```

These are the sample images I used.  After processing before perform the cropping operation, And after cropping., Thank you very much for reading.

2012 March 24 - Posted by | Image Processing, MATLAB, Technology

1. Nice work…….!!! Comment by Nalika | 2012 March 27 | Reply

• Thanks buddy 🙂 🙂 Comment by Thilina S. | 2012 March 27 | Reply

2. Great work…bravo,
but i have some questions if u can help me plz

1.Can u provide us with the code u used to translate the the image?! and if we use grayscale images not RGB what are the changes in the code above???
2. If the image is rotated and scaled also how to recover them??

Actually i have the equations but i don’t know the way of applying them in matlab 😦 Comment by lama | 2012 April 4 | Reply

• Hi, Thank you very much for your comment,
1. If you are using gray scale images just remove the RGB to gray scale conversion (rgb2gray).
2. There is a research paper for this requirement and you can find the MATLAB implementation of this paper in MATLAB file exchange. Comment by Thilina S. | 2012 April 19 | Reply

3. Heya, I came upon your work while researching for a panoramic imaging project and it’s been very helpful. Being a novice though, I have some trouble understanding exactly where you use the first three bits of code you’ve got up there (the hanning bit). Could you explain it a bit more thoroughly please? Comment by John Korn | 2012 May 22 | Reply

4. thank you very much thilina for this great work. if we want to apply the phase correlation to multiple images not for two only, what is the change that can be done on the code? Comment by hamsa | 2012 June 19 | Reply

5. simple explanation along with sample code! what more could i ask for.
Thank you for posting this! Comment by ted | 2014 March 25 | Reply

6. Thanks for awesome code,Can you provide me java code equivalent to this matlab code? Comment by Chris | 2014 April 26 | Reply

7. i need your kind help please, can u help me with the code of image stitching ,using FFT based method, i am trying hard to get the code but getting any help from anywhere,pls reply me as u see Comment by jyoti patel | 2017 January 22 | Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.