2022-12-17 14:05:14 +00:00
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2023-01-11 12:18:31 +00:00
|
|
|
from itertools import product
|
|
|
|
from rich.progress import track
|
2022-12-17 14:05:14 +00:00
|
|
|
|
|
|
|
from matrices_reader import *
|
|
|
|
|
2023-01-11 12:18:31 +00:00
|
|
|
VOXEL_SIZE = 2e-2
|
2022-12-17 14:05:14 +00:00
|
|
|
X_MIN, X_MAX = -2.0, 2.0
|
|
|
|
Y_MIN, Y_MAX = -2.0, 2.0
|
|
|
|
Z_MIN, Z_MAX = -2.0, 2.0
|
|
|
|
|
|
|
|
projection_matrices = matrices_reader('data/torus/matrices.txt')
|
|
|
|
nb_frame = len(projection_matrices)
|
2023-01-11 12:18:31 +00:00
|
|
|
|
|
|
|
points = np.array([[x, y, z, 1.0] for x, y, z in product(
|
|
|
|
np.arange(X_MIN, X_MAX, VOXEL_SIZE),
|
|
|
|
np.arange(Y_MIN, Y_MAX, VOXEL_SIZE),
|
|
|
|
np.arange(Z_MIN, Z_MAX, VOXEL_SIZE))])
|
|
|
|
|
|
|
|
background = np.array([18, 18, 18])
|
2022-12-17 14:05:14 +00:00
|
|
|
|
|
|
|
for k in range(nb_frame):
|
2023-01-11 12:18:31 +00:00
|
|
|
frame = cv2.imread(f'data/torus/torus{k+1:04}.png')
|
2022-12-17 14:05:14 +00:00
|
|
|
proj_mat = projection_matrices[k]
|
2023-01-11 12:18:31 +00:00
|
|
|
|
|
|
|
cam_points = proj_mat @ points.T
|
|
|
|
cam_points /= cam_points[2,:]
|
|
|
|
cam_points = np.round(cam_points, 0).astype(np.int32)
|
|
|
|
|
|
|
|
visible = np.logical_and.reduce((0 <= cam_points[0,:], cam_points[0,:] < frame.shape[1], 0 <= cam_points[1,:], cam_points[1,:] < frame.shape[0]))
|
|
|
|
cam_points = cam_points[:,visible]
|
|
|
|
points = points[visible,:]
|
2022-12-17 14:05:14 +00:00
|
|
|
|
2023-01-11 12:18:31 +00:00
|
|
|
solid = np.invert(((frame[cam_points[1,:],cam_points[0,:]] == background)).all(1))
|
|
|
|
cam_points = cam_points[:,solid]
|
|
|
|
points = points[solid,:]
|
|
|
|
|
|
|
|
for k in range(nb_frame):
|
2022-12-17 14:05:14 +00:00
|
|
|
frame = cv2.imread(f'data/torus/torus{k+1:04}.png')
|
2023-01-11 12:18:31 +00:00
|
|
|
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
|
# frame = 255 * (frame == 18).astype(np.uint8)
|
|
|
|
# frame = cv2.filter2D(frame, -1, np.ones((5, 5)) / 25)
|
|
|
|
# frame = 255 * (frame > 255/2).astype(np.uint8)
|
|
|
|
|
|
|
|
proj_mat = projection_matrices[k]
|
|
|
|
|
|
|
|
cam_points = proj_mat @ points.T
|
|
|
|
cam_points /= cam_points[2,:]
|
|
|
|
cam_points = np.round(cam_points, 0).astype(np.int32)
|
|
|
|
|
|
|
|
for cam_point in cam_points.T:
|
|
|
|
cv2.circle(frame, (cam_point[0], cam_point[1]), 2, (255, 0, 0))
|
|
|
|
|
|
|
|
|
2022-12-17 14:05:14 +00:00
|
|
|
cv2.imshow('Frame', frame)
|
|
|
|
cv2.waitKey(0)
|