Raspberry Pi LogoI’ve been doing a lot of work with the Raspberry Pi lately with what little free time I’ve had and one of the things I’ve been working with the most is computer vision. Since computer vision is my passion and the Raspberry Pi has so much potential, I wanted to push it to its limits by seeing if I can run some of the same computer vision apps on my Raspberry Pi as I do on my full size laptop. Most of the computer vision applications I’ve worked on recently are all written in C++ and consist of proprietary code. However, I still love using other computer vision libraries and OpenCV is still at the top of that list. Even though I sometimes write OpenCV applications in C++ or Java which I can also do on the Raspberry Pi, I really like the fact that the Raspberry Pi is configured with Python right out-of-the-box. So, I want to take a few minutes to show all of you how to configure your RPi to work with OpenCV using Python. Let’s begin.

Before you go any further in this article, make sure your RPi has internet access as it will need to go out to the web and fetch the libraries we will be working with. Lucky for us, OpenCV has already been ported to Raspbian. So, all we have to do is go out and fetch it. To do that, open a terminal window and run the following command.

sudo apt-get install libcv-dev libopencv-dev libcv2.3 opencv-doc

This command will fetch the libraries needed for running OpenCV on the Raspberry Pi. Next, you will need to load the OpenCV modules for Python. To do that, you can run the follow command again from your terminal.

sudo apt-get install python-opencv

That’s it. You now have all of the required components for running OpenCV on your Raspberry Pi. The only thing you need now is some code. So, I thought I would share a very simple example with you that I wrote for you to test with. This code will simply display live video from a typical USB webcam. I’ll save the cooler stuff for future articles.

import cv

cap = cv.CaptureFromCAM(0)
cv.NamedWindow("OpenCV Test", 1)

while True:
    img = cv.QueryFrame(cap)
    cv.ShowImage("OpenCV Test", img)
    if cv.WaitKey(10) == 27:

Here is a screenshot of this example in action on my Raspberry Pi.

OpenCV on Raspberry Pi with Python

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Tagged with:  

Leave a Reply