三维数组创建模型并对模型进行旋转

问题:我使用三维数组创建三维模型,在使用rotate函数时,用了三组rotate函数,但是只有一组发挥了效果,请问这个是为什么呢?

我使用这个库来调用rotate函数


from scipy.ndimage import rotate

这里是部分代码,先这样的rotate函数我用了三次,但是只有一次是有效果的,请问如何解决呢?

cuboid = np.zeros ([20,30,50])
space = np.full ([100,100,100],255)
cuboid_x = random.randint(0,80)
cuboid_y = random.randint(0,70)
cuboid_z = random.randint(0,50)
space[cuboid_x: cuboid_x+20,cuboid_y: cuboid_y+30,cuboid_z: cuboid_z+50] = cuboid

angle_cuboid_x = random.randint(0, 359)
angle_cuboid_y = random.randint(0, 359)
angle_cuboid_z = random.randint(0, 359)

rotated_cuboid = rotate(cuboid, angle_cuboid_x, axes=(0, 1), reshape=False)
rotated_cuboid = rotate(rotated_cuboid, angle_cuboid_y, axes=(1, 2), reshape=False)
rotated_cuboid = rotate(rotated_cuboid, angle_cuboid_z, axes=(0, 2), reshape=False)

space[cuboid_x:cuboid_x+20, cuboid_y:cuboid_y+30 ,cuboid_z:cuboid_z+50] = rotated_cuboid

您看看:

import numpy as np
 
# 旋转,axis为旋转轴,0,1,2分别代表x,y,z轴
# theta为旋转角度,单位已改为度,非弧度
# center为旋转中心,其为一维np数组[x,y,z],默认值为图像中心点
def rotation(data, axis, theta, c = np.array([])):# c代表旋转点
    theta = -np.pi * theta / 180
    if c.size == 0:
        c = np.array([np.floor((data.shape[0]-1)/2), np.floor((data.shape[1]-1)/2), np.floor((data.shape[1]-1)/2)])
    
    s = data.shape
    mean = np.mean(data)
    # new_data = np.ones(s) * mean # 补均值
    new_data = np.zeros(s) # 补零
    
    # 绕x轴旋转
    if axis == 0:
        for i in range(0, s[0]):
            for j in range(0, s[1]):
                for k in range(0, s[2]):
                    x = i
                    y = (j-c[1])*np.cos(theta)-(k-c[2])*np.sin(theta)+c[1]
                    if(y < 0 or y > s[1]-1):
                        continue
                    z = (j-c[1])*np.sin(theta)+(k-c[2])*np.cos(theta)+c[2]
                    if(z < 0 or z > s[2]-1):
                        continue
                    y1 = np.floor(y).astype(int)
                    y2 = np.ceil(y).astype(int)
                    z1 = np.floor(z).astype(int)
                    z2 = np.ceil(z).astype(int)
                    dy = y - y1
                    dz = z - z1
                    new_data[i,j,k] = (data[x,y1,z1]*(1-dy)+data[x,y2,z1]*dy)*(1-dz) + (data[x,y1,z2]*(1-dy)+data[x,y2,z2]*dy)*dz
      
    # 绕y轴旋转              
    elif axis == 1:
        for i in range(0, s[0]):
            for j in range(0, s[1]):
                for k in range(0, s[2]):
                    y = j
                    x = (i-c[0])*np.cos(theta)-(k-c[2])*np.sin(theta)+c[0]
                    if(x < 0 or x > s[0]-1):
                        continue
                    z = (i-c[0])*np.sin(theta)+(k-c[2])*np.cos(theta)+c[2]
                    if(z < 0 or z > s[2]-1):
                        continue
                    x1 = np.floor(x).astype(int)
                    x2 = np.ceil(x).astype(int)
                    z1 = np.floor(z).astype(int)
                    z2 = np.ceil(z).astype(int)
                    dx = x - x1
                    dz = z - z1
                    new_data[i,j,k] = (data[x1,y,z1]*(1-dx)+data[x2,y,z1]*dx)*(1-dz) + (data[x1,y,z2]*(1-dx)+data[x2,y,z2]*dx)*dz
 
    # 绕z轴旋转
    else:
        for i in range(0, s[0]):
            for j in range(0, s[1]):
                for k in range(0, s[2]):
                    z = k
                    x = (i-c[0])*np.cos(theta)-(j-c[1])*np.sin(theta)+c[0]
                    if(x < 0 or x > s[0]-1):
                        continue
                    y = (i-c[0])*np.sin(theta)+(j-c[1])*np.cos(theta)+c[1]
                    if(y < 0 or y > s[1]-1):
                        continue
                    x1 = np.floor(x).astype(int)
                    x2 = np.ceil(x).astype(int)
                    y1 = np.floor(y).astype(int)
                    y2 = np.ceil(y).astype(int)
                    dx = x - x1
                    dy = y - y1
                    new_data[i,j,k] = (data[x1,y1,z]*(1-dx)+data[x2,y1,z]*dx)*(1-dy) + (data[x1,y2,z]*(1-dx)+data[x2,y2,z]*dx)*dy
                    
    return new_data
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^