element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci Fi Your Pi
  • Challenges & Projects
  • Design Challenges
  • Sci Fi Your Pi
  • More
  • Cancel
Sci Fi Your Pi
Blog [Project VIRUS][Week 3] Getting started with OpenCV and RPi Camera
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ipv1
  • Date Created: 16 May 2015 9:14 AM Date Created
  • Views 1533 views
  • Likes 3 likes
  • Comments 7 comments
  • project_virus
  • ip_iot
  • sci_fi_your_pi
Related
Recommended

[Project VIRUS][Week 3] Getting started with OpenCV and RPi Camera

ipv1
ipv1
16 May 2015

Prelude

My initial design was to build a set of robots around the raspberry Pi and use other components in the kit to build the control systems. Since I am yet to receive the kit, I have moved to researching the Image capture and recognition part of the project.

 

Introduction

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision, developed by Intel Russia research center in Nizhny Novgorod, and now supported by Willow Garage and Itseez. It is free for use under the open-source BSD license. The library is cross-platform. It focuses mainly on real-time image processing. If the library finds Intel's Integrated Performance Primitives on the system, it will use these proprietary optimized routines to accelerate itself.

 

OpenCV has gained a lot of attention for the last few years with the advent of the RPi and other single board computers, it is now possible to have simpler Image Manipulation tasks executed on DIY systems. One module in my scifi design is to use gesture recognition and for that I intend to use an RPI 2 dedicated to Image processing and sensing.

 

I am going to start with installing OpenCV on the RPI and then using it to simply acquiring an image. I will be using C++ for more advanced stuff since I am more comfortable with it as compared to Python but I will do the initial stuff using Python 2.7.

 

Lets get started...

 

Installing OpenCV

Now there are a LOT of guides out there for this BUT I am giving you the details of "my way” of doing the install. If I make a mistake and you have a better way, please leave a comment and I will update it here.

 

The first thing is to download the OpenCV library for Raspberry Pi and to do that we go to http://opencv.org/downloads.html

 

I chose the last stable version which was 2.4.10 and I think that should work for the most part.

 

We also need to install some dependencies run the following commands

 

sudo apt-get -y install build-essential cmake cmake-curses-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev

 

and then

 

sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev

 

Next unzip it using

 

unzip opencv-2.4.10.zip
cd opencv-2.4.10
mkdir release
cd release
ccmake ../

 

press ‘c’ to configure and toggle the options you want. I did the following.

 

ANT_EXECUTABLE                   ANT_EXECUTABLE-NOTFOUND                                                                                              
BUILD_DOCS                       ON                                                                                                                   
BUILD_EXAMPLES                   ON                                                                                                                   
BUILD_JASPER                     ON                                                                                                                   
BUILD_JPEG                       ON                                                                                                                   
BUILD_OPENEXR                    ON                                                                                                                   
BUILD_PACKAGE                    ON                                                                                                                   
BUILD_PERF_TESTS                 ON                                                                                                                   
BUILD_PNG                        ON                                                                                                                   
BUILD_SHARED_LIBS                ON                                                                                                                   
BUILD_TBB                        OFF                                                                                                                  
BUILD_TESTS                      ON                                                                                                                   
BUILD_TIFF                       ON                                                                                                                   
BUILD_WITH_DEBUG_INFO            ON                                                                                                                   
BUILD_ZLIB                       ON                                                                                                                   
BUILD_opencv_apps                ON                                                                                                                   
BUILD_opencv_calib3d             ON                                                                                                                   
BUILD_opencv_contrib             ON                                                                                                                   
BUILD_opencv_core                ON                                                                                                                   
BUILD_opencv_features2d          ON                                                                                                                   
BUILD_opencv_flann               ON                                                                                                                   
BUILD_opencv_gpu                 ON                                                                                                                   
BUILD_opencv_highgui             ON                                                                                                                   
BUILD_opencv_imgproc             ON                                                                                                                   
BUILD_opencv_legacy              ON                                                                                                                   
BUILD_opencv_ml                  ON                                                                                                                   
BUILD_opencv_nonfree             ON                                                                                                                   
BUILD_opencv_objdetect           ON                                                                                                                   
BUILD_opencv_ocl                 ON                                                                                                                   
BUILD_opencv_photo               ON                                                                                                                   
BUILD_opencv_python              ON                                                                                                                   
BUILD_opencv_stitching           ON                                                                                                                   
BUILD_opencv_superres            ON                                                                                                                   
BUILD_opencv_ts                  ON                                                                                                                   
BUILD_opencv_video               ON                                                                                                                   
BUILD_opencv_videostab           ON                                                                                                                   
BUILD_opencv_world               OFF                                                                                                                  
CLAMDBLAS_INCLUDE_DIR            CLAMDBLAS_INCLUDE_DIR-NOTFOUND                                                                                       
CLAMDBLAS_ROOT_DIR               CLAMDBLAS_ROOT_DIR-NOTFOUND                                                                                          
CLAMDFFT_INCLUDE_DIR             CLAMDFFT_INCLUDE_DIR-NOTFOUND                                                                                        
CLAMDFFT_ROOT_DIR                CLAMDFFT_ROOT_DIR-NOTFOUND                                                                                           
CMAKE_BUILD_TYPE                 Release                                                                                                                
CMAKE_CONFIGURATION_TYPES        Debug;Release                                                                                                              
CMAKE_INSTALL_PREFIX             /usr/local

CMAKE_VERBOSE                    OFF                                                                                                                  
CUDA_BUILD_CUBIN                 OFF                                                                                                                  
CUDA_BUILD_EMULATION             OFF                                                                                                                  
CUDA_HOST_COMPILER               /usr/bin/gcc                                                                                                         
CUDA_SDK_ROOT_DIR                CUDA_SDK_ROOT_DIR-NOTFOUND                                                                                           
CUDA_SEPARABLE_COMPILATION       OFF                                                                                                                  
CUDA_TOOLKIT_ROOT_DIR            CUDA_TOOLKIT_ROOT_DIR-NOTFOUND                                                                                       
CUDA_VERBOSE_BUILD               OFF                                                                                                                  
EIGEN_INCLUDE_PATH               /usr/include/eigen3                                                                                                  
ENABLE_NEON                      OFF                                                                                                                  
ENABLE_NOISY_WARNINGS            OFF                                                                                                                  
ENABLE_OMIT_FRAME_POINTER        ON                                                                                                                   
ENABLE_PRECOMPILED_HEADERS       ON                                                                                                                   
ENABLE_PROFILING                 OFF                                                                                                                  
ENABLE_SOLUTION_FOLDERS          OFF                                                                                                                  
ENABLE_VFPV3                     OFF                                                                                                                  
EXECUTABLE_OUTPUT_PATH           /home/pi/opencv-2.4.8/release/bin                                                                             
GIGEAPI_INCLUDE_PATH             GIGEAPI_INCLUDE_PATH-NOTFOUND                                                                                        
GIGEAPI_LIBRARIES                GIGEAPI_LIBRARIES-NOTFOUND                                                                                           
INSTALL_CREATE_DISTRIB           OFF                                                                                                                  
INSTALL_C_EXAMPLES               OFF                                                                                                                  
INSTALL_PYTHON_EXAMPLES          OFF                                                                                                                  
INSTALL_TO_MANGLED_PATHS         OFF                                                                                                                  
OPENCV_CONFIG_FILE_INCLUDE_DIR   /home/pi/opencv/opencv-2.4.8/release                                                                                 
OPENCV_EXTRA_MODULES_PATH                                                                                                                             
OPENCV_WARNINGS_ARE_ERRORS       OFF                                                                                                                  
OPENEXR_INCLUDE_PATH             OPENEXR_INCLUDE_PATH-NOTFOUND                                                                                        
PVAPI_INCLUDE_PATH               PVAPI_INCLUDE_PATH-NOTFOUND                                                                                          
PYTHON_NUMPY_INCLUDE_DIR         /usr/lib/pymodules/python2.7/numpy/core/include                                                                      
PYTHON_PACKAGES_PATH             lib/python2.7/dist-packages                                                                                          
SPHINX_BUILD                     SPHINX_BUILD-NOTFOUND                                                                                                
WITH_1394                        OFF                                                                                                                  
WITH_CUBLAS                      OFF                                                                                                                  
WITH_CUDA                        OFF                                                                                                                  
WITH_CUFFT                       OFF                                                                                                                  
WITH_EIGEN                       ON                                                                                                                   
WITH_FFMPEG                      ON                                                                                                                   
WITH_GIGEAPI                     OFF                                                                                                                  
WITH_GSTREAMER                   ON                                                                                                                   
WITH_GTK                         ON                                                                                                                   
WITH_JASPER                      ON                                                                                                                   
WITH_JPEG                        ON                                                                                                                   
WITH_LIBV4L                      ON                                                                                                                   
WITH_NVCUVID                     OFF

WITH_OPENCL                      ON                                                                                                                   
WITH_OPENCLAMDBLAS               ON                                                                                                                   
WITH_OPENCLAMDFFT                ON                                                                                                                   
WITH_OPENEXR                     ON                                                                                                                   
WITH_OPENGL                      ON                                                                                                                   
WITH_OPENMP                      OFF                                                                                                                  
WITH_OPENNI                      OFF                                                                                                                  
WITH_PNG                         ON                                                                                                                   
WITH_PVAPI                       ON                                                                                                                   
WITH_QT                          OFF                                                                                                                  
WITH_TBB                         OFF                                                                                                                  
WITH_TIFF                        ON                                                                                                                   
WITH_UNICAP                      OFF                                                                                                                  
WITH_V4L                         ON                                                                                                                   
WITH_XIMEA                       OFF                                                                                                                  


WITH_XINE                        OFF

 

 

For the most part I just left things to the default except for enabling stuff with Jpeg, Png and TBB.

 

press c again to configure and then g to generate the make file.

 

This should drop you back to the command prompt.

 

Next build with

 

make

 

I did this whole thing on a RPi 2 with a class 10 card. Yes it makes a difference since class 10 cards have faster access rates.

 

It took around 3.5 hours and if you happen to do something wrong, do a

 

make clean
make

 

Lastly do a

 

make install

 

and then reboot. This should have you up and running.

 

Testing everything

 

I am assuming that you are using the RPi Camera and that you have enabled the RPi Camera using raspi-config. If not, then please refer- (https://www.raspberrypi.org/help/camera-module-setup/)

You need to either attach a monitor to the RPi or access it via vnc as we did in the last post. Start the windows manager by typing

 

startx

 

You should now have the windows system running and should be able to see the desktop. Start a new terminal and create a new folder by typing

 

mkdir opencv_tests

 

Next create a new file by typing

 

cd opencv_tests
leafpad test1.py

 

I am using leafpad as its just simple when using the windows env.

 

Type the following lines

 

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)

# allow the camera to warmup
time.sleep(0.1)

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array

# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)

 

Save the file and close LeaftPad which will drop you back to the LX Terminal

Now just type

 

python test1.py

 

And that should display an image in a new windows. Press any key and it will close the windows and return you to the command prompt.

 

Conclusion

This was a lot of fun and I think to start with you should use python even if you have used OpenCV in C in the past. Its much simpler and with the power of the RPI2 the lag is almost gone. I will be implementing some gesture recognition while I wait for the Kit to be delivered.

See you next time...

  • Sign in to reply

Top Comments

  • fvan
    fvan over 10 years ago +3
    Learned something new following this tutorial: http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ You can use " make - j4" to make use of all 4 cores, speeding up the process…
  • ipv1
    ipv1 over 10 years ago +2
    DAB sir, Thank you. And the opencv works much better on the Pi 2 as opposed to freeky slow performance on the older RPi. I highly recommend giving it a whirl and with python its actually quite easy. I…
  • DAB
    DAB over 10 years ago +1
    Great post. I have long considered using opencv, but it is nice to know it also works with the RPi. Thanks DAB
Parents
  • jlopez2022
    jlopez2022 over 10 years ago

    Thank you for the tutorial

    That installation can be used to link in c++ programs?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ipv1
    ipv1 over 10 years ago in reply to jlopez2022

    The installation is required if you want to work in python or c++. It sets up all the libraries etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • ipv1
    ipv1 over 10 years ago in reply to jlopez2022

    The installation is required if you want to work in python or c++. It sets up all the libraries etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube