Install opencv Raspberry pi




This procedure will show how to install OpenCV which can use C++ and Python for programming.

$ df -h

Filesystem      Size       Used        Avail Use% Mounted on
/dev/root        13G        7.4G         4.9G  61% /
devtmpfs        363M      0              363M   0% /dev
...

OpenCV will need about 2Gb for installation. If not enough space, delete other files or use USB stick to extend memory

Step 1. Update system

$ sudo apt-get update
$ sudo apt-get upgrade




Step 2.  Install tool and lib:

$ sudo apt-get install build-essential cmake cmake-curses-gui pkg-config
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install \
  libjpeg-dev \
  libtiff5-dev \
  libjasper-dev \
  libpng12-dev \
  libavcodec-dev \
  libavformat-dev \
  libswscale-dev \
  libeigen3-dev \
  libxvidcore-dev \
  libx264-dev \
  libgtk2.0-dev


Step 3. Install Video4Linux

Enable camera:
$ sudo raspi-config
-> Set enable the camera: "5 Interface option" -> "P1 camera"
-> Set large memory 256 for gpu_mem: "7 Advance Options" -> "A3 Memmory Split"

Install V4L lib:
$ sudo apt-get -y install libv4l-dev v4l-utils

Now plug USB camera to Raspberry Pi, then check connection
$ v4l2-ctl --list-devices

The result will show as:
UVC Camera (046d:0918) (usb-3f980000.usb-1.4):
        /dev/video0


Step 4. Setup Python & numpy

$ sudo apt-get install python2.7-dev python2-numpy
$ sudo apt-get install python3-dev python3-numpy

Step 5. Download Open CV

$ mkdir /home/pi/opencv
$ cd  /home/pi/opencv
$ wget https://github.com/opencv/opencv/archive/3.2.0.zip -O opencv_source.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip -O opencv_contrib.zip

Then, uzip the file
$ unzip opencv_source.zip
$ unzip opencv_contrib.zip

Step 6. Build & Install OpenCV

$ cd /home/pi/opencv/opencv-3.2.0
$ mkdir build
$ cd build

make configure like these:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_WITH_DEBUG_INFO=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_PERF_TESTS=OFF \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules \
-D ENABLE_NEON=ON \
-D WITH_LIBV4L=ON \
        ../

If everything is fine, result will as:


Adjust configure:
$ ccmake ../


then press [c] for configure, and press [g] for generating makefile
Let's build OpenCV
$ make -j4
It will take long time, about 1 hour for building, if no error, it will end at
[100%] Built target ...

Install OpenCV
$ sudo make install
$ sudo ldconfig

Step 7. Test with C++

Make sample program, name sample.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
 
using namespace cv;
using namespace std;
 
int main(int argc,char ** argv)
{
  VideoCapture cap(0);
  if (!cap.isOpened()) {
    cerr << "ERROR: Unable to open the camera" << endl;
    return 0;
  }
 
  Mat frame;
  cout << "Start grabbing, press a key on Live window to terminate" << endl;
  while(1) {
    cap >> frame;
    if (frame.empty()) {
        cerr << "ERROR: Unable to grab from the camera" << endl;
        break;
    }
    imshow("Live",frame);
    int key = cv::waitKey(5);
    key = (key==255) ? -1 : key; //#Solve bug in 3.2.0
    if (key>=0)
      break;
  }
 
  cout << "Closing the camera" << endl;
  cap.release();
  destroyAllWindows();
  cout << "bye!" <<endl;
  return 0;
}
Build program by command:
$ g++ $(pkg-config --libs --cflags opencv) -o Sample Sample.cpp
Run program by $ ./Sample
Live camera window should show on screen

Another project can be checked at /usr/local/share/OpenCV/samples/cpp

Face detection project:
Build face detect project facedetect.cpp, run it with command:
./facedetect --cascade="/home/pi/opencv/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_alt.xml"

Result video of face detection








4 comments: