Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Rockpi4/dev/build-opencv

< Rockpi4‎ | dev
    ROCK Pi 4 >  Development >  Install OpenCV

This a guide to install OpenCV on your ROCK Pi 4 that running Ubuntu. All the instructions are done in terminal.

Steps Of Install OpenCV

Step 1: Get Ubuntu running on ROCK Pi 4

To use OpenCV, a OS desktop is necessary. If your ROCK Pi 4 is running Ubuntu but without desktop, you can install Ubuntu mate desktop.

   sudo apt install ubuntu-mate-core && sudo apt install ubuntu-mate-desktop

Step 2: Install all the recommended packages

  • Compilers:
   $ sudo apt-get install build-essential
  • Required:
   $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  • Recommended optional packages
   $ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
   $ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libavresample-dev
   $ sudo apt install tesseract-ocr cmake-data liblept5

Step 3: Download OpenCV source

We suggest that you are at the home folder, so that you don't have to change the code in the next steps.

We need to download OpenCV source. OpenCV latest release: [here]. As of writing this, the latest release is 4.0.1. For the newer version, just replace the link for the source code zip file.

   $ cd ~
   $ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.1.zip
   $ unzip opencv.zip

And we also need to install the contribution packages of OpenCV. Here we get the latest release version:

   $ cd ~
   $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.1.zip
   $ unzip opencv_contrib.zip

Step 4: Configuring and compiling

Confifure thte build using cmake:

   $ cd ~/opencv-4.0.1/
   $ mkdir build
   $ cd build
   $ export PY_NAME=$(python -c 'from sys import version_info as v; print("python%d.%d" % v[:2])')
   $ export PY_NUMPY_DIR=$(python -c 'import os.path, numpy.core; print(os.path.dirname(numpy.core.__file__))')
   $ cmake -DCMAKE_BUILD_TYPE=RELEASE \
        -DCMAKE_INSTALL_PREFIX=/usr/local \
        \
        -DPYTHON2_EXECUTABLE=$(which python) \
        -DPYTHON_INCLUDE_DIR=/usr/include/$PY_NAME \
        -DPYTHON_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/$PY_NAME \
        -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/lib$PY_NAME.so \
        -DPYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/$PY_NAME/dist-packages/numpy/core/include/ \
        \
        -DBUILD_DOCS=OFF \
        -DBUILD_EXAMPLES=OFF \
        -DBUILD_TESTS=OFF \
        -DBUILD_PERF_TESTS=OFF \
        \
        -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.1/modules \
        ..

Compile OpenCV in the build folder:

   $ make -j$(nproc --all)

Install OpenCV in the build folder:

   $ sudo make install
   $ sudo ldconfig

Step 5: Test your building and installation

To confirm that you have install ed OpenCV correctly , try this:

   rock@linux:~$ python
   Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
   [GCC 7.3.0] on linux2
   Type "help", "copyright", "credits" or "license" for more information.
   >>> import cv2
   >>> cv2.__version__
   '4.0.1'
   >>>

Step6: Do the sample supported by OpenCV official

   $ cd ~/opencv-4.0.1/samples/python
   $ python watershed.py

Finally, if you don't need the files in opencv-4.0.1 or opencv_contrib-4.0.1. You can remove them:

   $ cd ~
   $ rm -rf opencv-4.0.1 opencv_contrib-4.0.1 opencv_contrib.zip opencv.zip

If you follow the code exactly, you will find it working just fine.

Troubleshooting