projet-probleme-inverse-3D/src/draw.py

124 lines
4 KiB
Python
Raw Normal View History

import matplotlib.pyplot as plt
import numpy as np
2023-01-31 13:51:14 +00:00
import cv2
2023-01-31 17:06:15 +00:00
VOXEL_SIZE = 0.1
X_MIN, X_MAX = -5, 5
Y_MIN, Y_MAX = -5, 5
2023-02-01 08:12:18 +00:00
x_vals = np.arange(X_MIN + VOXEL_SIZE / 2, X_MAX, VOXEL_SIZE)
y_vals = np.arange(Y_MIN + VOXEL_SIZE / 2, Y_MAX, VOXEL_SIZE)
2023-01-31 17:06:15 +00:00
image_length = 1500
2023-01-31 17:06:15 +00:00
def f(x, y):
return np.exp(-((x**2) + y**2) / 3)
X, Y = np.meshgrid(x_vals, y_vals)
Z = f(X - 2, Y) + f(X + 2, Y)
Z = (Z > 0.4).astype(np.float32)
Z *= np.random.rand(*Z.shape)
2023-02-01 08:12:18 +00:00
for j, x in enumerate(x_vals):
for i, y in enumerate(y_vals):
color = f"{hex(int(Z[i, j] * 255))[2:]}"
if color == "0":
2023-01-30 14:51:11 +00:00
color = "#003"
else:
color = "#" + 3 * color
2023-02-01 08:12:18 +00:00
plt.fill([x - VOXEL_SIZE/2, x + VOXEL_SIZE/2, x + VOXEL_SIZE/2, x - VOXEL_SIZE/2],
[y - VOXEL_SIZE/2, y - VOXEL_SIZE/2, y + VOXEL_SIZE/2, y + VOXEL_SIZE/2], color=color)
nb_cams = 32
cam_poses = np.array(
2023-01-31 17:06:15 +00:00
[[6 * np.cos(theta), 6 * np.sin(theta)]
for theta in np.linspace(0, 2 * np.pi, nb_cams, endpoint=False)]
)
cam_rots = np.linspace(np.pi, 3 * np.pi, nb_cams, endpoint=False)
cam2world_projs = np.array(
[
2023-01-31 17:06:15 +00:00
[[np.cos(theta), -np.sin(theta), cam_pose[0]],
[np.sin(theta), np.cos(theta), cam_pose[1]],
[0, 0, 1]]
for theta, cam_pose in zip(cam_rots, cam_poses)
]
)
for i in range(nb_cams):
2023-02-01 08:12:18 +00:00
x = np.array([[0, 0, 1], [0.5, -0.2, 1], [0.5, 0.2, 1], [0, 0, 1]])
x = cam2world_projs[i] @ x.T
2023-01-31 17:06:15 +00:00
plt.plot(cam_poses[i][0], cam_poses[i][1], "ro")
plt.text(cam_poses[i][0], cam_poses[i][1], str(i))
plt.plot(x[0, :], x[1, :], "r-")
plt.xlim(-7, 7)
plt.ylim(-7, 7)
plt.axis("equal")
plt.axis("off")
plt.savefig("data/peanut/peanut_cams.png", dpi=300,
2023-01-31 17:06:15 +00:00
bbox_inches="tight", pad_inches=0, transparent=True)
2023-01-31 14:59:46 +00:00
plt.close()
# draw 1d image of the scene for each camera
2023-01-31 15:40:40 +00:00
for i in range(nb_cams):
2023-01-31 14:59:46 +00:00
# sort pixels by distance to camera
cam_pose = cam_poses[i]
2023-01-31 17:06:15 +00:00
pixels_dist = np.linalg.norm(
np.array([X.flatten(), Y.flatten()]).T - cam_pose, axis=1)
pixels_sort = np.argsort(pixels_dist)[::-1]
2023-01-30 14:51:11 +00:00
x0 = -1
x1 = 1
2023-01-31 17:06:15 +00:00
img = np.zeros((100, image_length, 3), dtype=np.uint8)
mask = np.zeros((100, image_length, 3), dtype=np.uint8)
for j in pixels_sort:
x, y = X.flatten()[j], Y.flatten()[j]
2023-02-01 08:12:18 +00:00
color = int(Z.flatten()[j] * 255)
if color == 0:
continue
2023-01-31 17:06:15 +00:00
RT = np.linalg.inv(cam2world_projs[i])[:-1, :]
2023-02-01 08:12:18 +00:00
px = np.array([[x - VOXEL_SIZE/2, y - VOXEL_SIZE/2, 1],
[x + VOXEL_SIZE/2, y - VOXEL_SIZE/2, 1],
[x + VOXEL_SIZE/2, y + VOXEL_SIZE/2, 1],
[x - VOXEL_SIZE/2, y + VOXEL_SIZE/2, 1]])
2023-01-31 17:06:15 +00:00
px = RT @ px.T
2023-01-30 14:51:11 +00:00
px /= px[0, :]
2023-01-31 17:06:15 +00:00
px += np.array([[0], [1.0]])
px *= 0.5 * np.array([[1], [image_length]])
x0 = px[1, :].min()
x1 = px[1, :].max()
2023-01-31 17:06:15 +00:00
img[:, int(x0):int(x1), :] = np.array(
2023-02-01 08:12:18 +00:00
3 * [color], dtype=np.uint8)
2023-01-31 17:06:15 +00:00
mask[:, int(x0):int(x1), :] = np.array([255, 255, 255], dtype=np.uint8)
2023-02-01 08:12:18 +00:00
# x, y = X_MIN + 27 * VOXEL_SIZE, Y_MIN + 66 * VOXEL_SIZE
# RT = np.linalg.inv(cam2world_projs[i])[:-1, :]
# px = np.array([[x + VOXEL_SIZE/2, y + VOXEL_SIZE/2, 1],
# [x, y, 1],
# [x + VOXEL_SIZE, y, 1],
# [x + VOXEL_SIZE, y + VOXEL_SIZE, 1],
# [x, y + VOXEL_SIZE, 1]])
# px = RT @ px.T
# px /= px[0, :]
# px += np.array([[0], [1.0]])
# px *= 0.5 * np.array([[1], [image_length]])
# img[48:52, int(px[1, 0]-1):int(px[1, 0]+1), :] = np.array([255, 0, 0], dtype=np.uint8)
# img[48:52, int(px[1, 1]-1):int(px[1, 1]+1), :] = np.array([255, 255, 0], dtype=np.uint8)
# img[48:52, int(px[1, 2]-1):int(px[1, 2]+1), :] = np.array([0, 255, 255], dtype=np.uint8)
# img[48:52, int(px[1, 3]-1):int(px[1, 3]+1), :] = np.array([255, 0, 255], dtype=np.uint8)
# img[48:52, int(px[1, 4]-1):int(px[1, 4]+1), :] = np.array([0, 255, 0], dtype=np.uint8)
2023-01-31 17:06:15 +00:00
cv2.imwrite(f"data/peanut/images/Image{i:04}.png", img)
cv2.imwrite(f"data/peanut/masks/Image{i:04}.png", mask)