Build OpenCV from source code

Build OpenCV from source code

·

4 min read

Ubuntu 22.04 install OpenCV

sudo apt install python3-opencv

Write a python script to check if it works.

#!/usr/bin/env python3

import cv2 as cv

print(cv.__version__)

show version.

$ chmod +x version.py
$ ./version.py
4.5.4

Build from source code

Get Started - OpenCV

OpenCV: Installation in Linux

Build core modules

# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Download and unpack sources
wget -O opencv.zip https://codeload.github.com/opencv/opencv/zip/refs/tags/4.9.0
unzip opencv.zip
# Create build directory
mkdir -p build && cd build
# Configure
cmake -D OPENCV_GENERATE_PKGCONFIG=YES ../opencv-4.9.0
# Build
cmake --build .

Build with opencv_contrib

# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Python3
sudo apt install python3-dev python3-numpy
# Optional Dependencies
sudo apt install libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev
sudo apt install libgtk-3-dev
sudo apt install libvtk7-dev
# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.9.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.9.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
# Create build directory and switch into it
mkdir -p build && cd build
# Configure
cmake -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.9.0/modules ../opencv-4.9.0
# Build
cmake --build .
# Install
sudo make install

If you get some errors like below, you'd better enable swap.

[ 27%] Building CXX object modules/core/CMakeFiles/opencv_perf_core.dir/perf/opencl/perf_arithm.cpp.o
c++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
gmake[2]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/build.make:90: modules/core/CMakeFiles/opencv_perf_core.dir/perf/opencl/perf_arithm.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:3879: modules/core/CMakeFiles/opencv_perf_core.dir/all] Error 2
gmake: *** [Makefile:166: all] Error 2

Enable swap

To enable swap on Ubuntu, follow these steps:

  1. Check if swap is already enabled:
$ sudo swapon --show
NAME  TYPE SIZE USED PRIO
/swap file 265M  63M   -2

If the output of this command is empty, then swap is not enabled.

  1. Create a swap file:
sudo fallocate -l 2G /swapfile

This command will create a 2 gigabyte swap file named /swapfile. You can adjust the size of the swap file to suit your needs.

  1. Set the permissions on the swap file:
sudo chmod 600 /swapfile

This command will set the permissions on the swap file so that only the root user can read and write to it.

  1. Enable the swap file:
sudo mkswap /swapfile

This command will enable the swap file and make it available to the system.

  1. Add the swap file to /etc/fstab:
sudo nano /etc/fstab

Add the following line to the end of the file:

/swapfile       swap    swap    defaults    0       0

This line will tell the system to use the swap file as a swap partition.

  1. Activate the swap file:
sudo swapon /swapfile

This command will activate the swap file and make it available for use by the system.

You can now verify that swap is enabled by running the following command:

$ sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swap     file 265M  67M   -2
/swapfile file   2G 252K   -3

The output of this command should show the swap file that you created.

Note: If you are using a solid-state drive (SSD), you may not need to create a swap file. SSDs are much faster than traditional hard disk drives (HDDs), and they do not suffer from the same performance degradation when using swap space. However, if you are using an HDD, creating a swap file can help to improve performance by providing additional memory for the system to use.

main.cpp

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

int main() {
        std::cout << cv::getVersionString() << std::endl;
        cv::Mat image = cv::imread("opencv.png");

        if (image.empty()) {
                std::cerr << "Error: Unable to load image." << std::endl;
                return 1;
        }

        cv::namedWindow("OpenCV Demo", cv::WINDOW_AUTOSIZE);
        cv::imshow("OpenCV Demo", image);
        cv::waitKey(0);

        return 0;
}

Makefile

CC = g++
CFLAGS = -Wall
LIBS = `pkg-config --cflags --libs opencv4`

all: main

main: main.cpp
        $(CC) $(CFLAGS) -o $@ $< $(LIBS)

clean:
        rm -f main

run

$ make
g++ -Wall -o main main.cpp `pkg-config --cflags --libs opencv4`
$ ./main
4.9.0