Co-authored-by: pejour <pejour@users.noreply.github.com>
Co-authored-by: Laureηt <laurent@fainsin.bzh>
This commit is contained in:
gdamms 2023-01-25 17:08:54 +01:00
parent ad152bcf02
commit f1dbcdcd1c

View file

@ -3,14 +3,16 @@ import numpy as np
from itertools import product from itertools import product
import pickle import pickle
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import mcubes
from rich.progress import track
from borders import update_border from borders import update_border
from fvi import fast_voxel_intersect
VOXEL_SIZE = 1e-2 VOXEL_SIZE = 1e-1
X_MIN, X_MAX = -1.3, 1.3 X_MIN, X_MAX = -2, 2
Y_MIN, Y_MAX = -1.3, 1.3 Y_MIN, Y_MAX = -2, 2
Z_MIN, Z_MAX = -0.3, 0.3 Z_MIN, Z_MAX = -1, 1
nb_frame = 24 nb_frame = 24
@ -19,15 +21,22 @@ 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))])
mask = np.array([255, 255, 255]) mask = 255 * np.ones((3,))
test_point = [0, 0, 0, 1.0] positions = []
proj_mats = []
frames = []
for k in range(nb_frame): for k in range(nb_frame):
frame = cv2.imread(f'/tmp/masks/Image{k:04}.png') frame = cv2.imread(f'data/torus/masks/Image{k:04}.png')
frames.append(cv2.imread(f'data/torus/images/Image{k:04}.png'))
with open(f"/tmp/cameras/{k:04d}.pickle", 'rb') as file: with open(f"data/torus/cameras/{k:04d}.pickle", 'rb') as file:
proj_mat = pickle.load(file)["P"] matrices = pickle.load(file)
proj_mat = matrices["P"]
proj_mats.append(proj_mat)
position = matrices["RT"][:, 3]
positions.append(position)
cam_points = proj_mat @ points.T cam_points = proj_mat @ points.T
cam_points /= cam_points[2,:] cam_points /= cam_points[2,:]
@ -42,8 +51,8 @@ for k in range(nb_frame):
points = points[solid,:] points = points[solid,:]
# for cam_point in cam_points.T: # for cam_point in cam_points.T:
# cv2.circle(frame, (cam_point[0], cam_point[1]), 2, (255*is_in[k], 0, 255*(not is_in[k]))) # cv2.circle(frame, (cam_point[0], cam_point[1]), 2, (255*is_in[k], 0, 255*(not is_in[k])))
# cv2.circle(frame, (cam_point[0], cam_point[1]), 2, (255, 0, 255)) # cv2.circle(frame, (cam_point[0], cam_point[1]), 2, (255, 0, 255))
# for k in range(nb_frame): # for k in range(nb_frame):
# frame = cv2.imread(f'/tmp/masks/Image{k:04}.png') # frame = cv2.imread(f'/tmp/masks/Image{k:04}.png')
@ -69,9 +78,51 @@ voxel[idx[:,0], idx[:,1], idx[:,2]] = 1
border = update_border(voxel) border = update_border(voxel)
# 3D plot the result # # 3D plot the result
fig = plt.figure() # fig = plt.figure()
ax = fig.add_subplot(111, projection='3d') # ax = fig.add_subplot(111, projection='3d')
ax.scatter(np.where(border)[0], np.where(border)[1], np.where(border)[2], c='b', marker='o', s=1) # ax.scatter(np.where(border)[0], np.where(border)[1], np.where(border)[2], c='b', marker='o', s=1)
plt.axis('equal') # plt.axis('equal')
plt.show() # plt.show()
origin = np.array([X_MIN, Y_MIN, Z_MIN])
step = np.array([VOXEL_SIZE, VOXEL_SIZE, VOXEL_SIZE])
shape = np.array([int((X_MAX-X_MIN)/VOXEL_SIZE), int((Y_MAX-Y_MIN)/VOXEL_SIZE), int((Z_MAX-Z_MIN)/VOXEL_SIZE)])
for idx in track(np.argwhere(border)):
# coordonnées du centre du voxel
start = np.array([
X_MIN + (idx[0] + 0.5) * VOXEL_SIZE,
Y_MIN + (idx[1] + 0.5) * VOXEL_SIZE,
Z_MIN + (idx[2] + 0.5) * VOXEL_SIZE])
# array qui contiendra les nuances de gris des frames qui voient le voxel
values = []
# pour chaque camera (frame)
for i in range(nb_frame):
# coordonnées du centre de la caméra
end = positions[i]
# si le rayon ne traverse aucun autre voxel entre le centre du voxel (idx) et le centre de la caméra
if not fast_voxel_intersect(start, end, origin, step, shape):
proj = proj_mats[i] @ np.array([start[0], start[1], start[2], 1.0])
proj /= proj[2]
proj = np.round(proj).astype(np.int32)
values.append(frames[i][proj[1], proj[0]])
# calcule écartype des valeurs
std = np.std(values)
# changer le levelset en fonction de l'écartype
if std < 2:
voxel[idx[0], idx[1], idx[2]] = 1
else:
voxel[idx[0], idx[1], idx[2]] = 0
vertices, triangles = mcubes.marching_cubes(border, 0)
mcubes.export_obj(vertices, triangles, "result.obj")