| Follow with RSS

Reading Data From Kinect libfreenect “record”

May 1st, 2012 by Ken Mankoff

The file format produced by the Kinect libfreenect record program is not well documented and suffers from endian complication issues on most computers. Below are four snippets of code that can be used to read the big-endian PGM (depth data) files produced by record. The PPM (RGB data) are a more popular image format and should be easier to work with.

// C
fp = fopen("file.pgm", "r");
while (getc(fp) != '\n'); // skip header line
uint16_t data[640*480];
fread(data, sizeof(uint16_t), 640*480, fp); // read the data
fclose(fp);

# Python
import numpy as np
infile = open('file.pgm','r')
header = next(infile)
infile.seek(len(header))
data = np.fromfile(infile, dtype=np.uint16).reshape((480, 640))

;; IDL
openr, lun, "file.pgm", /get_lun
header = {P5:BYTARR(2),width:BYTARR(4),height:BYTARR(4),maxV:BYTARR(7)}
readu, lun, header
data = intarr( string(header.width), string(header.height) )
readu, lun, data
free_lun, lun

% MATLAB
data = imread('file.pgm');
data = swapbytes(data);

If using my kinect_register program then reading a PGM is not absolutely necessary, as that program converts a single frame of depth data to an ASCII x,y,z file format. However, a single frame is noisy, and I suggest using the above PGM reader snippets to load several (or several hundred) PGMs and average all the good values of each pixel. Then, write out a new (averaged) PGM to be used as input to kinect_register. The snippets above can be used as templates for the writer function. The averaging will produce fractional numbers, but they will need to be rounded to whole numbers before writing the PGM.

If you have code to load 16-bit big-endian PGM files in another language, or a PGM writer function, feel free to share.

One Response to “Reading Data From Kinect libfreenect “record””

  1. KenMankoff » kinect_record Says:

    [...] a program called kinect_record. This program has output identical to record, but also shows the data as it captures [...]


Leave a Reply