3D Mesh Visualization and Rotation

Requirements

  • Open3D
  • NumPy
  • OpenCV
  • PIL
  • IPython

Installation

To install the required packages, run:

Usage

  1. Update the wildtype_image_path variable in the script to the path of your .ply file.
  2. Run the script:

The script will visualize the 3D mesh, rotate it, and save the resulting frames as images in the output_frames folder.

Details

The script uses the Open3D library to read a 3D mesh from a .ply file and visualize it. It then rotates the mesh and captures the frames of the rotation, saving them as images.

The script first reads the 3D mesh from the specified .ply file and computes its vertex normals. It then initializes the Open3D visualizer and sets up the parameters for the rotation and frame saving.

The script then enters a loop where it rotates the mesh by a small amount, adds it to the visualizer, captures the current visualization as an image, converts the image to an OpenCV-compatible format, and saves the image as a .png file in the output_frames folder.

The script rotates the mesh 360 degrees over the specified number of frames and saves each frame as an image.

import open3d as o3d
import numpy as np
import cv2
import os

wildtype_image_path = "images/Wildtype63D.ply"
mesh = o3d.io.read_triangle_mesh(wildtype_image_path)
mesh.compute_vertex_normals()


# we can display the image as a point cloud
# pcd_WildType = mesh.sample_points_uniformly(number_of_points=500)
# o3d.visualization.draw_geometries([pcd_WildType])


# Define parameters for frame saving
output_frames_folder = "output_frames"
os.makedirs(output_frames_folder, exist_ok=True)
num_frames = 10  # Number of frames
rotation_degrees_per_frame = 360 / num_frames  # Rotate 360 degrees over the frames

# Initialize Open3D visualizer
vis = o3d.visualization.Visualizer()
vis.create_window()

# Save the frames as images
for frame_idx in range(num_frames):
    # Clear the visualizer and add the mesh at the current rotation
    vis.clear_geometries()
    mesh.rotate(mesh.get_rotation_matrix_from_xyz((0, np.radians(rotation_degrees_per_frame * frame_idx), 0)))
    vis.add_geometry(mesh)

    # Capture the current visualization as an image
    vis.poll_events()
    vis.update_renderer()
    color_image = vis.capture_screen_float_buffer()

    # Convert the Open3D image to an OpenCV-compatible format
    color_image_cv = np.array(color_image)
    color_image_cv = cv2.cvtColor((color_image_cv * 255).astype(np.uint8), cv2.COLOR_RGB2BGR)  # OpenCV uses BGR format

    # Save the frame as an image
    frame_filename = os.path.join(output_frames_folder, f"frame_{frame_idx:03d}.png")
    cv2.imwrite(frame_filename, color_image_cv)

# Release the visualizer
vis.destroy_window()
Farouk Yahaya, PhD
Farouk Yahaya, PhD
Postdoctoral Research Associate