# Converting Fish Eye image to Panoramic image?

35 views (last 30 days)
Kristian Moesgaard on 3 Apr 2014
Commented: N A on 25 Jan 2020
I have acquired a fish eye lens image of the sky and I would like to "unfold" this image, in matlab, into a Panoramic image instead of spherical. I am having a very hard time figuring out how to do so. I've found a function in MATLAB, which I think might be useful:
[x,y,z] = sph2cart(azimuth,elevation,r)
But i simply can't figure out how to use it.
I was wondering if any one might have created an example of how to use this function to convert an image from Spherical to panoramic coordinates.

Image Analyst on 3 Apr 2014
Edited: Image Analyst on 3 Apr 2014
In optical distortion, the distance the pixel moves from its correct location is proportional to the cube of the radius from the optical axis. In other words:
rBad = rTrue + df * rTrue;
Where rTrue is where it should be if there were no distortion, df is the magnitiude of the fish-eye effect, and rBad is where it actually is (because of the distortion). You can use that formula to get find out where bad pixels should get mapped (sent) to. Make a grid of every 10 pixels or so and find out what their true location should be.
Then I believe you can use those locations (the actual bad locations, and the desired good locations) in imtransform() to un-distort the image. Give it a try. Demo attached. See if you can finish it by using imtransform(). The + are the distorted points and the o are the corrected points. Not sure why the legend function is not realizing what points are what.

Image Analyst on 7 Apr 2014
See MATLAB example. Sorry I haven't had a chance to finish the code for you. But it looks like you can do it with interp2 as shown here: http://www.mathworks.com/matlabcentral/answers/23708#answer_31442
Kristian Moesgaard on 8 Apr 2014
Hello again.. I really appreciate you talking time to help me out. I tried implementing the code you have linked, unfortunately this has no effect on the image. Perhaps I am doing it wrong?
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Specify the distortion correction factor.
% Positive to correct pincushion distortion, and bring coreners inwards.
% Negative to correct barrel (fisheye) distortion, and push corners outward.
df = -0.5;
xCenter = columns / 2; % Assume optical axis in center of image.
yCenter = rows / 2;
samplingRate = 50;
hold on;
for r = 1 : size(xBad, 1)
for c = 1 : size(xBad, 2)
% Get the actual distance from the optic axis.
rBad = sqrt((x - xCenter)^2 + (y - yCenter)^2);
% Get the delta R that is moved.
angle = atan2((y - yCenter), (x - xCenter));
xTrue(r, c) = xCenter + rTrue * cos(angle);
yTrue(r, c) = yCenter + rTrue * sin(angle);
D = interp2(double(grayImage), x-xTrue, y-yTrue);
% display the result
imshow(D, []);
end
end
plot(xTrue, yTrue, 'bo', 'MarkerSize', 5, 'LineWidth', 2);
legend({'+ = Distorted'; 'o = Corrected'});
N A on 25 Jan 2020
This provides a correction algorithm for distortion, but I'm trying to take a grayscale image (that's rendered) and apply a barrel distortion to it. How would I approach this problem?