Converting an equirectangular image to a perspective projection

Written by Paul Bourke
November 2016

See also: sphere2fish to convert a equirectangular projection to a fisheye

The source code implementing the projections below is only available on request for a small fee. It includes a demo application and an invitation to convert an image of your choice to verify the code does what you seek. For more information please contact the author.


Update (Aug 2017)
Added offaxis perspective projections, also known as a tilt-shift camera/lens often used in architectural photography.


The following describes a tool for extracting an ideal, pinhole camera style perspective projection from an equirectangular (also called spherical) projection. The real-time way to do this might be to map the equirectangular image onto a sphere and using a real-time API (eg: Opengl) view the result with a virtual camera located in the center of the sphere. The computational method here has some advantages, for example: it can result in higher quality conversions and it can handle unlimited image sizes (both input and output).

The following image will be used for the examples illustrated on this page.

The usual approach for such image transformations is to perform the inverse mapping. That is, one needs to consider each pixel in the output image (perspective) and map backwards to find the best estimate pixel in the input image (spherical). In this way every pixel in the output image is found (compared to a forward mapping), it also means that the performance is governed by the resolution of the output image (and supersampling) irrespective of the size of the input image. A key aspect of these mappings is also to perform some sort of antialiasing, the solutions here use a simple supersampling approach.

The default usage creates an image with a 100 degree horizontal field of view, the vertical field of view is calculated based upon the image dimensions, the default being 1920x1080. Zero degrees longitude is 1/4 of the width of the image from the left edge.


sphere2persp pano4.tga

The code here in written in plain vanilla C tested on Unix style operating systems using the gcc compilers (specifically Mac and Linux), but the algorithms/code can readily be modified for other operating systems and programming languages. This is not meant to be a final application but rather something you integrate into your code base. Having said that it is wrapped up in a simple TGA image reader/writer for the purposes of algorithm testing, the intent is that one would be implementing the function into ones own pipeline. They all operate on a RGB buffer (fisheye image) in memory.

Usage string

Usage: sphere2persp [options] sphericalimage
Options
   -w n     perspective image width, default = 1920
   -h n     perspective image height, default = 1080
   -t n     aperture of perspective (degrees), default = 100
   -x n     tilt angle (degrees), default: 0
   -y n     roll angle (degrees), default: 0
   -z n     pan angle (degrees), default: 0
   -o n     offset for offaxis perspective (percentage), default: 0
   -a n     antialiasing level, default = 2
   -f s     output file name

Tilt, pan and roll are performed with the command line options -x, -y and -z. That is the x axis is to the right, the z axis is up and the y axis is forward. The following pans the camera 90 degrees and tilts up 40 degrees.


sphere2persp -x -40 -z 90 pano4.tga

If the mapping is performed correctly, and the original equirectangular image is correct, all lines in the scene that should be straight will be straight, unlike the curvature in the equirectangular image. Any perspective field of view can be chosen, smaller fields of view result in zooming in, larger are equivalent to zooming out. While perspective views are defined up to, but not including 180 degrees, for practical purposes perspective projections become increasingly inefficient after 150 degrees. The following is a 150 degree perspective view.


sphere2persp -t 150 -z 180 pano4.tga

Assuming a correct equirectangular, there should be no distortion looking straight down or straight up.


sphere2persp -t 120 -x 90 -z -90 pano4.tga


sphere2persp -t 120 -x -90 -z -90 pano4.tga