2013-08-30 15 views
11

Pythonライブラリを使用して、points3dクラスを使用して3Dポイントをプロットしています。 The documentationは、各点の色は、第四の引数を通してsに指定されていることを指定します。MayaViの3Dポイントの絶対色を指定してください

加えて、第四のアレイのX、Y、 およびz関連するスカラー値を与えると同じ形状のを渡すことができ各点について、または関数 f(x、y、z)がスカラー値を返します。このスカラー値は、 を使用して、ポイントの色とサイズを調整することができます。

これはcolourmapにポイントをマッピングする各点に対するスカラー値を指定する、などcopperjet又はhsv。例えば。それらのドキュメントから:

import numpy 
from mayavi.mlab import * 

def test_points3d(): 
    t = numpy.linspace(0, 4*numpy.pi, 20) 
    cos = numpy.cos 
    sin = numpy.sin 

    x = sin(2*t) 
    y = cos(t) 
    z = cos(2*t) 
    s = 2+sin(t) 

    return points3d(x, y, z, s, colormap="copper", scale_factor=.25) 

を与える:

enter image description here

その代わりに、私は(R、G、B)のタプルとして各点について実際の値を指定したいです。これはMayaViでも可能ですか? sをタプルの配列に置き換えようとしましたが、エラーがスローされます。

答えて

1

これは今単純で行うことができますcolor引数

from mayavi import mlab 
import numpy as np 

c = np.random.rand(200, 3) 
r = np.random.rand(200)/10. 

mlab.points3d(c[:, 0], c[:, 1], c[:, 2], r, color=(0.2, 0.4, 0.5)) 

mlab.show() 

enter image description here

3

ルックアップテーブルを使用して、必要なロジックを使用してrgb値をマップすることができます。ここでは簡単な例です:

import numpy, random 
from mayavi.mlab import * 

def cMap(x,y,z): 
    #whatever logic you want for colors 
    return [random.random() for i in x] 

def test_points3d(): 
    t = numpy.linspace(0, 4*numpy.pi, 20) 
    cos = numpy.cos 
    sin = numpy.sin 

    x = sin(2*t) 
    y = cos(t) 
    z = cos(2*t) 
    s = cMap(x,y,z) 

    return points3d(x, y, z, s, colormap="spectral", scale_factor=0.25) 

test_points3d() 

私はあなたが望むカラースキームは考えているが、あなたはX、Y、Zの位置を評価し、あなたが求めているのRGB値に対応するものは何でもスカラー返すことができます。

+0

256x256x256値でユニークなカラーマップを指定せずに、任意の0〜255のRGB値を生成するためにこれを行う方法はありますか? 逆のスペクトル()関数を特徴付ける方法があります。スペクトラル()はドメイン[0,1]と0-255,0-255,0の範囲の関数です。 255? この現象はいつも気にならず、いくつかのアプリケーションが周りを回っているのを見ています(たとえば、所有者がソースを私にリリースすることを拒否したvtkプログラムのtrackvisesなど)。 – aestrivex

+0

mayavi.core.lut_manager、特にmayavi.core.lut_manager内のpylab_lutsを調べて、これらがどのように構築されているかを確認することができます(自分自身を構築し、任意の動作を割り当てようとする) – Chrismit

+0

これで遊んだことがあります。スカラが指定されている各頂点に一意のRGB値を指定するよりも、カスタムlutsを使用して目的のカラースキームを指定する方がはるかに一般的ではありません。 RGBカラースペクトルを一意に定義する256x256x256の値を持つLUTを指定することができるため、これは真実ではありません。しかし実際にそれをすることは特別な痛みであろう。 この質問に対する正しい答えは「申し訳ありませんが、これを行う正しい方法はありません」と思います。 – aestrivex

3

色を直接設定するより良い方法を見つけました。

独自の直接LUTを簡単に作成することができます。我々は256 ** 3粒度をしたいとしましょう:

#create direct grid as 256**3 x 4 array 
def create_8bit_rgb_lut(): 
    xl = numpy.mgrid[0:256, 0:256, 0:256] 
    lut = numpy.vstack((xl[0].reshape(1, 256**3), 
         xl[1].reshape(1, 256**3), 
         xl[2].reshape(1, 256**3), 
         255 * numpy.ones((1, 256**3)))).T 
    return lut.astype('int32') 

# indexing function to above grid 
def rgb_2_scalar_idx(r, g, b): 
    return 256**2 *r + 256 * g + b 

#N x 3 colors 
colors = numpy.array([_.color for _ in points]) 

#N scalars 
scalars = numpy.zeros((colors.shape[0],)) 

for (kp_idx, kp_c) in enumerate(colors): 
    scalars[kp_idx] = rgb_2_scalar_idx(kp_c[0], kp_c[1], kp_c[2]) 

rgb_lut = create_8bit_rgb_lut() 

points_mlab = mayavi.mlab.points3d(x, y, z 
            keypoint_scalars, 
            mode = 'point') 

#magic to modify lookup table 
points_mlab.module_manager.scalar_lut_manager.lut._vtk_obj.SetTableRange(0, rgb_lut.shape[0]) 
points_mlab.module_manager.scalar_lut_manager.lut.number_of_colors = rgb_lut.shape[0] 
points_mlab.module_manager.scalar_lut_manager.lut.table = rgb_lut 
7

、今日のほとんどのためにこれで苦労した後、私は質問を尋ねる正確に何をすべきか、比較的簡単な方法を発見した - 各点のRGBタプルを指定します。トリックは、プロットする点があるとしてだけでエントリーのとまったく同じ数のカラーマップを定義することで、その後、インデックスのリストを引数として使用する設定:

# Imports 
import numpy as np 
from mayavi.mlab import quiver3d, draw 

# Primitives 
N = 200 # Number of points 
ones = np.ones(N) 
scalars = np.arange(N) # Key point: set an integer for each point 

# Define color table (including alpha), which must be uint8 and [0,255] 
colors = (np.random.random((N, 4))*255).astype(np.uint8) 
colors[:,-1] = 255 # No transparency 

# Define coordinates and points 
x, y, z = colors[:,0], colors[:,1], colors[:,2] # Assign x, y, z values to match color 
pts = quiver3d(x, y, z, ones, ones, ones, scalars=scalars, mode='sphere') # Create points 
pts.glyph.color_mode = 'color_by_scalar' # Color by scalar 

# Set look-up table and redraw 
pts.module_manager.scalar_lut_manager.lut.table = colors 
draw() 
+0

これは最も簡単な答えです。 – chris

+0

震えるよりも他の種類のプロットでこれをやりたければ、それは重要なものである2番目の最終行です。つまり、 'surf.module_manager.scalar_lut_manager.lut.table = colors'も実行できます。 – RolKau

関連する問題