Using OpenCV library on Raspberry Pi

This tutorial shows how to use the OpenCV 2 library in cross-compiled projects for Raspberry Pi 2. Before you begin, download the Raspberry Pi Jessie image and write it to the SD card. Then get a cross-toolchain compatible with the image from the website.

  1. Connect to your Raspberry Pi via SSH and run the “sudo apt-get install libopencv-dev” command to install OpenCV:01-inst
  2. Start Visual Studio and open the VisualGDB Linux Project Wizard: 02-linuxprj
  3. OpenCV comes with configuration files for CMake that simplify including the library from CMake projects. Hence we will use CMake to build our demo application. Select it in the wizard:03-newcmake
  4. Select “Build the project with a cross-compiler” and choose your Raspberry Pi board in the “deployment computer” field:04-cross
  5. Press “Finish” to create your project. Then open VisualGDB Project Properties, go to the CMake page and click “Synchronize sysroot”. Ensure that /opt/vc and /usr/share/OpenCV (not lowercase /usr/share/opencv) are selected:05-resync
  6. Open the CMakeLists.txt file in the newly created project and add the following line after add_executable():

    Also add ${OpenCV_LIBS} to target_libraries() statement: 06-findpkg
  7. Open VisualGDB Project Properties and set the CMAKE_PREFIX_PATH variable for the CMake command to point at <sysroot>\usr\share\OpenCV:07-prefix
  8. Replace the contents of the main source file with the following code from the find_contours sample in OpenCV:
  9. Upload any PNG image that will be used to check out contour detection to the Raspberry Pi.  In this tutorial we will use the Lena.png file from the OpenCV package:18-lena
  10. Specify the path to the uploaded image in the command-line arguments to your application via VisualGDB Project Properties:08-args
  11. Press F5 to start debugging the application. You will see two windows: one with the original image and another one with the contours:09-windowsYou can move the trackbar in the left window to change the threshold value for the edge detection algorithm.

To learn how to cross-compile OpenCV 3 for Raspberry Pi and step through its source code, follow this tutorial.

Read this tutorial for detailed instructions on using OpenCV with the Raspberry Pi camera module.