Simple-image-to-3d / model_generator.py
Felguk's picture
Create model_generator.py
4468629 verified
raw
history blame contribute delete
980 Bytes
import numpy as np
import trimesh
from scipy.spatial import Delaunay
class ModelGenerator:
def __init__(self):
pass
def depth_to_points(self, depth_map, scale=1.0):
"""Convert depth map to 3D points"""
height, width = depth_map.shape
x, y = np.meshgrid(np.arange(width), np.arange(height))
# Create points array
points = np.stack([x.flatten(), y.flatten(), depth_map.flatten() * scale], axis=1)
return points
def generate_mesh(self, points):
"""Generate a 3D mesh from points"""
# Project points to 2D for triangulation
points_2d = points[:, :2]
# Create triangulation
tri = Delaunay(points_2d)
# Create mesh
mesh = trimesh.Trimesh(vertices=points, faces=tri.simplices)
return mesh
def save_mesh(self, mesh, filename):
"""Save the mesh to a file"""
mesh.export(filename)