如何在 Mayavi 中正确显示具有非立方体素的体积 [英] How to display a volume with non-cubic voxels correctly in mayavi

查看:35
本文介绍了如何在 Mayavi 中正确显示具有非立方体素的体积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mayavi (3.3.2) 来显示体积等值面.

通常,我的体积没有立方体素;例如,采样网格可能在 X 和 Y 方向为 1mm x 1mm,但在 Z 方向为 1.4mm.

如何使用 mayavi 的 mlab.contour3dmlab.pipeline.iso_surface 以正确的空间比例显示此类体积?我真的不想将体积重新采样为立方网格.

说明问题的另一种方式:我该怎么做才能让下面的代码显示一个球体而不是一个扁平的椭球体(将 volume 与它预期的 1:1:2 纵横比体素作为给定的,无需重新生成或重新采样体积).

将 numpy 导入为 np从 enthought.mayavi 导入 mlabdef sqr(x): 返回 x*xs=64x,y,z = np.ogrid[0:s,0:s,0:s/2]体积 = np.sqrt(sqr(x-s/2)+sqr(y-s/2)+sqr(2*z-s/2))isos = mlab.contour3d(volume,contours=[5,15,25],transparent=True)mlab.show()

我猜应该有某种方法可以进入底层的 VTK 图形管道(它的变换等)并插入适当的各向异性缩放(如果没有某种方法可以通过 mlab API 更直接地做到这一点).

解决方案

为此,从输入数据显式创建 scalar_field 对象是最简单的方法.

我实际上经常这样做,因为我们喜欢深入地质学(正值向下).这意味着您需要在 z 方向上进行负增量.如果只是各种 mlab 函数的参数就好了,但它仍然不难做到.

from mayavi import mlab将 numpy 导入为 nps=64x,y,z = np.ogrid[0:s,0:s,0:s/2]数据 = np.sqrt((x-s/2)**2 + (y-s/2)**2 + (2*z-s/2)**2)网格 = mlab.pipeline.scalar_field(data)grid.spacing = [1.0, 1.0, 2.0]轮廓 = mlab.pipeline.contour_surface(网格,轮廓=[5,15,25],透明=真)mlab.show()

I'm using mayavi (3.3.2) to display volume isosurfaces.

Generally, my volumes do not have cubic voxels; for example, the sampling grid might be 1mm x 1mm in X and Y, but 1.4mm in the Z direction.

How can I get such volumes to display with the correct spatial proportions using mayavi's mlab.contour3d or mlab.pipeline.iso_surface ? I'd really prefer to not resample the volumes to a cubic grid.

Another way of stating the problem: what can I do to get the below code to display a sphere instead of a flattened elipsoid (taking the volume with it's intended 1:1:2 aspect-ratio voxels as a given, and without regenerating or resampling the volume).

import numpy as np
from enthought.mayavi import mlab

def sqr(x): return x*x

s=64
x,y,z = np.ogrid[0:s,0:s,0:s/2]

volume = np.sqrt(sqr(x-s/2)+sqr(y-s/2)+sqr(2*z-s/2))

isos = mlab.contour3d(volume,contours=[5,15,25],transparent=True)
mlab.show()

I'm guessing there ought to be some way of getting at the underlying VTK graphics pipeline (its transforms etc) and inserting the appropriate anisotropic scaling (if there isn't some way of doing it more directly through the mlab API).

解决方案

For this, it's easiest to explicitly create a scalar_field object from the input data.

I actually do this quite frequently, as we like to put things in depth (where positive is downwards) in geology. That means that you need a negative increment in the z-direction. It would be nice if it was just an argument to the various mlab functions, but its still not too hard to do.

from mayavi import mlab
import numpy as np

s=64
x,y,z = np.ogrid[0:s,0:s,0:s/2]

data = np.sqrt((x-s/2)**2 + (y-s/2)**2 + (2*z-s/2)**2)

grid = mlab.pipeline.scalar_field(data)
grid.spacing = [1.0, 1.0, 2.0]

contours = mlab.pipeline.contour_surface(grid, 
                         contours=[5,15,25], transparent=True)
mlab.show()

这篇关于如何在 Mayavi 中正确显示具有非立方体素的体积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆