Co-authored-by: pejour <pejour@users.noreply.github.com>
Co-authored-by: Damien Guillotin <damguillotin@gmail.com>
This commit is contained in:
Laureηt 2023-01-11 17:12:13 +01:00
parent 6026199d92
commit 4d270bbacb
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI
4 changed files with 44 additions and 27 deletions

View file

@ -2,7 +2,9 @@
import bpy import bpy
from mathutils import Matrix from mathutils import Matrix
from mathutils import Vector import pickle
import numpy as np
#--------------------------------------------------------------- #---------------------------------------------------------------
# 3x4 P matrix from Blender camera # 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 v_0 = resolution_y_in_px * scale / 2
skew = 0 # only use rectangular pixels skew = 0 # only use rectangular pixels
K = Matrix( K = Matrix((
((alpha_u, skew, u_0), (alpha_u, skew, u_0),
( 0 , alpha_v, v_0), ( 0, alpha_v, v_0),
( 0 , 0, 1 ))) ( 0, 0, 1)
))
return K return K
# Returns camera rotation and translation matrices from Blender. # Returns camera rotation and translation matrices from Blender.
@ -62,7 +66,7 @@ def get_calibration_matrix_K_from_blender(camd):
# used in digital images) # used in digital images)
# - right-handed: positive z look-at direction # - right-handed: positive z look-at direction
def get_3x4_RT_matrix_from_blender(cam): def get_3x4_RT_matrix_from_blender(cam):
# bcam stands for blender camera # # bcam stands for blender camera
R_bcam2cv = Matrix( R_bcam2cv = Matrix(
((1, 0, 0), ((1, 0, 0),
(0, -1, 0), (0, -1, 0),
@ -101,14 +105,28 @@ def get_3x4_P_matrix_from_blender(cam):
RT = get_3x4_RT_matrix_from_blender(cam) RT = get_3x4_RT_matrix_from_blender(cam)
return K@RT, K, RT return K@RT, K, RT
def run_script(scene): def run_script(scene):
# projection_matrix = scene.camera.matrix_world with open(PICKLE_PATH, 'rb') as file:
projection_matrix, _, _ = get_3x4_P_matrix_from_blender(scene.camera) matrices = pickle.load(file)
with open('/home/damien/Documents/3A/projet-be/imgs/torus/matrices.txt', 'a') as f:
f.write(projection_matrix.__repr__() + '\n\n')
f = open('/home/damien/Documents/3A/projet-be/imgs/torus/matrices.txt', 'w') projection_matrix, _, _ = get_3x4_P_matrix_from_blender(scene.camera)
f.close() matrices.append(np.array(projection_matrix))
bpy.app.handlers.frame_change_post.append(run_script)
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
View file

@ -1,16 +1,19 @@
import cv2 import cv2
import numpy as np import numpy as np
from itertools import product from itertools import product
from rich.progress import track import pickle
from matrices_reader import * 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 X_MIN, X_MAX = -2.0, 2.0
Y_MIN, Y_MAX = -2.0, 2.0 Y_MIN, Y_MAX = -2.0, 2.0
Z_MIN, Z_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) nb_frame = len(projection_matrices)
points = np.array([[x, y, z, 1.0] for x, y, z in product( 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(Y_MIN, Y_MAX, VOXEL_SIZE),
np.arange(Z_MIN, Z_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): 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] proj_mat = projection_matrices[k]
cam_points = proj_mat @ points.T 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)) solid = np.invert(((frame[cam_points[1,:],cam_points[0,:]] == background)).all(1))
cam_points = cam_points[:,solid] cam_points = cam_points[:,solid]
points = points[solid,:] points = points[solid,:]
print('ok')
for k in range(nb_frame): for k in range(nb_frame):
frame = cv2.imread(f'data/torus/torus{k+1:04}.png') frame = cv2.imread(f'/tmp/images/Image{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)
proj_mat = projection_matrices[k] proj_mat = projection_matrices[k]

Binary file not shown.

BIN
torus.blend1 Normal file

Binary file not shown.