Skip to content
Frank Bauernöppel edited this page Oct 12, 2018 · 58 revisions

OpenCV == Open Computer Vision. A great open-source and multi-platform computer vision, image processing,... framework containing hundreds of state-of-the-art algorithms in its field.

Tested with version 3.4.1 see the docs http://docs.opencv.org/3.4.1/

including OpenCV in your image

Open a bitbake shell on your build host and build

frank@FrankBuntuSSD:~/raspi/build$ bitbake rpi-avg-image

You may also build different images and include the OpenCV part from rpi-avg-image.bb into your recipe.

Note that we focus on framebuffer (non-X11) builds here. It is also possible to build OpenCV with X11 as a graphical backend. This might be preferable for debugging because you can open several windows for visual debugging easily. Caution: The OpenCV default imshow+waitKey window output will slow down your algorithm! There are alternatives!

Prepare microSD card, boot your RasPi, and login.

The image contains the opencv-dev packages so you can develop small OpenCV apps on the RasPi. For larger projects and/or automated build processes you might want to cross-compile your apps and include them in the image.

Strictly speaking, the graphical backend X11 is not required and could be omitted in production images. But, having graphical output is very convenient for visual debugging. If network login via ssh is used, X11 windows can be redirected to the build host (use ssh -X switch).

Hello OpenCV in C++

This is a tiny example which does not need any extra hardware. Its only purpose is to demonstrate correct installation and a workflow for building OpenCV apps.

First, create a new folder hello_opencv in your home folder /home/root.

Use vi or another editor of your choice and create a new file hello_opencv.cpp. Remember that you could also edit the file on the build host and copy it by using scp or the X11 clipboard to the RasPi.

hello_opencv.cpp content:

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char *argv[])
{
  std::cout << "Hello OpenCV" << std::endl;
  cv::Mat image = cv::Mat::zeros(480,640,CV_8UC3);
  cv::circle( image, cv::Point(240,320), 100.0, cv::Scalar(0,255,255) );
  bool ok = cv::imwrite("hello.jpg",image);
  if(ok) {
    std::cout << "hello.jpg written" << std::endl;
  } else {
    std::cerr << "failed to write hello.jpg" << std::endl;
  }
  return 0;
}

Edit a Makefile in the very same folder with the following content (the indented lines must be indented with a tabulator TAB):

CXXFLAGS += $(shell pkg-config --cflags opencv)
LDFLAGS += $(shell pkg-config --libs opencv)

PROG = hello_opencv
OBJS = $(PROG).o

.PHONY: all clean
$(PROG): $(OBJS)
	$(CXX) -o $(PROG) $(OBJS) $(LDFLAGS)

%.o: %.cpp
	$(CXX) -c $(CXXFLAGS) $<

all: $(PROG)

clean:
	rm -f $(OBJS) $(PROG)

The command pkg-config --libs opencv lists all libs which the opencv-dev package provides. Likewise pkg-config --cflags opencv gives you the necessary compiler flags (include path).

Build the app using

root@raspberrypi3:~/hello_opencv# make
g++ -c -I/usr/include/opencv hello_opencv.cpp
g++ -o hello_opencv hello_opencv.o -L/usr/share/OpenCV/3rdparty/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core 

and execute it:

root@raspberrypi3:~/hello_opencv# ./hello_opencv
hello.jpg written

Check that you have created a JPEG file. You may copy and watch it on the build host or make a simple test on the RasPi:

root@raspberrypi3:~/hello_opencv# file hello.jpg 
hello.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 640x480, frames 3

using cmake

OpenCV goes well with cmake instead of make.

Hello OpenCV in Python

Edit a new file hello_opencv.py with the following content:

import cv2
import numpy as np

img = np.zeros((480,640,3), np.uint8)
cv2.circle( img, (240,320), 100, (0,255,255) )
cv2.imwrite( 'hello_python.jpg', img )

Run the python interpreter on it and compare the result to the C++ generated JPEG. Both should be identical (diff stays quiet):

root@raspberrypi3:~/hello_opencv# python hello_opencv.py
root@raspberrypi3:~/hello_opencv# diff hello.jpg hello_python.jpg

using imshow for live video output

This requires X11 as graphical backend.

For testing, add two lines in the Hello OpenCV C++ example after drawing the circle and before imwrite:

cv::imshow( "image", image );
cv::waitKey();

Rebuild (make) and rerun. When you login using "ssh -X ...", a window appears on the build host showing the black image with the yellow circle. After issuing export DISPLAY=:0 the local display attached to the RasPi will be used (tested with HDMI). Note that waitKey() waits for a keypress on the keyboard belonging to the window. waitKey also passes control to the windowing system, so it is necessary to call waitKey() in order to refresh the display and see something.

In Python use:

cv2.imshow("img",img)
cv2.waitKey()

using a camera for live video input

using RasPi Cam

This requires X11 for the output (imshow). Tested with RasPi Cam V2 on a RasPi3. See also CSI Camera. First, the video for Linux driver must be loaded:

root@raspberrypi3:~/hello_opencv# modprobe bcm2835-v4l2

Change the C++ example like this:

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char *argv[])
{
  cv::Mat image;
  cv::VideoCapture videoCapture(0);
  if(!videoCapture.isOpened()) {
    std::cerr << "failed to open video capture" << std::endl;
    return -1;
  }
  while(1) {
    videoCapture >> image;
    cv::imshow( "image", image );
    int c = cv::waitKey(1);
    if(c=='x') {
      break;
    }
  }
  return 0;
}

The above Makefile can be reused.

Rebuild (make) and rerun. A live video image shall appear in the window.

If you want to see a more fancy video, try inserting a video filter (you need to define a second image, because the videoCapture output must be treated read-only.

cv::Canny( image, image2, 50, 3*50, 3 );

using an USB camera

Tested with Microsoft Corp. LifeCam Studio on a RasPi3. See also USB UVC webcam.

Make sure that the CSI camera module is not loaded (see video for Linux) and repeat execution of the hello example. This time live video comes via USB. Your mileage may vary, because there are many different makes of USB webcams.

Note: If more that one video for linux capture device is available (ls /dev/video*), you may specify the camera index in the VideoCapture constructor, e.g. for stereo vision.

using a network camera

Tested with AXIS M1013 in the same LAN. Used a webbrowser and VLC on the build host to setup the camera and check the stream.

Just change the videoCapture c'tor to the (optionally password protected) RTSP stream form the camera:

cv::VideoCapture videoCapture("rtsp://user:password@192.168.2.114/axis-media/media.amp?videocodec=h264&resolution=640x480");

The RTSP URI varies from camera to camera, read the camera manual.

using gstreamer for video input and output

gstreamer may be used for video input (cv::VideoCapture) and output (cv::VideoWriter). This is usually faster than using cv::imshow + cv::waitKey and also necessary when the Raspi image was built without windowing (X11) support.

hello_opencv.cpp:

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char *argv[])
{
  cv::Mat image;
  cv::VideoCapture videoCapture(0);
  if(!videoCapture.isOpened()) {
    std::cerr << "failed to open video capture" << std::endl;
    return -1;
  }

  cv::VideoWriter videoWriter("appsrc ! glimagesink", cv::VideoWriter::fourcc('Y', 'U', 'Y', 'V'), 30.0,   cv::Size(640, 480), true);
  if(!videoWriter.isOpened()) {
    std::cerr << "failed to open video writer" << std::endl;
    return -1;
  }

  while(1) {
    videoCapture >> image;
    videoWriter << image;
  }
  return 0;
}

Notes:

  • in OpenCV, captured video images must be treated read-only
  • videoWriter c'tor make a guess for some parameters, this is not production-ready code
  • videoWriter expected an image with 3 8U channels (BGR), you might need to convert
  • test gstreamer pipeline first form the command line. You might H.264 encode the stream and send it out via network

using framebuffer for video output

An OpenCV cv::Mat object can be constructed which wraps the Linux framebuffer. This can be a quite efficient way for drawing video images.

OpenCV samples

By default, many pre-compiled C++ and Python samples are installed /usr/share/OpenCV/samples/. See the OpenCV docs and the sources for using these examples.

tips and tricks

  • Check the OpenCV configuration which was used for bitbake. The log is in ${BUILDDIR}/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/*/temp/log.do_configure. It looks like this (excerpt):
-- General configuration for OpenCV 3.3.0-dev =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/contrib/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2017-12-02T17:12:30Z
--     Host:                        Linux 4.4.0-63-generic x86_64
--     Target:                      Linux arm
--     CMake:                       3.8.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /home/frank/raspi/yocto/rocko/rpi-build/tmp/hosttools/make
--     Configuration:               Release
-- 
--   CPU/HW features:
--     Baseline:                    NEON
--       requested:                 DETECT
--       disabled:                  VFPV3 NEON
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++11:                       YES
--     C++ Compiler:                /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++  (ver 7.2.0)
--     C++ flags (Release):         -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0=/usr/src/debug/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0 -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native= -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot=  -fvisibility-inlines-hidden  -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0=/usr/src/debug/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0 -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native= -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot=  -fvisibility-inlines-hidden  -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -g  -DDEBUG -D_DEBUG
--     C Compiler:                  /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
--     C flags (Release):           -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0=/usr/src/debug/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0 -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native= -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot=   -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections  -mfp16-format=ieee -fvisibility=hidden -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0=/usr/src/debug/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0 -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native= -fdebug-prefix-map=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot=   -march=armv7ve -marm -mfpu=neon-vfpv4  -mfloat-abi=hard -mcpu=cortex-a7 -I/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/git/include   --sysroot=/home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections  -mfp16-format=ieee -fvisibility=hidden -g  -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed
--     Linker flags (Debug):        -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libjpeg.so /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libwebp.so /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libpng.so /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libz.so /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libtiff.so gstbase-1.0 gstreamer-1.0 gobject-2.0 glib-2.0 gstvideo-1.0 gstapp-1.0 gstriff-1.0 gstpbutils-1.0 v4l1 v4l2 avcodec avformat avutil swscale gphoto2 gphoto2_port exif dl m pthread rt /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libtbb.so
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 core flann imgproc ml objdetect phase_unwrapping photo plot reg surface_matching video xphoto bgsegm face fuzzy img_hash imgcodecs shape tracking videoio xobjdetect highgui superres ts bioinspired dpm features2d line_descriptor saliency calib3d ccalib rgbd stereo structured_light videostab xfeatures2d ximgproc aruco optflow stitching python3
--     Disabled:                    world contrib_world freetype text
--     Disabled by dependency:      datasets
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 viz cnn_3dobj cvv dnn_modern hdf matlab sfm
-- 
--   GUI: 
--     QT:                          NO
--     GTK+:                        NO
--     GThread :                    NO
--     GtkGlExt:                    NO
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libz.so (ver 1.2.11)
--     JPEG:                        /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libjpeg.so (ver )
--     WEBP:                        /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libwebp.so (ver encoder: 0x020e)
--     PNG:                         /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libpng.so (ver 1.6.31)
--     TIFF:                        /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libtiff.so (ver 42 - 4.0.8)
--     JPEG 2000:                   NO
--     OpenEXR:                     build (ver 1.7.1)
--     GDAL:                        NO
--     GDCM:                        NO
-- 
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 57.89.100)
--       avformat:                  YES (ver 57.71.100)
--       avutil:                    YES (ver 55.58.100)
--       swscale:                   YES (ver 4.6.100)
--       avresample:                NO
--     GStreamer:                   
--       base:                      YES (ver 1.12.2)
--       video:                     YES (ver 1.12.2)
--       app:                       YES (ver 1.12.2)
--       riff:                      YES (ver 1.12.2)
--       pbutils:                   YES (ver 1.12.2)
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     OpenNI2:                     NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     Aravis SDK:                  NO
--     UniCap:                      NO
--     UniCap ucil:                 NO
--     V4L/V4L2:                    Using libv4l1 (ver 1.12.3) / libv4l2 (ver 1.12.3)
--     XIMEA:                       NO
--     Xine:                        NO
--     Intel Media SDK:             NO
--     gPhoto2:                     YES
-- 
--   Parallel framework:            TBB (ver 2017.0 interface 9106)
-- 
--   Trace:                         YES ()
-- 
--   Other third-party libraries:
--     Use Intel IPP:               NO
--     Use Intel IPP IW:            NO
--     Use VA:                      NO
--     Use Intel VA-API/OpenCL:     NO
--     Use Lapack:                  NO
--     Use Eigen:                   YES (ver 3.2.8)
--     Use Cuda:                    NO
--     Use OpenCL:                  NO
--     Use OpenVX:                  NO
--     Use custom HAL:              YES (carotene (ver 0.0.1))
-- 
--   Python 2:
--     Interpreter:                 NO
-- 
--   Python 3:
--     Interpreter:                 /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native/usr/bin/python3-native/python3 (ver 3.5.3)
--     Libraries:                   /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/libpython3.5m.so (ver 3.5.3)
--     numpy:                       /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot/usr/lib/python3.5/site-packages/numpy/core/include (ver undefined - cannot be probed because of the cross-compilation)
--     packages path:               lib/python3.5/site-packages
-- 
--   Python (for build):            /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/recipe-sysroot-native/usr/bin/python3-native/python3
-- 
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Matlab:                        Matlab not found or implicitly disabled
-- 
--   Documentation:
--     Doxygen:                     NO
-- 
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              YES
-- 
--   Install path:                  /usr
-- 
--   cvconfig.h is in:              /home/frank/raspi/yocto/rocko/rpi-build/tmp/work/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/opencv/3.3+gitAUTOINC+87c27a074d_2a9d1b22ed_a62e20676a_34e4206aef_fccf7cd6a4-r0/build
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done

Notes for Raspbian users

install first (for smooth video rendering):

Note: if you have a X11 GUI in your image and want GUI support (imshow, waitKey), you must also install:

sudo apt-get install libgtk2.0-dev

OpenCV native build on RasPi instructions: http://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/

sudo apt-get install build-essential git cmake pkg-config 
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev 
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev 
sudo apt-get install gfortran python2.7-dev python3-dev 
sudo apt-get install libatlas3-base libatlas-base-dev libatlas-dev libopenblas-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev libgstreamer1.0-dev gstreamer1.0-omx
mkdir OpenCV
cd OpenCV/
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/pi/OpenCV/opencv_contrib/modules ../opencv
make -j4

Future Steps

Raspbian

Here is a nice tutorial building a recent OpenCV from the sources: https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/ However, I would not create a virtual environment (unless you really need it).

In addition to this, the following pre-requisites are recommended:

gstreamer 1.0

sudo apt-get install gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-rtsp gstreamer1.0-omx-rpi gstreamer1.0-tools libgstreamer1.0-dev

misc.

For makeing on the target

  • I had to increase the swap file size to 400MB
  • use a strong power supply when using parallel make (-j4) otherwise you might expect "spontaneuos" reboots

cmake log summary

-- General configuration for OpenCV 3.4.1 =====================================
--   Version control:               unknown
--
--   Extra modules:
--     Location (extra):            /home/pi/opencv_contrib-3.4.1/modules
--     Version control (extra):     unknown
--
--   Platform:
--     Timestamp:                   2018-03-13T07:13:00Z
--     Host:                        Linux 4.9.59-v7+ armv7l
--     CMake:                       3.7.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
--
--   CPU/HW features:
--     Baseline:
--       requested:                 DETECT
--       disabled:                  VFPV3 NEON
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++11:                       YES
--     C++ Compiler:                /usr/bin/c++  (ver 6.3.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
--
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern hdf java matlab ovis sfm viz
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   GUI:
--     GTK+:                        YES (ver 3.22.11)
--       GThread :                  YES (ver 2.50.3)
--       GtkGlExt:                  NO
--     VTK support:                 NO
--
--   Media I/O:
--     ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.8)
--     JPEG:                        libjpeg (ver 90)
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.28)
--     TIFF:                        build (ver 42 - 4.0.9)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 57.64.101)
--       avformat:                  YES (ver 57.56.101)
--       avutil:                    YES (ver 55.34.101)
--       swscale:                   YES (ver 4.2.100)
--       avresample:                NO
--     GStreamer:
--       base:                      YES (ver 1.10.4)
--       video:                     YES (ver 1.10.4)
--       app:                       YES (ver 1.10.4)
--       riff:                      YES (ver 1.10.4)
--       pbutils:                   YES (ver 1.10.4)
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     NO
--
--   Parallel framework:            pthreads
--
--   Trace:                         YES (built-in)
--
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.2)
--     Custom HAL:                  YES (carotene (ver 0.0.1))
--     Protobuf:                    build (3.5.1)
--
--   NVIDIA CUDA:                   NO
--
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/pi/opencv-3.4.1/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.13)
--     Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.13)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.1)
--     packages path:               lib/python2.7/dist-packages
--
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.3)
--     Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython3.5m.so (ver 3.5.3)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.12.1)
--     packages path:               lib/python3.5/dist-packages
--
--   Python (for build):            /usr/bin/python2.7
--
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Matlab:                        NO
--
--   Install to:                    /usr/local
-- -----------------------------------------------------------------

Python USB Webcam Video Capture Example

Check cam device number and capture capabilities upfront:

pi@raspberrypi:~ $ v4l2-ctl -d /dev/video0 -D
Driver Info (not using libv4l2):
        Driver name   : uvcvideo
        Card type     : Microsoft® LifeCam Studio(TM)
        Bus info      : usb-3f980000.usb-1.2
        Driver version: 4.9.59
        Capabilities  : 0x84200001
                Video Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
pi@raspberrypi:~ $ v4l2-ctl -d /dev/video0 --list-formats-ext
...
       Index       : 1
        Type        : Video Capture
        Pixel Format: 'MJPG' (compressed)
        Name        : Motion-JPEG
        ...
                Size: Discrete 1920x1080
                        Interval: Discrete 0.033s (30.000 fps)
                        ...
import cv2
import numpy as np

cam=cv2.VideoCapture("v4l2src ! image/jpeg, width=1920, height=1080,framerate=30/1 ! jpegdec ! videoconvert ! video/x-raw, format=BGR ! appsink ")
while(1):
        ok,img=cam.read()
        if not ok :
                print "failed to read image"
                break
        cv2.imshow("image",img)
        k=cv2.waitKey(1)
        if k==ord('q'):
                break
cv2.destroyAllWindows()
Clone this wiki locally