11 Feb 2013, 19:52

Playing with Kinect and ZeroMQ Part 2

Share

In the previous post I briefly outlined writing a simple tool to pull data off of a kinect, and pass the buffers into zeromq frames for transmission.  In this post I’m going to go over the other side of the equation - a simple receiver that subscribes to the data stream and displays it. I’ll be using opencv for image creation and display:

#include stdio.h;
#include stdlib.h;
#include opencv/cv.h;
#include opencv/highgui.h;
#include czmq.h;

So, first I create my zeromq SUB socket, subscribe (in this instance I’m not using a topic so I subscribe to “”), and connect to the broadcaster:

zctx_t *ctx = zctx_new ();
void *receive = zsocket_new ( ctx, ZMQ_SUB );
zsocket_set_subscribe ( receive, "" );
int rc = zsocket_connect ( receive, "tcp://192.168.1.113:9999" );

Next, I’ll create an image header using opencv’s cvCreateImageHeader call:

IplImage *image = cvCreateImageHeader ( cvSize (640, 480), 8, 3 );

Within my while loop, I will first do a blocking zmsg_recv call. I could instead use the zmq poller, or czmq’s zloop, but I kept it simple for this example. After I receive the message, I think pop off the rgb and depth frames, then copy the rgb frame to a buffer:

/* receive message */
zmsg_t *msg = zmg_recv ( receive );

/* pop frames and then copy rgb data */
zframe_t *depth_frame = zmsg_pop ( msg );
zframe_t *rgb_frame = zmsg_pop ( msg );

char *rgb_buffer = zframe_strdup ( rgb_frame );

Note that I could use zero copy techniques instead of duplicating the frame, but once again for this example I kept things simple.

Next is the code do display the image:

cvSetData ( image, rgb_buffer, 640*3 );
cvCvtColor ( image, image, CV_RGB2BGR );
cvShowImage ( "RGB", image );

Then all that’s left is cleanup:

zframe_destroy ( &depth_frame );
zframe_destroy ( &rgb_frame );
zmsg_destroy ( &msg );

Here’s a little video showing 6 receivers running, all pulling from the same broadcaster. Oh, the music in the background is a track that happened to be up on spotify when I shot the video - it’s Marley Carroll’s “Meaning Leaving”, and you might want to check him out, he’s fantastic!

Have Fun!