pickle
Co-authored-by: pejour <pejour@users.noreply.github.com> Co-authored-by: Damien Guillotin <damguillotin@gmail.com>
This commit is contained in:
parent
6026199d92
commit
4d270bbacb
48
get_proj.py
48
get_proj.py
|
@ -2,7 +2,9 @@
|
|||
|
||||
import bpy
|
||||
from mathutils import Matrix
|
||||
from mathutils import Vector
|
||||
import pickle
|
||||
import numpy as np
|
||||
|
||||
|
||||
#---------------------------------------------------------------
|
||||
# 3x4 P matrix from Blender camera
|
||||
|
@ -41,10 +43,12 @@ def get_calibration_matrix_K_from_blender(camd):
|
|||
v_0 = resolution_y_in_px * scale / 2
|
||||
skew = 0 # only use rectangular pixels
|
||||
|
||||
K = Matrix(
|
||||
((alpha_u, skew, u_0),
|
||||
( 0 , alpha_v, v_0),
|
||||
( 0 , 0, 1 )))
|
||||
K = Matrix((
|
||||
(alpha_u, skew, u_0),
|
||||
( 0, alpha_v, v_0),
|
||||
( 0, 0, 1)
|
||||
))
|
||||
|
||||
return K
|
||||
|
||||
# Returns camera rotation and translation matrices from Blender.
|
||||
|
@ -62,7 +66,7 @@ def get_calibration_matrix_K_from_blender(camd):
|
|||
# used in digital images)
|
||||
# - right-handed: positive z look-at direction
|
||||
def get_3x4_RT_matrix_from_blender(cam):
|
||||
# bcam stands for blender camera
|
||||
# # bcam stands for blender camera
|
||||
R_bcam2cv = Matrix(
|
||||
((1, 0, 0),
|
||||
(0, -1, 0),
|
||||
|
@ -101,14 +105,28 @@ def get_3x4_P_matrix_from_blender(cam):
|
|||
RT = get_3x4_RT_matrix_from_blender(cam)
|
||||
return K@RT, K, RT
|
||||
|
||||
|
||||
|
||||
def run_script(scene):
|
||||
# projection_matrix = scene.camera.matrix_world
|
||||
projection_matrix, _, _ = get_3x4_P_matrix_from_blender(scene.camera)
|
||||
with open('/home/damien/Documents/3A/projet-be/imgs/torus/matrices.txt', 'a') as f:
|
||||
f.write(projection_matrix.__repr__() + '\n\n')
|
||||
with open(PICKLE_PATH, 'rb') as file:
|
||||
matrices = pickle.load(file)
|
||||
|
||||
f = open('/home/damien/Documents/3A/projet-be/imgs/torus/matrices.txt', 'w')
|
||||
f.close()
|
||||
bpy.app.handlers.frame_change_post.append(run_script)
|
||||
projection_matrix, _, _ = get_3x4_P_matrix_from_blender(scene.camera)
|
||||
matrices.append(np.array(projection_matrix))
|
||||
|
||||
with open(PICKLE_PATH, 'wb') as file:
|
||||
pickle.dump(matrices, file)
|
||||
|
||||
def setup_script(scene):
|
||||
matrices = []
|
||||
with open(PICKLE_PATH, 'wb') as file:
|
||||
pickle.dump(matrices, file)
|
||||
|
||||
PICKLE_PATH = "/tmp/pickle.truc"
|
||||
|
||||
# clear handlers
|
||||
bpy.app.handlers.render_init.clear()
|
||||
bpy.app.handlers.frame_change_pre.clear()
|
||||
bpy.app.handlers.frame_change_post.clear()
|
||||
|
||||
# add handler
|
||||
bpy.app.handlers.render_init.append(setup_script)
|
||||
bpy.app.handlers.frame_change_pre.append(run_script)
|
||||
|
|
23
main.py
23
main.py
|
@ -1,16 +1,19 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
from itertools import product
|
||||
from rich.progress import track
|
||||
import pickle
|
||||
|
||||
from matrices_reader import *
|
||||
|
||||
VOXEL_SIZE = 2e-2
|
||||
PICKLE_PATH = "/tmp/pickle.truc"
|
||||
|
||||
VOXEL_SIZE = 8e-2
|
||||
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')
|
||||
with open(PICKLE_PATH, 'rb') as file:
|
||||
projection_matrices = pickle.load(file)
|
||||
nb_frame = len(projection_matrices)
|
||||
|
||||
points = np.array([[x, y, z, 1.0] for x, y, z in product(
|
||||
|
@ -18,10 +21,10 @@ points = np.array([[x, y, z, 1.0] for x, y, z in product(
|
|||
np.arange(Y_MIN, Y_MAX, VOXEL_SIZE),
|
||||
np.arange(Z_MIN, Z_MAX, VOXEL_SIZE))])
|
||||
|
||||
background = np.array([18, 18, 18])
|
||||
|
||||
background = np.array([255, 255, 255])
|
||||
print('ok')
|
||||
for k in range(nb_frame):
|
||||
frame = cv2.imread(f'data/torus/torus{k+1:04}.png')
|
||||
frame = cv2.imread(f'/tmp/masks/Image{k+1:04}.png')
|
||||
proj_mat = projection_matrices[k]
|
||||
|
||||
cam_points = proj_mat @ points.T
|
||||
|
@ -35,13 +38,9 @@ for k in range(nb_frame):
|
|||
solid = np.invert(((frame[cam_points[1,:],cam_points[0,:]] == background)).all(1))
|
||||
cam_points = cam_points[:,solid]
|
||||
points = points[solid,:]
|
||||
|
||||
print('ok')
|
||||
for k in range(nb_frame):
|
||||
frame = cv2.imread(f'data/torus/torus{k+1:04}.png')
|
||||
# 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)
|
||||
frame = cv2.imread(f'/tmp/images/Image{k+1:04}.png')
|
||||
|
||||
proj_mat = projection_matrices[k]
|
||||
|
||||
|
|
BIN
torus.blend
BIN
torus.blend
Binary file not shown.
BIN
torus.blend1
Normal file
BIN
torus.blend1
Normal file
Binary file not shown.
Loading…
Reference in a new issue