Matplotlib之3D骨骼关节的动画显示

一 、显示效果

动画链接:3D骨骼关节的动画显示

二、3D骨骼关节点坐标的文件说明

skeleton_pos.txt: Skeleton position data
    
Skeleton data consists of 15 joints per person. Each row follows the following format.
    
    Frame#,PA(1),PA(2),...,PA(15),PB(1),PB(2),...,PB(15)

    PA(i)   => position of ith joint (x,y,z) for the subject A located at left
    PB(i)   => position of ith joint (x,y,z) for the subject B located at right

    x and y are normalized as [0,1] while z is normalized as [0,7.8125]

    Joint number -> Joint name
            1 -> HEAD
            2 -> NECK
            3 -> TORSO
            4 -> LEFT_SHOULDER
            5 -> LEFT_ELBOW
            6 -> LEFT_HAND
            7 -> RIGHT_SHOULDER
            8 -> RIGHT_ELBOW
            9 -> RIGHT_HAND
            10 -> LEFT_HIP
            11 -> LEFT_KNEE
            12 -> LEFT_FOOT
            13 -> RIGHT_HIP
            14 -> RIGHT_KNEE
            15 -> RIGHT_FOOT

The skeleton data was normalized. In order to extract the original position of the joints, the following equations are needed:
    
    original_X = 1280 - (normalized_X .* 2560);
    original_Y = 960 - (normalized_Y .* 1920);
    original_Z = normalized_Z .* 10000 ./ 7.8125;

三、源代码

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation

a = pd.read_csv("skeleton_pos.txt",header=None)
b = a.values[:,1:]
b = b.reshape((-1,30,3))
length = b.shape[0]
b[:,:,0] = 1280 - b[:,:,0] * 2560
b[:,:,1] = 960  - b[:,:,1] * 1920
b[:,:,2] = b[:,:,2] * 10000 / 7.8125        
Person1,Person2 = np.split(b,2,axis = 1)

print Person1[1]
print Person1.shape

fig = plt.figure()
ax = Axes3D(fig)
stick_defines = [
    (0, 1),
    (1, 2),
    (1, 3),
    (1, 6),
    (3, 4),
    (4, 5),
    (6, 7),
    (7, 8),
    (2, 9),
    (2, 12),
    (9, 10),
    (10, 11),
    (12, 13),
    (13, 14)
]
Person1_lines = [ax.plot(Person1[0,i,0], Person1[0,i,2], Person1[0,i,1], 'k-')[0] for i in stick_defines]
Person2_lines = [ax.plot(Person2[0,i,0], Person2[0,i,2], Person2[0,i,1], 'r-')[0] for i in stick_defines]

def init():
    for stick_line, i in zip(Person1_lines, stick_defines):
        stick_line._verts3d = Person1[0,i,0], Person1[0,i,2], Person1[0,i,1]
    for stick_line, i in zip(Person2_lines, stick_defines):
        stick_line._verts3d = Person2[0,i,0], Person2[0,i,2], Person2[0,i,1] 
    return Person1_lines+Person2_lines


def animate(j):
    
    for stick_line, i in zip(Person1_lines, stick_defines):
        stick_line._verts3d = Person1[j,i,0], Person1[j,i,2], Person1[j,i,1]
    for stick_line, i in zip(Person2_lines, stick_defines):
        stick_line._verts3d = Person2[j,i,0], Person2[j,i,2], Person2[j,i,1]   
#    stick_lines1 = [ax.plot(FirPerson[j,i,0], FirPerson[j,i,2], FirPerson[j,i,1], 'k-')[0] for i in stick_defines]
#    stick_lines2 = [ax.plot(SecPerson[j,i,0], SecPerson[j,i,2], SecPerson[j,i,1], 'r-')[0] for i in stick_defines]
    return Person1_lines+Person2_lines

# instantiate the animator.
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=length, interval=100, blit=True)

anim.save('result.mp4',fps = 20)
#plt.show()




  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值