# Thilina's Blog

## Photo Effects using MATLAB 3.0 – Image warping using MATLAB – Fisheye Similar Effect

The term fisheye was coined in 1906 by American physicist and inventor Robert W. Wood based on how a fish would see an ultra-wide hemispherical view from beneath the water (a phenomenon known as Snell’s window) (Wikipedia). I tried several domain transformation functions to create the fisheye effect and ultimately I ended up with warping an image into a circle which gives somewhat similar result to the fisheye effect. In this article I am going to share my experience on warping an image in to a circle (fisheye effect, my version).

Here is an example of correct fisheye effect.

In my design first I reshaped the input image to a square, which will helps to wrap the image easily in to a circle. Then I divided the image into four small equal sized images which represents the four quadrants of a circle.

Now this each quadrant is again iterated to create a single line of the image in interest as follows. And the pixel values are collected into a matrix.

This matrix represents an arc in the circle which we are going to develop.

This extracted matrix for the 2nd quadrant will be as follows.

Then this extracted component will be placed around the circle.

When this process is continued for next three quadrants we can obtain a fisheye similar effect. Sample MATLAB code for processing the image is as below.

```clear all;
close all;
clc;

fname = 'img_003';

[r0 c0 d0] = size(im);
mx = max(r0,c0);
imo = imresize(im,[mx mx]);

m = floor(mx/2);

im00 = imo(1:m,1:m,:);
im01 = imo(1:m,m+1:end,:);
im11 = imo(m+1:end,m+1:end,:);
im10 = imo(m+1:end,1:m,:);

for i = 1:m
u = im00(m:-1:i,i,:);
v = im00(i,i+1:m,:);
s = [];
s(:,:,1) = [u(:,:,1)' v(:,:,1)];
s(:,:,2) = [u(:,:,2)' v(:,:,2)];
s(:,:,3) = [u(:,:,3)' v(:,:,3)];
cd = size(s,2);
dss(i,i:i+cd-1,:) = s;
R = m;
for j = 1:0.2:cd
a = 90*j/cd;
q = m+1-floor((m-i)*cosd(a));
p = m+1-floor((m-i)*sind(a));
ds(p,q,:) = s(1,floor(j),:);
end
imshow(cast(ds,'uint8'));
pause(0.001);
end

for i = 1:m
u = im01(i,1:m+1-i,:);
v = im01(i:m,m+1-i,:);
s = [];
s(:,:,1) = [u(:,:,1) v(:,:,1)'];
s(:,:,2) = [u(:,:,2) v(:,:,2)'];
s(:,:,3) = [u(:,:,3) v(:,:,3)'];
cd = size(s,2);
dss(i,i:i+cd-1,:) = s;
R = m;
for j = 1:0.2:cd
a = 90*j/cd;
q = m+1+floor((m-i)*cosd(90-a));
p = m+1-floor((m-i)*sind(90-a));
ds(p,q,:) = s(1,floor(j),:);
end
imshow(cast(ds,'uint8'));
pause(0.001);
end

for i = 1:m
u = im11(1:m+1-i,m+1-i,:);
v = im11(m+1-i,m+1-i:-1:1,:);
s = [];
s(:,:,1) = [u(:,:,1)' v(:,:,1)];
s(:,:,2) = [u(:,:,2)' v(:,:,2)];
s(:,:,3) = [u(:,:,3)' v(:,:,3)];
cd = size(s,2);
dss(i,i:i+cd-1,:) = s;
R = m;
for j = 1:0.2:cd
a = 90*j/cd;
q = m+1+floor((m-i)*cosd(a));
p = m+1+floor((m-i)*sind(a));
ds(p,q,:) = s(1,floor(j),:);
end
imshow(cast(ds,'uint8'));
pause(0.001);
end

for i = 1:m
u = im10(m+1-i,m:-1:i,:);
v = im10(m+1-i:-1:1,i,:);
s = [];
s(:,:,1) = [u(:,:,1) v(:,:,1)'];
s(:,:,2) = [u(:,:,2) v(:,:,2)'];
s(:,:,3) = [u(:,:,3) v(:,:,3)'];
cd = size(s,2);
dss(i,i:i+cd-1,:) = s;
R = m;
for j = 1:0.2:cd
a = 90*j/cd;
q = m+1-floor((m-i)*cosd(90-a));
p = m+1+floor((m-i)*sind(90-a));
ds(p,q,:) = s(1,floor(j),:);
end
imshow(cast(ds,'uint8'));
pause(0.001);
end

imout = cast(ds,'uint8');
imshow(imout);
imwrite(imout,[fname '_mod.png']);
```

Source and output images are as follows.

### Output image

Thank you very much for reading.

2013 April 22 -

1. Amazing. Great work!

Comment by Lorrin | 2014 February 9 | Reply

2. Hi, Thilina Sameera ,
Your program of Image warping using MATLAB – Fisheye Similar Effect is very much impressive, We are recording sky images using all-Sky-Imager and we want to crop the sky image in circle. Can you help me to write program in mat lab? Also I want to unwrap the images recorded by using fisheye lens.Please help me in this development.
Regards,
P.T. Patil
India

Comment by Dr. P.T. Patil | 2014 February 24 | Reply

3. Could you share the code for the reverse, i.e. fish eye to equi-rectangular?

Comment by agrimaseth | 2015 March 1 | Reply

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