aminzabardast / MastersThesis

0 stars 0 forks source link

Converting Disparity Map to Mesh #47

Closed aminzabardast closed 6 years ago

aminzabardast commented 6 years ago

Check and try to create and save mesh with and without disparity maps.

aminzabardast commented 6 years ago

It is perfectly possible to save the data into a mesh. Download Example STL File.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
from mpl_toolkits.mplot3d import Axes3D
from stl import mesh

# Generating an equally spaced grid
x = np.arange(0, 100, 5)
x, y = np.meshgrid(x, x)
x = x.flatten()
y = y.flatten()

# Generating depth and magnifying the values since their absolute value is less than 1
z = (np.sinc((x-20)/100*3.14) + np.sinc((y-50)/100*3.14))*30

# Creating triangles from point (x, y) point cloud.
tries = mtri.Triangulation(x, y)

# # Uncomment Plotting Calculated Triangles and Surface
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1)
# ax.triplot(tries)  # Triangles
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1, projection='3d')
# ax.plot_trisurf(x, y, z, triangles=tries.triangles, cmap='jet')  # Surface
# plt.show()  # Show plot

# Creating Mesh in the size of triangles
data = np.zeros(len(tries.triangles), dtype=mesh.Mesh.dtype)
mesh = mesh.Mesh(data, remove_empty_areas=False)

# Populating the mesh
mesh.x[:] = x[tries.triangles]
mesh.y[:] = y[tries.triangles]
mesh.z[:] = z[tries.triangles]

# Saving the mesh
mesh.save('mesh.stl')

Requirements:

numpy
matplotlib
numpy-stl