Nikhil Kumar bio photo

Nikhil Kumar

A stylish blog on my code

Gmail LinkedIn Github Résumé
Changelog:

I am currently doing research on the differences between Barefoot running and Shod running(running with shoes). I am working with software known as the AnyBody Modeling System using the Gait lower Extremity model. This allows me to input motion captured data, which is givin in the form of a .c3d file. However, I ran into a problem where I wanted to modify the .c3d file but I could not find anyt decent software to allow me to do this. I was able to find btk a python package that allows me to modify .c3d files.

C3d files

What are c3d files:

According to the c3d main page:

The C3D format is a public domain, binary file format that has been used in Biomechanics, Animation and Gait Analysis laboratories to record synchronized 3D and analog data since the mid 1980's. It is supported by all 3D major Motion Capture System manufacturers, as well as other companies in the Biomechanics, Motion Capture and Animation Industries.

File Structure

The structure of the C3d file is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*.c3d/
	├── Header
	|    ├── File type
	|    ├── Data type
	|    ├── 3D Points
	|    ├── Analog Channels
	|    ├── First Frame
	|    ├── Last Frame
	|    ├── Start Record
	|    ├── AV ratio
	|    ├── Video rate
	|    └── Max interpolation Gap
	├── Events
	├── Groups
	|    |	├── Point
	|    |	├── ...
	|    |	├──Labels
	|    |	└── ...
	|    |
	|    ├── Analog
	|    ├── Seq
	|    ├── Manufacturer
	|    └── Force Platform
	├── Analog data
	└── Point data

Instalation

What is Btk?

According to the BTk site on google code:

BTK is an open-source and cross-platform library for biomechanical analysis. BTK read and write acquisition files and can modify them. All these operations can be done by the use of the C++ API or by the wrappers included (Matlab, Octave, and Python).

Python packages needed

1
2
3
numpy
Matplotlib
BTK

To install python2 packages you need to run pip. For example to install numpy, you need to run:

1
pip install numpy

Caution: BTK is the exception here. I was not able to get btk working through pip. BTK also seems to be architectural dependent as it was not installing in my ARCH distribution.

I was able to get it working on Fedora 19 by installing the python wrapper package from here.

Then run:

1
yum localinstall packagename.rpm 

Using b-tk to modify your file

You can import btk in python by specify your system path to the package or by just using import depending on your installation.

1
2
3
import sys
sys.path.append("path to your btk python package")
import btk

You then need to create a btk reader.

1
reader = btk.btkAcquisitionFileReader()

Then you need to open the file and create a btk aquisition object.

1
2
3
4
reader.SetFilename("sample.c3d") # open the c3d file
reader.Update()
acq = reader.GetOutput() # creates a btk aquisition object
 

You can then itterate through the Marker labels and Analog channels by doing:

1
2
3
4
5
6
print('Marker labels:')
for i in range(0, acq.GetPoints().GetItemNumber()):
    print(acq.GetPoint(i).GetLabel(), end='  ')   
print('\n\nAnalog channels:')
for i in range(0, acq.GetAnalogs().GetItemNumber()):
    print(acq.GetAnalog(i).GetLabel(), end='  ') 

You can then set the value in a new c3d file clone or the original c3d file useing the SetValue method. Here is some code I did to cut off the force at some value.

1
2
3
4
5
6
7
8
9
10
11
12
#modifies the values in the z axis 1st force plate

ana = clone.GetAnalog("Fz1")
valz1 = ana.GetValues()
scalez1 = ana.GetScale()
x = 0
y = float(sys.argv[2])
for forcez1 in np.nditer(valz1):
      if forcez1/scalez1 > y:
			ana.SetValue(x,y * scalez1)

      x = x + 1