Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
world_grid = make_world_grid()
image_grid = world2image(world_grid, cam2world, sensor_size, image_size,
focal_length)
plt.figure(figsize=(12, 5))
ax = plt.subplot(121, projection="3d")
ax.view_init(elev=30, azim=-70)
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.set_zlim((-1, 1))
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plot_transform(ax)
plot_transform(ax, A2B=cam2world)
ax.set_title("Camera and world frames")
ax.scatter(world_grid[:, 0], world_grid[:, 1], world_grid[:, 2])
ax.scatter(world_grid[-1, 0], world_grid[-1, 1], world_grid[-1, 2], color="r")
for p in world_grid[::10]:
ax.plot([p[0], cam2world[0, 3]],
[p[1], cam2world[1, 3]],
[p[2], cam2world[2, 3]], c="k", alpha=0.2, lw=2)
ax = plt.subplot(122, aspect="equal")
ax.set_title("Camera image")
ax.set_xlim(0, image_size[0])
ax.set_ylim(0, image_size[1])
ax.scatter(image_grid[:, 0], -(image_grid[:, 1] - image_size[1]))
ax.scatter(image_grid[-1, 0], -(image_grid[-1, 1] - image_size[1]), color="r")
plt.show()
image_size = (640, 480)
world_grid = make_world_grid()
image_grid = world2image(world_grid, cam2world, sensor_size, image_size,
focal_length)
plt.figure(figsize=(12, 5))
ax = plt.subplot(121, projection="3d")
ax.view_init(elev=30, azim=-70)
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.set_zlim((-1, 1))
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plot_transform(ax)
plot_transform(ax, A2B=cam2world)
ax.set_title("Camera and world frames")
ax.scatter(world_grid[:, 0], world_grid[:, 1], world_grid[:, 2])
ax.scatter(world_grid[-1, 0], world_grid[-1, 1], world_grid[-1, 2], color="r")
for p in world_grid[::10]:
ax.plot([p[0], cam2world[0, 3]],
[p[1], cam2world[1, 3]],
[p[2], cam2world[2, 3]], c="k", alpha=0.2, lw=2)
ax = plt.subplot(122, aspect="equal")
ax.set_title("Camera image")
ax.set_xlim(0, image_size[0])
ax.set_ylim(0, image_size[1])
ax.scatter(image_grid[:, 0], -(image_grid[:, 1] - image_size[1]))
ax.scatter(image_grid[-1, 0], -(image_grid[-1, 1] - image_size[1]), color="r")
PA[:, :3] = 0.1 * random_state.randn(10, 3)
PA[:, 0] += 0.3
PA[:, :3] += 0.3
x_translation = -0.1
y_translation = 0.2
z_rotation = np.pi / 4.0
A2B = np.array([
[np.cos(z_rotation), -np.sin(z_rotation), 0.0, x_translation],
[np.sin(z_rotation), np.cos(z_rotation), 0.0, y_translation],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
PB = transform(A2B, PA)
plot_transform(ax=ax, A2B=np.eye(4))
ax.scatter(PA[:, 0], PA[:, 1], PA[:, 2], c="orange")
plot_transform(ax=ax, A2B=A2B, ls="--", alpha=0.5)
ax.scatter(PB[:, 0], PB[:, 1], PB[:, 2], c="cyan")
axis_arrow = Arrow3D(
[0.7, 0.3],
[0.4, 0.9],
[0.2, 0.2],
mutation_scale=20, lw=3, arrowstyle="-|>", color="k")
ax.add_artist(axis_arrow)
plt.tight_layout()
plt.show()
import pytransform3d.rotations as pyrot
import pytransform3d.transformations as pytr
p = np.array([0.0, 0.0, -0.5])
a = np.array([0.0, 0.0, 1.0, np.pi])
B2A = pytr.transform_from(pyrot.matrix_from_axis_angle(a), p)
p = np.array([0.3, 0.4, 0.5])
a = np.array([0.0, 0.0, 1.0, -np.pi / 2.0])
C2B = pytr.transform_from(pyrot.matrix_from_axis_angle(a), p)
C2A = pytr.concat(C2B, B2A)
p = pytr.transform(C2A, np.ones(4))
ax = pytr.plot_transform(A2B=B2A)
pytr.plot_transform(ax, A2B=C2A)
ax.scatter(p[0], p[1], p[2])
plt.show()
PA = np.ones((10, 4))
PA[:, :3] = 0.1 * random_state.randn(10, 3)
PA[:, 0] += 0.3
PA[:, :3] += 0.3
x_translation = -0.1
y_translation = 0.2
z_rotation = np.pi / 4.0
A2B = np.array([
[np.cos(z_rotation), -np.sin(z_rotation), 0.0, x_translation],
[np.sin(z_rotation), np.cos(z_rotation), 0.0, y_translation],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
plot_transform(ax=ax, A2B=np.eye(4), ls="--", alpha=0.5)
ax.scatter(PA[:, 0], PA[:, 1], PA[:, 2], c="orange")
plot_transform(ax=ax, A2B=A2B)
axis_arrow = Arrow3D(
[0.0, -0.1],
[0.0, 0.2],
[0.2, 0.2],
mutation_scale=20, lw=3, arrowstyle="-|>", color="k")
ax.add_artist(axis_arrow)
plt.tight_layout()
plt.show()
PA[:, 0] += 0.3
PA[:, :3] += 0.3
x_translation = -0.1
y_translation = 0.2
z_rotation = np.pi / 4.0
A2B = np.array([
[np.cos(z_rotation), -np.sin(z_rotation), 0.0, x_translation],
[np.sin(z_rotation), np.cos(z_rotation), 0.0, y_translation],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
plot_transform(ax=ax, A2B=np.eye(4), ls="--", alpha=0.5)
ax.scatter(PA[:, 0], PA[:, 1], PA[:, 2], c="orange")
plot_transform(ax=ax, A2B=A2B)
axis_arrow = Arrow3D(
[0.0, -0.1],
[0.0, 0.2],
[0.2, 0.2],
mutation_scale=20, lw=3, arrowstyle="-|>", color="k")
ax.add_artist(axis_arrow)
plt.tight_layout()
plt.show()
ax = plt.subplot(121, projection="3d")
ax.set_title("Grid in 3D camera coordinate system")
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.set_zlim((0, 2))
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
cam_grid = make_world_grid(n_points_per_line=11) - np.array([0, 0, -2, 0])
img_grid = cam_grid * focal_length
c = np.arange(len(cam_grid))
ax.scatter(cam_grid[:, 0], cam_grid[:, 1], cam_grid[:, 2], c=c)
ax.scatter(img_grid[:, 0], img_grid[:, 1], img_grid[:, 2], c=c)
plot_transform(ax)
sensor_grid = cam2sensor(cam_grid, focal_length)
img_grid = sensor2img(sensor_grid, sensor_size, image_size)
ax = plt.subplot(122, aspect="equal")
ax.set_title("Grid in 2D image coordinate system")
ax.scatter(img_grid[:, 0], img_grid[:, 1], c=c)
ax.set_xlim((0, image_size[0]))
ax.set_ylim((0, image_size[1]))
plt.show()
PA[:, :3] += 0.3
x_translation = -0.1
y_translation = 0.2
z_rotation = np.pi / 4.0
A2B = np.array([
[np.cos(z_rotation), -np.sin(z_rotation), 0.0, x_translation],
[np.sin(z_rotation), np.cos(z_rotation), 0.0, y_translation],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
PB = transform(A2B, PA)
plot_transform(ax=ax, A2B=np.eye(4))
ax.scatter(PA[:, 0], PA[:, 1], PA[:, 2], c="orange")
plot_transform(ax=ax, A2B=A2B, ls="--", alpha=0.5)
ax.scatter(PB[:, 0], PB[:, 1], PB[:, 2], c="cyan")
axis_arrow = Arrow3D(
[0.7, 0.3],
[0.4, 0.9],
[0.2, 0.2],
mutation_scale=20, lw=3, arrowstyle="-|>", color="k")
ax.add_artist(axis_arrow)
plt.tight_layout()
plt.show()
import pytransform3d.transformations as pytr
p = np.array([0.0, 0.0, -0.5])
a = np.array([0.0, 0.0, 1.0, np.pi])
B2A = pytr.transform_from(pyrot.matrix_from_axis_angle(a), p)
p = np.array([0.3, 0.4, 0.5])
a = np.array([0.0, 0.0, 1.0, -np.pi / 2.0])
C2B = pytr.transform_from(pyrot.matrix_from_axis_angle(a), p)
C2A = pytr.concat(C2B, B2A)
p = pytr.transform(C2A, np.ones(4))
ax = pytr.plot_transform(A2B=B2A)
pytr.plot_transform(ax, A2B=C2A)
ax.scatter(p[0], p[1], p[2])
plt.show()