三维空间变换

三维空间变换


操作系统:ubuntu18.04
显卡:GTX1080ti
python版本:3.68
QQ群加入深度学习交流群 获取更多环境配置细节和学习资料 (147960154)


参考博客


这里先直接上代码
  • 复合矩阵(包括旋转,缩放,平移)
# 1. params
rx = torch.tensor(r[0] * np.pi / 180)
ry = torch.tensor(r[1] * np.pi / 180)
rz = torch.tensor(r[2] * np.pi / 180)
s_x,s_y,s_z = s[0],s[1],s[2]
tx, ty, tz = t[0], t[1], t[2]
cx, sx, cy, sy, cz, sz = torch.cos(rx), torch.sin(rx), torch.cos(ry), torch.sin(ry), torch.cos(rz), torch.sin(rz)
# 2. compound matrix
rst_xyz = torch.tensor(
	[[ s_x * (cy * cz),  -cx * cy * sz + sx * sy,       cx * sy + cy * sx * sz, 0],
	 [              sz,          s_y * (cx * cz),                     -cz * sx, 0],
	 [        -cz * sy,   cx * sy * sz + cy * sx, s_z*(cx * cy - sx * sy * sz), 0],
	 [              tx,                       ty,                           tz, 1]])
draw_hand2(uvd,Stn(uvd).rst([0,0,45],[1,1,1],[-0.2,-0.2,0]), [1,1,1])

result:
ad

完整类

class Stn():
    def __init__(self,uvd):
        self.uvd = torch.from_numpy(np.insert(uvd.numpy(),3,values=1, axis=1)) # [5,4]
    def t(self,tx, ty, tz):
        t_xyz = torch.tensor([[ 1., 0,  0, 0],
                              [ 0,  1,  0, 0],
                              [ 0,  0,  1, 0],
                              [tx, ty, tz, 1],
                             ])
        return self.uvd@t_xyz
    def s(self,sx, sy, sz):
        s_xyz = torch.tensor([[sx,  0.,  0, 0],
                              [ 0, sy,  0, 0],
                              [ 0,  0, sz, 0],
                              [ 0,  0,  0, 1],
                             ])
        return self.uvd@s_xyz
    def r(self,rx,ry,rz):
        rx = torch.tensor(rx * np.pi / 180)
        ry = torch.tensor(ry * np.pi / 180)
        rz = torch.tensor(rz * np.pi / 180)
        cx, sx, cy, sy, cz, sz = torch.cos(rx), torch.sin(rx),torch.cos(ry), torch.sin(ry), torch.cos(rz), torch.sin(rz)
        print(cz,sz)
        r_x = torch.tensor([[ 1,  0,  0, 0],
                            [ 0, cx, sx, 0],
                            [ 0,-sx, cx, 0],
                            [ 0,  0,  0, 1]])

        r_y = torch.tensor([[cy,  0,-sy, 0],
                            [ 0,  1,  0, 0],
                            [sy,  0, cy, 0],
                            [ 0,  0,  0, 1]])

        r_z = torch.tensor([[ cz, sz,  0, 0],
                            [-sz, cy,  0, 0],
                            [  0,  0,  1, 0],
                            [  0,  0,  0, 1]])
        r_xyz = torch.tensor([[ cy * cz,  -cx * cy * sz + sx * sy,  cx * sy + cy * sx * sz, 0],
                              [      sz,                  cx * cz,                -cz * sx, 0],
                              [-cz * sy,   cx * sy * sz + cy * sx,  cx * cy - sx * sy * sz, 0],
                              [       0,                        0,                       0, 1]])
        return self.uvd @ r_xyz
    def rst(self,r,s,t):
        rx = torch.tensor(r[0] * np.pi / 180)
        ry = torch.tensor(r[1] * np.pi / 180)
        rz = torch.tensor(r[2] * np.pi / 180)
        s_x,s_y,s_z = s[0],s[1],s[2]
        tx, ty, tz = t[0], t[1], t[2]
        cx, sx, cy, sy, cz, sz = torch.cos(rx), torch.sin(rx), torch.cos(ry), torch.sin(ry), torch.cos(rz), torch.sin(rz)
        rst_xyz = torch.tensor(
            [[ s_x * (cy * cz),  -cx * cy * sz + sx * sy,       cx * sy + cy * sx * sz, 0],
             [              sz,          s_y * (cx * cz),                     -cz * sx, 0],
             [        -cz * sy,   cx * sy * sz + cy * sx, s_z*(cx * cy - sx * sy * sz), 0],
             [              tx,                       ty,                           tz, 1]])

        return self.uvd @ rst_xyz
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值