本文介绍了如何使用scipy.ndimage.interpolate在3d中随机旋转一个numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3个维度上制作了一个正方形,本质上是这个版本的3d版本:

I made a square in 3 dimensions that is essentially a 3d version of this:

        [[0., 0., 0., 0., 1., 1., 1., 0.],
         [0., 0., 0., 0., 1., 1., 1., 0.],
         [0., 0., 0., 0., 1., 1., 1., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.]]

您可以看到一个 3x3 正方形的正方形.在3d图中,它给出了以下信息:

You can see a 3x3 square of ones. In 3d, in a plot, it gives this:

import numpy as np
import matplotlib.pyplot as plt

square = np.ones((8, 8, 8))
x, y, z = np.where(square ==1)

fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, alpha=.8, s=100, ec='k', color='#2FAA75')
ax.set_xlim(-5, 10), ax.set_ylim(-5, 10), ax.set_zlim(-5, 10)
plt.show()

我想要的是这个直角正方形,可以旋转各种角度,不仅旋转90度.

What I want is this straight square to rotate of various angles, not only 90 degrees.

我知道 scipy.spatial. transform.Rotation 可以做到.不幸的是,我不知道如何实现它. 预期结果:假设立方体相对于xz轴旋转了45度.

I know that scipy.spatial.transform.Rotation can do that. Unfortunately, I don't know how to implement it. Expected results: imagine the cube being rotated 45 degrees with respects to the x and z axes (for example).

推荐答案

# ...
coords = np.where(square == 1)
coords = np.transpose(coords)          # get coordinates into a proper shape

rot = Rotation.from_euler('xz', [45, 45], degrees=True)  # create a rotation
coords = rot.apply(coords)             # apply the rotation
coords = np.transpose(coords)          # get coordinates back to the matplotlib shape

# ...

ax.scatter(*coords, alpha=.8, s=100, ec='k', color='#2FAA75')

将旋转替换为

rot = Rotation.random()

这篇关于如何使用scipy.ndimage.interpolate在3d中随机旋转一个numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:47