简体   繁体   English

以3D形式转换图像数据

[英]Transform image data in 3D

I have 2D image data that I'd like to represent as a plane in 3D, and perform various manipulations (translate, rotate, magnify). 我有想要以3D形式表示为平面的2D图像数据,并执行各种操作(平移,旋转,放大)。 I'd like to obtain the cartesian components and color value of the pixels, after such operations. 经过这样的操作后,我想获得笛卡尔分量和像素的颜色值。

So, what is an efficient way to: 因此,什么是有效的方法:

  • represent row/col values of an image as cartesian values 将图像的行/列值表示为笛卡尔值
  • transform those cartesian values as described above 如上所述转换那些笛卡尔值

I'm sure there are libraries that will do most of the heavy lifting (np.linalg?) but I just don't know which ones are where I should start. 我确定有些库可以完成大部分繁重的工作(np.linalg?),但我只是不知道应该从哪个库开始。 Thanks. 谢谢。

You can use scipy for such things. 您可以将scipy用于此类事情。 In particular, the scipy.ndimage module can do translations , rotations , and magnification , among other transformations and mappings. 特别是, scipy.ndimage模块可以进行平移旋转放大以及其他变换和映射。 These operations use interpolation when necessary to fit into the grid of rectangular arrays. 这些操作在必要时使用插值法以适合矩形阵列的网格。

If you want to work directly on the coordinates of pixels without interpolation, an image library may not work. 如果要直接在像素坐标上工作而不进行插值,则图像库可能无法工作。 You can grab the coordinates of the array with np.indices , and run them through any transform you'd like, and the original will associate with the original pixel value. 您可以使用np.indices获取数组的坐标,然后通过所需的任何转换对其进行运行, np.indices坐标将与原始像素值关联。 Unfortunately these transformations don't seem to be implemented in a common library, so you have to search for functions, eg, Python - Rotation of 3D vector . 不幸的是,这些转换似乎没有在公共库中实现,因此您必须搜索函数,例如Python-3D vector的旋转

An example with the rotation from the linked answer: 链接答案的旋转示例:

a = np.arange(12).reshape(3, 4, 1) # a 2D image in 3D (hence the extra dim of 1)
i, j, k = np.indices(a.shape)
x, y, z = np.meshgrid(np.linspace(0, 1, 4), np.linspace(0, 1, 3), [.5], indexing='xy')

axis = [0, 0, 1]
theta = 90
#M = rotation_matrix(axis, theta)
# for example, rotate around z-axis:
M = np.array([[ 0., -1.,  0.],
              [ 1.,  0.,  0.],
              [ 0.,  0.,  1.]])
# the following two lines are equivalent ways of multiplying M by each point as a vector:
# we want to sum over last axis of M, first of [x, y z]
xp, yp, zp = np.einsum('ij,jklm->iklm' M, [x, y, z])
xp, yp, zp = np.tensordot(M, [x, y, z], axes=(-1,0))

So now, the point that was originally at, say, i, j, k = 2, 2, 0 , went from: 所以现在,原先位于i, j, k = 2, 2, 0 2,2,0的点来自:

x[2, 2, 0], y[2, 2, 0], z[2, 2, 0]
# (0.666666, 1.0, 0)

to

xp[2, 2, 0], yp[2, 2, 0], zp[2, 2, 0]
#(-1.0, 0.666666, 0.0)

And still has the color: 并且仍然具有颜色:

a[2, 2, 0]
# 10

You can see all the coordinate with the same shape as a just by looking at xp, yp, zp . 通过查看xp, yp, zp您可以看到所有形状与a相同的坐标。

If your images are color, be careful that your 2D image is already 3D with an extra axis for color. 如果您的图像是彩色的,请注意您的2D图像已经是3D并带有额外的颜色轴。 Include this when using indices or meshgrid , and if you use einsum or tensordot . 在使用indicesmeshgrid ,以及在使用einsumtensordot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM