1- Install dependencies:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libgtk2.0-dev pkg-config
2- To compile OpenCV you will need a C++ compiler. Usually it is G++/GCC or Clang/LLVM. also OpenCV uses CMake build configuration tool:
sudo apt install -y g++
sudo apt-get install cmake make
3- Clone repository using git:
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
4- Create build directory:
mkdir -p build && cd build
5- Configure - generate build scripts for the preferred build system:
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
6- Build - run actual compilation process. 4 is number of used processor cores. this part may takes some minutes.
make -j4
7- Installation process copies files to predefined locations and do minor patching:
sudo make install
7.1- You can remove opencv if you want:
sudo make uninstall
8- Create a CMake file. the name of the source code is DisplayImage.cpp
for example:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
9- Generate the executable. This part is easy, just proceed as with any other project using CMake:
cd <DisplayImage_directory>
cmake .
make
10- run executable file:
./DisplayImage
g++ opencv_test.cpp $(pkg-config --cflags opencv)