【问题标题】:opencv python Triangular mesh having coordinates具有坐标的opencv python三角形网格
【发布时间】:2016-03-17 14:14:10
【问题描述】:

我在 stl 文件中有一个网格 (https://dl.dropboxusercontent.com/u/710615/stlMidpoint.stl)

使用此代码:

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]

我能够得到与三角形表面的点有对应关系(我认为)的坐标。 我正在尝试绘制三角形但没有成功。这些点在那里,但不是三角形格式。 我正在使用 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

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

【问题讨论】:

    标签: python opencv mesh triangular


    【解决方案1】:

    使用 numpy-stl (https://github.com/WoLpH/numpy-stl) 我可以轻松打开、读取和渲染文件:

    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()
    

    然而,巨大的尺寸让它真的很慢。

    【讨论】:

    • 如何导出为 numpy 向量并在其他图像中叠加?例如,我如何将这个网格应用到此图像 dl.dropboxusercontent.com/u/710615/La1.png 上。使用 matplotlib 提取 numpy 数组我会得到点,而不是三角形。
    • 您可以使用figure.savefig('image.png', dpi=...)将图像存储为高分辨率图像。或者使用fig.canvas 直接访问数据。该向量实际上已经可以通过your_mesh.vectors[:, :, :2] 获得
    • 你知道在其他上下文中不同于 matplotlib 的绘图方式吗?
    • 您可以使用 Pillow (pillow.readthedocs.org) 来渲染图像。只需直接绘制多边形:pillow.readthedocs.org/en/3.1.x/reference/… 但我不得不说,我仍然不完全确定您要在这里实现什么,以及这是否是解决您问题的最简单解决方案。
    • 我需要将此网格应用于这样的图像:dl.dropboxusercontent.com/u/710615/La1.png。所以我需要确保尺寸是相同的,最好使用相同的“模块”。我正在使用 opencv
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多