Skip to main content

Fisheye Warping

Unroll a top-down fisheye image into a panorama, and wrap any panorama back into a fisheye image.

PyPI Python versions Test License: MIT Downloads

A tiny-planet fisheye photo of the Taipei skyline is dewarped into a 1608 × 512 panorama, and the panorama is rewarped back into the same fisheye image

FisheyeWarping is a Python package and command-line tool built on OpenCV. It dewarps a top-down fisheye image (a ceiling camera, a 360° camera pointing up or down, a "tiny planet" photo) into a flat panorama, and rewarps a panorama back into a fisheye circle. The slow part, working out where every pixel goes, is done once per image size and saved as a mesh file. After that, each image of that size takes a fraction of a second.

How it works

A colour wheel with rings is dewarped into a panorama: each colour sector becomes a vertical band, each ring a horizontal line

Read the fisheye image like a clock face. Dewarping uses polar coordinates: the angle around the centre becomes the horizontal axis, and the distance from the centre becomes the vertical axis. The panorama starts at 6 o'clock and runs clockwise; the rim of the circle becomes the top row and the centre the bottom row. Rewarping does the reverse.

Working out these pixel-to-pixel maps is the slow part, so the tool does it once and saves them as meshes. cv2.remap then moves the pixels in milliseconds. Rewarping needs both meshes: the rewarp mesh says where each fisheye pixel comes from, and the dewarp mesh holds the panorama size it expects. Any panorama you rewarp is resized to that size first.

The maths

Take a fisheye image F of w × h pixels. Pixel (x, y) is column x and row y, so x grows to the right and y downwards. The circle that gets unrolled is the largest one that fits, centred in the image: its centre is (cx, cy) = (w/2, h/2) and its radius is R = min(w, h)/2. For a square N × N image that is the centre (N/2, N/2) and the radius N/2; in a wider or taller image, the strips beside the circle are not used. The panorama P is W × H pixels:

W = \lfloor \pi R \rfloor, \qquad H = R

W is the length of the circle at half the radius, so the panorama keeps the scale there: it squeezes pixels together towards the rim and stretches them towards the centre. A 1024 × 1024 image gives a 1608 × 512 panorama, a 1920 × 1080 frame a 1696 × 540 one.

Dewarp. Column u of the panorama is an angle φ, measured clockwise from 6 o'clock, and row v is a distance r from the centre, counted from the rim:

\varphi = \frac{2\pi u}{W}, \qquad r = R - v, \qquad
P(u, v) = F\big(\lfloor c_x - r\,\sin\varphi \rfloor,\ \lfloor c_y + r\,\cos\varphi \rfloor\big)

Rewarp runs the same map backwards. Each fisheye pixel inside the circle works out its distance r from the centre and its clockwise angle ψ from 6 o'clock, and reads the panorama there. Pixels outside the circle stay black.

r = \sqrt{(x - c_x)^2 + (y - c_y)^2}, \qquad
\psi = \mathrm{atan2}(c_x - x,\ y - c_y) \bmod 2\pi, \qquad
F(x, y) = P\Big(\frac{W\psi}{2\pi},\ R - r\Big) \quad \text{for } r \le R

Dewarping copies whole pixels (the ⌊ ⌋ rounds down). Rewarping lands between pixels, so it blends the four nearest ones (bilinear interpolation). The code reaches these maps in a roundabout way: it turns images by 180° and mirrors the panorama for the left half of the circle. The result is within 1 pixel of the formulas, and a test checks that.

Install

pip install FisheyeWarping

Python 3.8 or newer. This also installs OpenCV, NumPy and tqdm. On Python 3.6 and 3.7, pip install FisheyeWarping picks 1.0.2 by itself, the last release that supports them.

Getting AttributeError: ... has no attribute 'itemset'? That's version 1.0.1, which crashes with NumPy 2. Upgrade: pip install -U FisheyeWarping (1.0.2 or newer).

OpenCV comes as its headless build, opencv-python-headless: the same cv2 module without the window functions such as cv2.imshow, which this tool never uses. It needs no extra system libraries, so it works as it is on servers and in slim Docker images such as python:3.12-slim.

Already have opencv-python? All OpenCV packages install the same cv2 folder, and OpenCV's advice is to keep exactly one of them in an environment; two of different versions can break import cv2. If pip list shows more than one opencv- package, pick one:

# You don't need OpenCV windows: keep the headless build
pip uninstall -y opencv-python opencv-python-headless
pip install opencv-python-headless

# You need cv2.imshow and friends: keep opencv-python, and stop pip from adding the headless build
pip uninstall -y opencv-python opencv-python-headless
pip install opencv-python
pip install --no-deps --force-reinstall FisheyeWarping

With the second way, pip check reports that opencv-python-headless is missing; that is expected. Use --no-deps again when you upgrade FisheyeWarping.

Quick start

The examples use a top-down fisheye image fisheye.jpg and a panorama panorama.jpg in the current folder. To try it with the photo above, scale doc/101-fisheye.jpg down to 1024 × 1024 first (the Mesh files section explains why).

Command line

# First image of this size: build both meshes (slow, once) and write the panorama to dewarp-output.png
fisheyewarping --fisheye_img_path fisheye.jpg \
  --save_dewarp_mesh_path dewarp-mesh.pkl --save_rewarp_mesh_path rewarp-mesh.pkl

# Any other fisheye image of the same size: reuse the dewarp mesh (fast)
fisheyewarping --fisheye_img_path fisheye.jpg --load_dewarp_mesh_path dewarp-mesh.pkl

# Any panorama: wrap it into a fisheye image, written to rewarp-output.png
fisheyewarping --panorama_img_path panorama.jpg \
  --load_dewarp_mesh_path dewarp-mesh.pkl --load_rewarp_mesh_path rewarp-mesh.pkl

Python

import cv2
from fisheyewarping import FisheyeWarping

if __name__ == '__main__':  # needed with use_multiprocessing=True, see the note below
    warper = FisheyeWarping(cv2.imread('fisheye.jpg'), use_multiprocessing=True)

    warper.build_dewarp_mesh(save_path='dewarp-mesh.pkl')  # slow, once per image size
    panorama = warper.run_dewarp(save_path='panorama-out.png')

    warper.build_rewarp_mesh(save_path='rewarp-mesh.pkl')  # needs the dewarp mesh
    fisheye = warper.run_rewarp_with_mesh(cv2.imread('panorama.jpg'), save_path='fisheye-out.png')

Next time, load the meshes instead of building them:

import cv2
from fisheyewarping import FisheyeWarping

# Rewarp a panorama: no fisheye image needed, only the two meshes
warper = FisheyeWarping(None)
warper.load_dewarp_mesh('dewarp-mesh.pkl')
warper.load_rewarp_mesh('rewarp-mesh.pkl')
fisheye = warper.run_rewarp_with_mesh(cv2.imread('panorama.jpg'), save_path='fisheye-out.png')

# Dewarp another fisheye image of the same size
warper = FisheyeWarping(cv2.imread('fisheye.jpg'))
warper.load_dewarp_mesh('dewarp-mesh.pkl')
panorama = warper.run_dewarp(save_path='panorama-out.png')

Multiprocessing in scripts. With use_multiprocessing=True, building a mesh starts one worker process per CPU core. Put that code under if __name__ == '__main__':, or the script stops with a RuntimeError on Windows, on macOS and on Python 3.14 or newer. The command line tool takes care of this itself. The defaults differ: the command uses multiprocessing unless you pass --use_multiprocessing False, the Python class only when you ask for it.

Command line reference

Run it as fisheyewarping or as python -m fisheyewarping; the second form helps on Windows when pip's Scripts folder is not on PATH. fisheyewarping --help lists everything below with examples.

What the command does depends on which images you give it:

  • A fisheye image (--fisheye_img_path): fisheye → panorama. It needs --save_dewarp_mesh_path to build the mesh or --load_dewarp_mesh_path to reuse one. Add --save_rewarp_mesh_path to also build the rewarp mesh for later.
  • A panorama (--panorama_img_path): panorama → fisheye. It needs both saved meshes, --load_dewarp_mesh_path and --load_rewarp_mesh_path.
  • Both: fisheye → panorama, then the given panorama → fisheye with the meshes of that fisheye image. It needs the dewarp mesh as above, plus --save_rewarp_mesh_path or --load_rewarp_mesh_path; without either, it only dewarps.
Option What it is Default
--fisheye_img_path PATH Fisheye image to dewarp (.jpg, .jpeg or .png, any case)
--panorama_img_path PATH Panorama to rewarp (.jpg, .jpeg or .png, any case)
--save_dewarp_mesh_path PATH Build the dewarp mesh and save it to this file
--save_rewarp_mesh_path PATH Build the rewarp mesh and save it to this file
--load_dewarp_mesh_path PATH Load a saved dewarp mesh instead of building one
--load_rewarp_mesh_path PATH Load a saved rewarp mesh instead of building one
--panorama_output PATH Where to write the panorama ./dewarp-output.png
--fisheye_output PATH Where to write the fisheye image made from the panorama ./rewarp-output.png
--use_multiprocessing True|False Build meshes with one worker process per CPU core True

The output format follows the file extension (anything OpenCV can write, such as .png or .jpg). The command checks the options first and the files second, and stops before writing anything when something is wrong. It exits with:

  • 0 when it worked;
  • 1 when a file is missing or cannot be used, such as a broken image or swapped mesh files;
  • 2 for wrong or missing options, such as a fisheye image without a mesh option. It prints the usage line first, like any argparse error. (Up to 1.0.2, missing options ended with 0.)

Python API reference

from fisheyewarping import FisheyeWarping, MeshNotReadyError

FisheyeWarping(img, use_multiprocessing=False): img is the fisheye image as cv2.imread returns it, or None when you only rewarp with loaded meshes. use_multiprocessing=True builds meshes on all CPU cores.

Method What it does Returns
build_dewarp_mesh(save_path=None) Builds the fisheye → panorama mesh for img, and saves it when save_path is given (panorama_size, map_x, map_y), where panorama_size is (width, height)
load_dewarp_mesh(mesh_path) Loads a saved dewarp mesh, including files saved by 1.0.x the same as above
run_dewarp(save_path=None) Dewarps img the panorama
build_rewarp_mesh(save_path=None) Builds the panorama → fisheye mesh for images the size of img. Needs the dewarp mesh. (map_x, map_y, mask)
load_rewarp_mesh(mesh_path) Loads a saved rewarp mesh the same as above
run_rewarp_with_mesh(panorama_img, save_path=None) Rewarps any panorama; resizes it to the panorama size first the fisheye image
run_rewarp(save_path=None) Round trip: dewarps img, then rewarps the result the fisheye image
dewarp(img, flip=False), rewarp(panorama_img, flip=False) The plain steps; the run_* methods also turn the result by 180° the image

Images are NumPy arrays in OpenCV's BGR order. save_path and mesh_path take a str or a pathlib.Path, and images are saved with cv2.imwrite.

When something is missing, you get an error that says what to do:

  • MeshNotReadyError: the method needs a mesh you have not built or loaded yet. It is also an AssertionError, which is what 1.0.x raised, so existing except AssertionError code still works.
  • TypeError: an image is None, which usually means cv2.imread could not read the file.
  • ValueError: a mesh file holds something else, for example when the dewarp and rewarp files are swapped.
  • A UserWarning when OpenCV could not write an output image, for example because the folder does not exist.

Mesh files

A mesh depends only on the image size, not on what is in the image. Build it once per camera or resolution and reuse it for every frame. The dewarp mesh holds (panorama_size, map_x, map_y) and the rewarp mesh (map_x, map_y, mask). Both are saved with Python's pickle; dewarp meshes saved by 1.0.x still load.

Building a mesh takes time and memory in proportion to the number of pixels. Measured with Python 3.12 on a 32-core machine:

Fisheye image Panorama Dewarp mesh Rewarp mesh Time to build both Peak memory
512 × 512 804 × 256 1.6 MB 2.4 MB 1 s 0.2 GB
1024 × 1024 1608 × 512 6.6 MB 9.4 MB 4 s (10 s without multiprocessing) 0.5 GB
2048 × 2048 3216 × 1024 26 MB 38 MB 15 s 1.9 GB

Once the meshes exist, loading one and dewarping an image takes well under a second. For very large images, scale them down with cv2.resize before building meshes: by the numbers above, a 4096 × 4096 image would need around 8 GB.

Only load mesh files you trust. Loading a pickle can run code hidden in the file. Meshes you built yourself are fine; treat a mesh file from someone else like a program you are about to run.

Limitations

  • The circle has to be centred. The tool unrolls the largest circle that fits around the middle of the image. If your fisheye circle sits elsewhere or is smaller, crop the image to a square around it first. (Up to 1.0.2, non-square images came out off-centre when rewarped.)
  • The centre pixel of a rewarped image is black, because the angle at the exact centre is undefined.

Development

git clone https://github.com/NatLee/fisheye-warping.git
cd fisheye-warping
pip install -e ".[test]"
pytest

tox runs the same checks as CI: the package manifest, the package metadata and the tests. Some tests recompute the 1.0 formulas point by point, so any change to the output geometry shows up as a failing test. CI runs on Ubuntu with Python 3.8 to 3.14, and on Windows and macOS with Python 3.12.

The images in this README are real output of the tool: doc/round-trip.jpg comes from doc/101-fisheye.jpg, and doc/how-it-works.jpg from a generated colour wheel.

Contributors

Nat Lee
Nat Lee

License

MIT

Metadata

Release files for FisheyeWarping 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for FisheyeWarping 2.0.0
File Size Uploaded Provenance Actions
fisheyewarping-2.0.0.tar.gz 2.6 MB Without provenance Details for fisheyewarping-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for FisheyeWarping 2.0.0
File Interpreter ABI Platform Actions
fisheyewarping-2.0.0-py3-none-any.whl Python 3 none any Details for fisheyewarping-2.0.0-py3-none-any.whl

Total release size: 2.6 MB

Release files / fisheyewarping-2.0.0.tar.gz

Download URL fisheyewarping-2.0.0.tar.gz
Size 2.6 MB
Tags Source
SHA-256 checksum
How to use checksums
53c523b136594e74a743215c642222d58596a990ce51ec510bbc0e100c48e70c
BLAKE2b-256 checksum
How to use checksums
cc67f7f62e0c13532f9e6ec9a7f0b3230661fa78220c33b955eac68da25c1a63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Release files / fisheyewarping-2.0.0-py3-none-any.whl

Download URL fisheyewarping-2.0.0-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00c43ab5d98504c836b5a32eaf5bf67c59ba934f3ff5d56de437e2d75a6c98bd
BLAKE2b-256 checksum
How to use checksums
dfdb54ed39acb4cb85334d69cf66a8b6aff066334e7e79f524e1af0aa0fbf3e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Security for FisheyeWarping 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page