Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Build OpenCV

Mark Sherstan edited this page Jun 2, 2020 · 3 revisions

Building OpenCV 4 on an Odroid running Linux Ubuntu

The following process has been tested on both Ubuntu 18.04 and 20.04.

Operating System (OS) Install

Option I: SD Card

Download the most up to date OS for the board (the following was tested with an Odroid C2). It can be found here. I selected ubuntu-18.04.3-3.16-minimal-odroid-c2-20190814.img.xz. The difference between minimal and mate is that the minimal version has no GUI and has slightly more usable ram.

Use a 16 Gb or larger (reccomended 32 Gb) micro SD card capable of 98Mb/s or greater and flash the OS using Balena Etcher. Once complete insert the SD card into the Odroid C2 and apply power. Once the Odroid has power and the OS has initialized a blue flashing light will begin.

Once the system is ready, scan for the IP address of the Odroid using Angry IP Scanner. On my MacOS the address is root@192.168.2.3 and the default password is odroid.

Option II: eMMC

Purchase a pre flashed eMMC module from the manufacturer and simply snap it into place. eMMC modules have much better performance than SD cards! Ensure to purchase an eMMC adapter to USB in case you need to re-flash anything. Scan for an IP address the same as option I and then update.

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

Prepare OpenCV Install

Run the following commands one at a time and read the outputs and act accordingly.

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

$ sudo apt-get install build-essential cmake unzip pkg-config

$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

$ sudo apt-get install libgtk-3-dev

$ sudo apt-get install libatlas-base-dev gfortran

$ sudo apt-get install python3-dev

The latest release of OpenCV as of April 2020 is 4.3.0. In the future this release can be changed, just update the numbers accordingly.

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

$ unzip opencv.zip
$ unzip opencv_contrib.zip

$ mv opencv-4.3.0 opencv
$ mv opencv_contrib-4.3.0 opencv_contrib

Install virtual environent requirments and source it. You can double check the commands worked by running nano source ~/.bashrc and checking the file contents.

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip

$ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
$ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
$ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

$ source ~/.bashrc

Create a virutal environment and install numpy.

$ mkvirtualenv cv -p python3

$ workon cv

$ pip install numpy

Install OpenCV

Navigate the directories and prepare the cmake.

$ cd ~/opencv
$ mkdir build
$ cd build

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D INSTALL_C_EXAMPLES=OFF \
	-D OPENCV_ENABLE_NONFREE=ON \
	-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
	-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
	-D BUILD_EXAMPLES=ON ..

The Odroid has a finite amount of ram and the install can hang and crash. To mititage this increase the swap storage, this should only be done temporarily as it can burn out the SD card.

$ sudo apt-get install zram-config

Enter the command sudo nano /usr/bin/init-zram-swapping and change the line

mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

To

mem=$(((totalmem / ${NRDEVICES}) * 1024))

OpenCV is now ready to be compiled. This make take forever so sit back and relax.

$ make -j4    

Note: If the compile keeps failing try making with only one core: make -j1

Configure OpenCV

Once OpenCV has compiled to 100% it must be linked. Run the following commands and find the current Python version installed in the virtual environment.

$ sudo make install
$ sudo ldconfig

$ workon cv
$ python --version
$ deactivate

Make sure we are no longer in the virtual environment. The current version of Python at the time of writing is Python 3.6.9. Using that information run the following (use tab completion to be sure):

$ cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6
$ sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

$ cd ~/.virtualenvs/cv/lib/python3.6/site-packages

From there we need to create a sym-link to the compiled file.

$ ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so cv2.so

Make sure change back the swap memory sudo nano /usr/bin/init-zram-swapping and change the line:

mem=$(((totalmem / ${NRDEVICES}) * 1024))

Back to

mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

Reboot the device with sudo reboot -h now and everything should be configured properly. Enter the commands as seen below to verify.

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'4.3.0'

If you run into any errors consider some of these resources:

Clone this wiki locally