1

I have a mesh in a stl file ( https://dl.dropboxusercontent.com/u/710615/stlMidpoint.stl )

With this code :

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

def unique(a):
   order = np.lexsort(a.T)
   a = a[order]
   diff = np.diff(a, axis=0)
   ui = np.ones(len(a), 'bool')
   ui[1:] = (diff != 0).any(axis=1) 

  return a[ui]

 A = np.loadtxt("vectors.txt")
 A = A[np.logical_not(A[:,2] > 0)]
 uniqA = unique(A)
 coordA = A[:,0:2]

I'm able to get the coordinates which has correspondence (I think) to the points of triangles surface. I'm trying draw the triangles without success. The points are there but not on a triangles format. I'm using polines:

img= cv2.imread('nimg.jpg')
imgMask = np.ones(img.shape[:2], dtype="uint8")*255
m_xor= np.ones(imgMask.shape, dtype="uint8") * 255

points = np.array(uniqA[:,0:2], np.int32)
print points
cv2.polylines( m_xor,[points], 1, (0,0,0))
cv2.imwrite('result.jpg', m_xor)

vectors.txt : https://dl.dropboxusercontent.com/u/710615/vectors.txt

nimg: https://dl.dropboxusercontent.com/u/710615/nimg.jpg

1 Answer 1

3
+50

Using numpy-stl (https://github.com/WoLpH/numpy-stl) I can open, read and render the file easily:

from stl import mesh
from matplotlib import collections
from matplotlib import pyplot

# Create a figure and axes
figure, axes = pyplot.subplots()

# Read the STL file
your_mesh = mesh.Mesh.from_file('stlMidPoint.stl')

# Scale the image to the STL dimensions
axes.set_xlim(your_mesh.min_[0], your_mesh.max_[0])
axes.set_ylim(your_mesh.min_[1], your_mesh.max_[1])

# Add the polygons, but only the X and Y axis since it's 2D
axes.add_collection(collections.PolyCollection(your_mesh.vectors[:, :, :2]))

# Make sure the aspect ratio stays correct
pyplot.gca().set_aspect('equal')

# Render!
pyplot.show()

The enormous size makes it really slow however.

enter image description here

Sign up to request clarification or add additional context in comments.

13 Comments

How can I export as numpy vector and overtlay in other images?For instance, how can I apply this mesh on this image dl.dropboxusercontent.com/u/710615/La1.png , for instance. Extracting the numpy array using matplotlib I'll get the points, not the triangles.
You can use figure.savefig('image.png', dpi=...) to store the image as a high-resolution image. Or use fig.canvas to access the data directly. The vector is actually already readily available through your_mesh.vectors[:, :, :2]
Do you know a way of drawing in other context different from matplotlib ?
You could use Pillow (pillow.readthedocs.org) for rendering the image. Just draw the polygons directly: pillow.readthedocs.org/en/3.1.x/reference/… But I have to say that I'm still not entirely sure what you are trying to achieve here and if this is the easiest solution for your problem.
I need to apply this mesh to an image like this: dl.dropboxusercontent.com/u/710615/La1.png . So I need to assure the sizes are the sime, and it's better being using the same "module". I was working with opencv
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.