mayavi在表面上映射离散的颜色条

时间:2014-10-17 15:41:41

标签: python mayavi surface colormap mayavi.mlab

我想根据另一个取代离散值的值(ID)来改变曲面的颜色(所以我需要一个离散的颜色条)。 在下面的简化示例中,我绘制了一个包含3个不同ID的球体:

左边的

0 /红色

2 /中间的蓝色

左边是

1 /绿色

但是使用下面的代码,我在红色和蓝色之间的极限处获得了一些奇怪的行为(绿点)。 这可能是因为插值!

守则:

from mayavi import mlab
import numpy as np

# my dataset -simplified-
x,y,z = np.mgrid[-3:3:100j, -3:3:100j, -3:3:100j]
values = np.sqrt(x**2 + y**2 + z **2)

# my color values : the volume is divided in 3 sub-volumes along x taking 
colorvalues=np.empty(values.shape)
colorvalues[0:33,:,:]=0.
colorvalues[33:66,:,:]=2.
colorvalues[66:,:,:]  =1.

src = mlab.pipeline.scalar_field(values)
src.image_data.point_data.add_array(colorvalues.T.ravel())
src.image_data.point_data.get_array(1).name = 'myID'
src.image_data.point_data.update()

# the surface i am interested on
contour = mlab.pipeline.contour(src)
contour.filter.contours= [2.8,]

# to map the ID
contour2 = mlab.pipeline.set_active_attribute(contour, point_scalars='myID')

# And we display the surface The colormap is the current attribute: the ID.
mySurf=mlab.pipeline.surface(contour2)

# I change my colormap to a discrete one : R-G-B 
mySurf.module_manager.scalar_lut_manager.lut.table = np.array([[255,0,0,255],[0,255,0,255],[0,0,255,255]])

mlab.colorbar(title='ID', orientation='vertical', nb_labels=3)      
mlab.show()

Image

我在mlab.show()之前尝试过这一行:

mySurf.actor.mapper.interpolate_scalars_before_mapping = True

渲染效果更好,但绿点变成绿色条纹。

Image

1 个答案:

答案 0 :(得分:0)

我通过使用scipy最近邻插值并使用我的对象的单元格颜色找到了我的答案。

from mayavi import mlab
import numpy as np
import scipy.interpolate

# my dataset -simplified-
x,y,z = np.mgrid[-3:3:100j, -3:3:100j, -3:3:100j]
values = np.sqrt(x**2 + y**2 + z **2)

# my color values : the volume is divided in 3 sub-volumes along x taking 
colorvalues=np.empty(values.shape)
colorvalues[0:33,:,:]=0.
colorvalues[33:66,:,:]=2.
colorvalues[66:,:,:]  =1.

src = mlab.pipeline.scalar_field(x,y,z ,values)

# the surface i am interested on
contour = mlab.pipeline.contour(src)
contour.filter.contours= [2.8,]

# I extract points that form my surface
PtsCoord = contour.outputs[0].points.to_array()

# then the variable that contains the indices of the points forming triangles.
PolyAndTriIDs = contour.outputs[0].polys.to_array() 
PolyAndTriIDs = PolyAndTriIDs.reshape(PolyAndTriIDs.size/4,4)

# Coordinates of each triangle
x1,y1,z1 = PtsCoord[PolyAndTriIDs[:,1]].T
x2,y2,z2 = PtsCoord[PolyAndTriIDs[:,2]].T
x3,y3,z3 = PtsCoord[PolyAndTriIDs[:,3]].T

# I interpolate the color value at the center of triangles with the Nearest-neighbour interpolation method
interp0 = scipy.interpolate.NearestNDInterpolator( (x.ravel(),y.ravel(),z.ravel()), colorvalues.ravel() )
result0 = interp0((np.mean((x1,x2,x3),0),np.mean((y1,y2,y3),0),np.mean((z1,z2,z3),0)))

# Displaying with triangular_mesh and color given by cell scalar value
mesh = mlab.triangular_mesh(PtsCoord[:,0], PtsCoord[:,1], PtsCoord[:,2], PolyAndTriIDs[:,1:])
cell_data = mesh.mlab_source.dataset.cell_data
cell_data.scalars = result0
cell_data.scalars.name = 'Cell data'
cell_data.update()
mesh.actor.mapper.scalar_mode = 'use_cell_data'
mesh.module_manager.scalar_lut_manager.lut.table = np.array([[255,0,0,255],[0,255,0,255],[0,0,255,255]])
mesh.module_manager.scalar_lut_manager.use_default_range = False 
mesh.module_manager.scalar_lut_manager.data_range = [ 0.,  2.]

mlab.colorbar(title='ID', orientation='vertical', nb_labels=3)

结果不太顺畅但更相关。

exmple OK