Added bilinear interpolation (broken) to python version.

This commit is contained in:
Jerome 2023-04-30 10:51:36 +02:00
parent 09a32e4b0b
commit 269d3c2fe6
2 changed files with 44 additions and 10 deletions

View file

@ -80,10 +80,15 @@ def sph2equirectangular(p_sph, width, height):
''' Converts spherical coordinates to pixel coordinates of an equirectangular image. '''
theta = p_sph[1]
phi = p_sph[2]
i = int(np.round(((height - 1)*theta)/np.pi)) % height
j = int(np.round((width*(np.pi + phi))/(2.0*np.pi))) % width
i = np.mod(((height - 1)*theta)/np.pi, height)
j = np.mod((width*(np.pi + phi))/(2.0*np.pi), width)
return [i,j]
def sph2equirectangular_int(p_sph, width, height):
''' Converts spherical coordinates to pixel coordinates of an equirectangular image. '''
i, j = sph2equirectangular(p_sph, width, height)
return [int(i), int(j)]
def equirectangular2sph(i, j, width, height):
return np.array([1.0, np.pi*(i/(height-1)), 2.0*np.pi*(j/width - 0.5)])

View file

@ -2,8 +2,23 @@ import numpy as np
from Camera import *
import matplotlib.pyplot as plt
def update_pixel(surf, i, j, cam, img):
''' Updates a pixel in a surface by projecting a ray from the camera to the sphere centered on the camera. '''
def interp2_normalized(f00, f10, f01, f11, x, y):
a00 = f00
a10 = f10 - f00
a01 = f01 - f00
a11 = f11 - f10 - f01 + f00
return a00 + a10*x + a01*y + a11*x*y
def interp2(x0, y0, x1, y1, f00, f10, f01, f11, x, y):
x_n = (x - x0)/(x1 - x0)
y_n = (y - y0)/(y1 - y0)
print(x_n, y_n)# DEBUG
return interp2_normalized(f00, f10, f01, f11, x_n, y_n)
def update_pixel(surf, i, j, cam, img, interp_type='nearest'):
''' Updates a pixel in a surface by projecting a ray from the camera to the sphere centered on the camera.
interp_type: 'linear' or 'nearest'
'''
# Get the ray direction in inertial frame by projecting the pixel to the sphere
p_sensor = pixelToNormalizedCoordinates(i, j, surf.shape[1], surf.shape[0])
p_sensor[0] *= -1
@ -11,27 +26,41 @@ def update_pixel(surf, i, j, cam, img):
ray_dir = cam.compute_ray_dir_inertial_frame(p_sensor[0], p_sensor[1])
ray_dir_sph = cart2sph(ray_dir)
i_img, j_img = sph2equirectangular(ray_dir_sph, img.shape[1], img.shape[0])
surf[i,j,:] = img[i_img, j_img, :]
if interp_type == 'linear':
i0 = int(np.floor(i_img)) % img.shape[0]
j0 = int(np.floor(j_img)) % img.shape[1]
i1 = int(np.ceil(i_img)) % img.shape[0]
j1 = int(np.ceil(j_img)) % img.shape[1]
col = interp2(i0, j0, i1, j1, img[i0,j0,:], img[i1,j0,:], img[i0,j1,:], img[i1,j1,:], i_img, j_img)
# print(i0, j0, i1, j1, i_img, j_img, img[i0,j0,:], img[i1,j0,:], img[i0,j1,:], img[i1,j1,:], col)# debug
surf[i,j,:] = [int(col[0]), int(col[1]), int(col[2])]
else:
surf[i,j,:] = img[int(i_img), int(j_img), :]
return surf
if __name__ == '__main__':
deg = np.pi/180.
width = 60
width = 300
height = int(width/2)
interp_type = 'linear'
surf = np.zeros((height, width, 3), dtype=int)
# load 360 image
img = plt.imread('venise.jpg')
plt.figure()
plt.imshow(img)
if 0: # display 360 image
plt.figure()
plt.imshow(img)
cam = Camera(FOV=90*deg).set_target([0.,1.,.8])
az = -10*deg
cam = Camera(FOV=10*deg).set_target([np.cos(az),np.sin(az),0.05])
# cam = Camera(FOV=90*deg).set_target([0.,1.,.8])
# cam = Camera(FOV=90*deg,up=np.array([1.,0.,1.])).set_target([0.,0.,-1.])# straight down
# update surface
for i in range(height):
for j in range(width):
surf = update_pixel(surf, i, j, cam, img)
surf = update_pixel(surf, i, j, cam, img, interp_type)
plt.figure()
plt.imshow(surf)