是否可以将仿射变换矩阵直接应用于 Mayavi ImageActor 对象? [英] Is it possible to directly apply an affine transformation matrix to a Mayavi ImageActor object?

查看:27
本文介绍了是否可以将仿射变换矩阵直接应用于 Mayavi ImageActor 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mayavi 来渲染一些成像数据,这些数据由 3D 体积内的多个 2D 平面组成,其位置、方向和比例由 4x4 刚体仿射变换矩阵定义.每个平面包括:

I'm using Mayavi to render some imaging data that consists of multiple 2D planes within a 3D volume, the position, orientation, and scale of which are defined by 4x4 rigid body affine transformation matrices. Each plane consists of:

  • 一组 2D 图像数据,我使用 mayavi.mlab.imshow
  • 显示
  • 一组由我分别使用 mayavi.mlab.points3dmayavi.mlab.plot3d 绘制的线和点组成的 ROI.
  • An array of 2D image data, which I display using mayavi.mlab.imshow
  • A set of ROIs consisting of lines and points that I draw using mayavi.mlab.points3d and mayavi.mlab.plot3d respectively.

我将我的点和线顶点从 2D 参考平面转换到 3D 空间,方法是用仿射矩阵点它们的坐标.根据我之前的问题/答案这里,我发现我可以单独设置 ImageActor 对象的位置和方向,使用:

I transform my points and line vertices from a 2D reference plane into the 3D space by dotting their coordinates with my affine matrix. Based on my previous question/answer here, I figured out that I could set the positions and orientations of the ImageActor objects individually, using:

obj = mlab.imshow(img)
obj.actor.orientation = [pitch, roll, yaw] # the required orientation (deg)
obj.actor.position = [dx, dy, dz] # the required position 
obj.actor.scale = [sx, sy, sz] # the required scale

现在的情节是这样的:

一切都很好地排列,但很难解释,因为平面在 z 方向上的间隔非常密集.我现在想要做的是通过一些比例因子拉伸"z 轴.在点和线的情况下,这很容易做到 - 我所做的就是将所有转换后的 z 坐标乘以一个比例因子.

Everything lines up nicely, but it's very difficult to interpret because the planes are so densely spaced in z. What I'd now like to be able to do is 'stretch out' the z-axis by some scaling factor. In the case of the points and lines, this is very easy to do - all I do is multiply all of the transformed z-coordinates by a scaling factor.

但是,我不知道如何将相同的转换应用于图像.如果我只是缩放 z 位置,图像的旋转和缩放当然是错误的,并且我绘制的点/线将不再与图像位于同一平面上:

However, I can't figure out how to apply the same transformation to the images. If I just scale the z-position, the rotation and scaling of the images will of course be wrong, and my plotted points/lines will no longer fall on the same plane as the image:

我需要做的是对我的图像应用一个非刚性仿射变换,其中包含剪切以及旋转、平移和缩放.

What I need to do is apply a non-rigid affine transformation that incorporates shear as well as rotation, translation, and scaling to my images.

有什么方法可以手动对 ImageActor 应用剪切,或者更好的是直接应用我预先计算的任意 4x4 仿射矩阵?

Is there any way I can manually apply shear to an ImageActor, or even better just directly apply an arbitrary 4x4 affine matrix that I've precomputed?

推荐答案

ImageActor 最终是 tvtk.ImageActor 的包装器,有一个 user_matrix 属性,可让您分配 4D 变换矩阵.

ImageActor, which ultimately is a wrapper for tvtk.ImageActor, has a user_matrix property, which lets you assign a 4D transformation matrix.

从随机图像开始,

    import numpy as np
    from mayavi.mlab import imshow
    s = np.random.random((10, 10))
    image = imshow(s, colormap='gist_earth', interpolate=False)

给我们以下...

创建一个变换矩阵并设置一个项以使其具有一定的剪切力...

Creating a transformation matrix and setting a term to give it some shear ...

    from tvtk.api import tvtk
    transform_matrix = tvtk.Matrix4x4()
    transform_matrix.set_element(0, 1, 2.5)
    image.actor.user_matrix = transform_matrix

给我们...

set_element 具有签名 (row, col, value),因此您应该能够根据需要在该矩阵上设置元素.

set_element has the signature (row, col, value), so you should be able to set elements on that matrix as needed.

这篇关于是否可以将仿射变换矩阵直接应用于 Mayavi ImageActor 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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