Files
MA_Param_Tuning/panorama_viewer.py
2025-08-27 10:34:43 +02:00

140 lines
4.6 KiB
Python

import os
from math import pi
import cv2
import imageio.v2 as imageio
import matplotlib.pyplot as plt
import numpy as np
def rotation_matrix_y(angle):
c = np.cos(angle); s = np.sin(angle)
return np.array([[ c, 0.0, s],
[0.0, 1.0, 0.0],
[-s, 0.0, c]], dtype=np.float32)
def rotation_matrix_x(angle):
c = np.cos(angle); s = np.sin(angle)
return np.array([[1.0, 0.0, 0.0],
[0.0, c, -s],
[0.0, s, c]], dtype=np.float32)
def combine_views(sep_imgs, pano_w, pano_h):
lon_1d = (np.arange(pano_w, dtype=np.float32) + 0.5) / pano_w * (2.0 * pi) - pi # -pi .. pi
lat_1d = (np.arange(pano_h, dtype=np.float32) + 0.5) / pano_h * pi - (pi / 2.0) # -pi/2 .. pi/2
lon, lat = np.meshgrid(lon_1d, lat_1d) # shapes: (H, W)
cos_lat = np.cos(lat)
dir_x = cos_lat * np.cos(lon)
dir_y = np.sin(lat)
dir_z = cos_lat * np.sin(lon)
dirs = np.stack([dir_x, dir_y, dir_z], axis=2).astype(np.float32) # (H,W,3)
H, W = pano_h, pano_w
first_shape = sep_imgs[0]['sz']
if len(first_shape) == 3:
channels = first_shape[2]
else:
channels = 1
pano = np.zeros((H, W, channels), dtype=np.float32)
zbuffer = np.full((H, W), -np.inf, dtype=np.float32)
for view in sep_imgs:
img = view['img']
if img.dtype == np.uint8:
img = img.astype(np.float32) / 255.0
else:
img = img.astype(np.float32)
if img.ndim == 2:
img = img[..., np.newaxis]
h_img, w_img = img.shape[0], img.shape[1]
fov = float(view['fov'])
vx = float(view.get('vx', 0.0))
vy = float(view.get('vy', 0.0))
# focal length in pixels
f = (w_img / 2.0) / np.tan(fov / 2.0)
cx = (w_img - 1) / 2.0
cy = (h_img - 1) / 2.0
R = rotation_matrix_x(-vy) @ rotation_matrix_y(-vx)
dirs_flat = dirs.reshape((-1, 3)).T # (3, N)
cam_dirs = (R @ dirs_flat).T.reshape((H, W, 3)) # (H, W, 3)
z_cam = cam_dirs[:, :, 2]
valid = z_cam > 1e-6
x_img = np.zeros_like(z_cam, dtype=np.float32)
y_img = np.zeros_like(z_cam, dtype=np.float32)
x_img[valid] = (f * (cam_dirs[:,:,0][valid] / z_cam[valid]) ) + cx
y_img[valid] = (f * ( - cam_dirs[:,:,1][valid] / z_cam[valid]) ) + cy
in_bounds = (x_img >= 0) & (x_img <= (w_img - 1)) & (y_img >= 0) & (y_img <= (h_img - 1))
mask = valid & in_bounds
if not np.any(mask):
continue
map_x = x_img.astype(np.float32)
map_y = y_img.astype(np.float32)
remapped = np.zeros((H, W, channels), dtype=np.float32)
for c in range(channels):
ch = img[..., c]
remapped[..., c] = cv2.remap(ch, map_x, map_y, interpolation=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT, borderValue=0.0)
replace = (z_cam > zbuffer) & mask
if np.any(replace):
replace3 = np.repeat(replace[:, :, np.newaxis], channels, axis=2)
pano[replace3] = remapped[replace3]
zbuffer[replace] = z_cam[replace]
return pano
if __name__ == "__main__":
ids = set()
for file in os.listdir('F:\\UNI\MA_Param_Tuning\\17DRP5sb8fy\matterport_color_images\\17DRP5sb8fy\_\matterport_color_images'):
_identifier = file.split('_')[0]
ids.add(_identifier)
for _identifier in ids:
for t in range(3):
intrinsics = np.loadtxt(f'F:\\UNI\MA_Param_Tuning\\17DRP5sb8fy\matterport_camera_intrinsics\\17DRP5sb8fy\_\matterport_camera_intrinsics\\{_identifier}_intrinsics_{t}.txt')
sep_imgs = []
vx = [-pi, -2/3*pi, -1/3*pi, 0.0, 1/3*pi, 2/3*pi]
vy = [0.0]*6
for a in range(6):
path = f'F:\\UNI\MA_Param_Tuning\\17DRP5sb8fy\matterport_color_images\\17DRP5sb8fy\_\matterport_color_images\\{_identifier}_i{t}_{a}.jpg'
img = imageio.imread(path) # returns HxWxC
img = img[0:1013, 0:1254, ...]
if img.dtype == np.uint8:
img = img.astype(np.float32) / 255.0
sep_imgs.append({
'img': img,
'vx': vx[a],
'vy': 0.0,
'fov': 1.06,
'sz': img.shape
})
pano_color = combine_views(sep_imgs, 2048, 1024)
plt.figure(figsize=(12,6))
plt.imshow(np.clip(np.flipud(pano_color), 0.0, 1.0))
plt.axis('off')
plt.title('Panorama Color')
plt.show()