问题描述
我有一些包含多个 2D 图像的数据,我想使用 mayavi2 (v4.3.0)
在相对于彼此的特定 [x,y,z] 位置呈现这些数据.>
从文档看来我应该能够用 mlab.imshow()
做到这一点.不幸的是,当我调用 imshow
并指定 extent
参数(AttributeError: 'ImageActor' object has no attribute 'actor'
)时,mayavi 会抛出异常.
我也尝试通过修改 im.mlab_source.x,y,z...
直接设置 x、y 和 z 数据.奇怪的是,虽然这正确地改变了 x 和 y 范围,但它对 z 位置没有任何影响,即使 im.mlab_source.z
明显改变.
这是一个可运行的示例:
将 numpy 导入为 np从 scipy.misc 导入 lena从 mayavi 导入 mlabdef normal_imshow(img=lena()):返回 mlab.imshow(img,colormap='gray')def set_extent(img=lena()):return mlab.imshow(img,extent=[0,100,0,100,50,50],colormap='cool')def set_xyz(img=lena()):im = mlab.imshow(img,colormap='hot')src = im.mlab_source打印 '旧 z :',src.zsrc.x = 100*(src.x - src.x.min())/(src.x.max() - src.x.min())src.y = 100*(src.y - src.y.min())/(src.x.max() - src.y.min())src.z[:] = 50打印 'New z :',src.z返回我如果 __name__ == '__main__':# 这有效normal_imshow()# # 这失败(AttributeError)# set_extent()# 奇怪的是,这似乎适用于 x 和 y 轴,但不会改变# 即使 data.z 确实改变了 z 位置set_xyz()
好吧,原来这是一个已知错误 在 mayavi 中.但是,可以在创建后更改 ImageActor
对象的方向、位置和比例:
obj = mlab.imshow(img)obj.actor.orientation = [0, 0, 0] # 需要的方向obj.actor.position = [0, 0, 0] # 需要的位置obj.actor.scale = [0, 0, 0] # 需要的比例
I have some data that consists of several 2D images that I would like to render in specific [x,y,z] positions relative to one another using mayavi2 (v4.3.0)
.
From the documentation it seems that I should just be able to do this with mlab.imshow()
. Unfortunately, mayavi throws an exception when I call imshow
specifying the extent
parameter (AttributeError: 'ImageActor' object has no attribute 'actor'
).
I also tried setting the x,y and z data directly by modifying im.mlab_source.x,y,z...
. Weirdly, whilst this correctly changes the x and y extents, it does nothing to the z-position even though im.mlab_source.z
clearly changes.
Here's a runnable example:
import numpy as np
from scipy.misc import lena
from mayavi import mlab
def normal_imshow(img=lena()):
return mlab.imshow(img,colormap='gray')
def set_extent(img=lena()):
return mlab.imshow(img,extent=[0,100,0,100,50,50],colormap='cool')
def set_xyz(img=lena()):
im = mlab.imshow(img,colormap='hot')
src = im.mlab_source
print 'Old z :',src.z
src.x = 100*(src.x - src.x.min())/(src.x.max() - src.x.min())
src.y = 100*(src.y - src.y.min())/(src.x.max() - src.y.min())
src.z[:] = 50
print 'New z :',src.z
return im
if __name__ == '__main__':
# this works
normal_imshow()
# # this fails (AttributeError)
# set_extent()
# weirdly, this seems to work for the x and y axes, but does not change
# the z-postion even though data.z does change
set_xyz()
Ok, it turns out that this is a known bug in mayavi. However, it is possible to change the orientation, position and scale of an ImageActor
object after it has been created:
obj = mlab.imshow(img)
obj.actor.orientation = [0, 0, 0] # the required orientation
obj.actor.position = [0, 0, 0] # the required position
obj.actor.scale = [0, 0, 0] # the required scale
这篇关于mayavi - 以编程方式设置图像的 [x,y,z] 范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!