Skip to content

Instantly share code, notes, and snippets.

@nanki
Created March 9, 2019 16:05
Show Gist options
  • Save nanki/cd6418ed2d8b3d105fbed60a4a31ebc4 to your computer and use it in GitHub Desktop.
Save nanki/cd6418ed2d8b3d105fbed60a4a31ebc4 to your computer and use it in GitHub Desktop.
gnomonic transformation
import numpy as np
from PIL import Image
from skimage import transform
def gnomonic2equirectangular(ishape, oshape, fov):
def inner(xy):
ones = np.expand_dims(np.ones(xy.shape[0]), 0).transpose()
v = np.concatenate((xy, ones), axis=1)
v /= np.linalg.norm(v, axis=1, keepdims=True)
xx = v[:, 0]
yy = v[:, 1]
zz = v[:, 2]
lat = np.arcsin(zz)
lon = np.arctan2(xx, yy)
return np.c_[lon, lat]
def outer(xy):
osa = np.flip(np.array(oshape, np.float) - 1, 0)
isa = np.flip(np.array(ishape, np.float) - 1, 0)
o = inner((2 * xy / osa - 1) * np.tan(fov / 2))
return (np.divide(o, np.array([2 * np.pi, np.pi])) + .5) * isa
return outer
def rect2gnomonic(img, fov=np.pi / 2):
ishape = img.shape[:2]
oshape = np.full(2, np.ceil(np.tan(fov / 2) / np.sin(2 * np.pi / ishape[1])))
return transform.warp(img.astype(np.float), inverse_map=gnomonic2equirectangular(ishape, oshape, fov), output_shape=oshape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment