OpenCVをとにかく使ってみる
- OpenCVとは
- OpenCVのインストール(Ubuntu)(Anacondaを入れて…だったら:こちら)
- 簡単写経用
- マーチングキューブする
git clone https://github.com/pmneila/PyMCubes cd PyMCubes sudo pip install Cython sudo python setup.py install
- Colladaフォーマットを使うので(Collada フォーマット)
sudo pip install PyCollada
- その上で ! X,Y,Zとuとの座標xボクセル値の4次元データを、3次元画像データとして渡せばよい
import numpy as np import mcubes # Create a data volume (30 x 30 x 30) X, Y, Z = np.mgrid[:10, :10, :10] # たくさんの3次元座標に「値」をつける u = (X-5)**2 + (Y-5)**2 + (Z-5)**2 - 2**2 np.shape(u) print X print Y print u # Extract the 0-isosurface # 指定した値0の等高面をマーチングキューブする vertices, triangles = mcubes.marching_cubes(u, 0) vertices_xyz = np.transpose(vertices) print vertices print triangles # Export the result to sphere.dae mcubes.export_mesh(vertices, triangles, "sphere.dae", "MySphere")
from collada import * mesh = Collada('sphere.dae')
-
- プロットする
from matplotlib import pyplot from mpl_toolkits.mplot3d import Axes3D from scipy import genfromtxt fig = pyplot.figure() ax = Axes3D(fig) ax.plot(vertices_xyz[0,],vertices_xyz[1,],vertices_xyz[2,], "o") col = ["g","r","b"] cnt = 0 for i,j,k in triangles: thiscol = col[cnt] ord = [i,j,k,i] if(cnt>2): cnt = 0 for t in range(3): ax.plot([vertices[ord[t],0],vertices[ord[t+1],0]],[vertices[ord[t],1],vertices[ord[t+1],1]],[vertices[ord[t],2],vertices[ord[t+1],2]],color = col[cnt]) cnt += 1 if(cnt>2): cnt = 0 pyplot.show()
-
- 三角形プロットする
import mpl_toolkits.mplot3d as a3 import matplotlib.colors as colors import pylab as pl import scipy as sp ax = a3.Axes3D(pl.figure()) ax.plot(vertices_xyz[0,],vertices_xyz[1,],vertices_xyz[2,], "o") cnt = 0 for i,j,k in triangles: vtx = np.array([vertices[i,],vertices[j,],vertices[k,]]) tri = a3.art3d.Poly3DCollection([vtx]) tri.set_color(colors.rgb2hex(sp.rand(3))) tri.set_edgecolor('k') cnt += 1 #if cnt < 100000: ax.add_collection3d(tri) pl.show()
-
- 4D オブジェクトには未対応
import numpy as np import mcubes # Create a data volume (30 x 30 x 30) X, Y, Z, W = np.mgrid[:10, :10, :10, :10] # たくさんの3次元座標に「値」をつける u = (X-5)**2 + (Y-5)**2 + (Z-5)**2 - + (W-5)**2 - 2**2 np.shape(u) # Extract the 0-isosurface # 指定した値0の等高面をマーチングキューブする vertices, triangles = mcubes.marching_cubes(u, 0)
-
-
- エラー
-
RuntimeError Traceback (most recent call last) <ipython-input-29-bad4ae16da1c> in <module>() 10 # Extract the 0-isosurface 11 # 指定した値0の等高面をマーチングキューブする ---> 12 vertices, triangles = mcubes.marching_cubes(u, 0) 13 14 print vertices mcubes/src/_mcubes.pyx in _mcubes.marching_cubes (mcubes/src/_mcubes.cpp:1590)() RuntimeError: Only three-dimensional arrays are supported.
|