Raspberry Pi Camera with OpenCV

October 19, 2013

I have OpenCV code using cvCreateCameraCapture and cvQueryFrame. Can I use it with the Raspberry Pi Camera? Well yes, read on to know how.

The Raspberry Pi Camera is widely available now, but there aren’t any open source video4linux (v4l) for it yet. Among other things, this makes it harder to use OpenCV with the camera.

When searching for a solution, I found Pierre Raufast’s blog, where he solves this problem. He took the existing userland libraries, which provide the source for the raspistill and raspivid tools. He modified them to produce OpenCV images instead.

Excellent news! So I now I can use this in my own OpenCV projects, and bypass the v4l drivers issue entirely. But wait, my code already calls cvCreateCameraCapture, cvQueryFrame in its own main loop, how do I do this?

I took Pierre Raufast’s modified camcv_vid0.c file and converted it into a library.

Instead of calling cvCreateCameraCapture and cvQueryFrame, you call the new raspi cam equivalents: raspiCamCvCreateCameraCapture and raspiCamCvQueryFrame. Instead of using a CvCapture, you use a RaspiCamCvCapture.

It’s a plain and simple search and replace operation in your code.

My source is here, it creates a library called libraspicamcv.a, you need to link with it and  #include “RaspiCamCV.h”

A more detailed README on how to compile and use it is the code repository itself.

This is a work in progress, I’ve done limited testing on the library. Contact me if you find bugs.

There is a raspiCamCvSetCaptureProperty method as well, but it doesn’t do anything yet. I plan to update it in the future,  to change parameters like: state->width, state->height, and state->graymode

For now, they are hardcoded in RaspiCamCV.c, so you need to change the code, and recompile the library to change resolution, or switch to grayscale mode (which is much faster).