From 269d3c2fe63a85acc2425d7669ce46475264ac93 Mon Sep 17 00:00:00 2001 From: Jerome Date: Sun, 30 Apr 2023 10:51:36 +0200 Subject: [PATCH] Added bilinear interpolation (broken) to python version. --- Camera.py | 9 +++++++-- ImageProjectionTest.py | 45 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Camera.py b/Camera.py index 88ac4d1..8867be5 100644 --- a/Camera.py +++ b/Camera.py @@ -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)]) diff --git a/ImageProjectionTest.py b/ImageProjectionTest.py index 97348bc..0647262 100644 --- a/ImageProjectionTest.py +++ b/ImageProjectionTest.py @@ -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)