【笔记】Compile and install OpenCV on Linux
OpenCV Build
This article is used to practice English and record the process of operation
1. Clone the OpenCV repository from Github
# Clone the original repository
git clone https://github.com/opencv/opencv.git
# Create a copy for version 3.2.0
cp -r opencv opencv-3.2
# Switch to version 3.2.0
cd opencv-3.2 && git checkout 3.2.0
# Return to the root directory
cd ..
# Create a copy for version 4.11.0
cp -r opencv opencv-4.11
# Switch to version 4.11.0
cd opencv-4.11 && git checkout 4.11.0
2. Clone the opencv_contrib
repository from Github
# Clone the original repository
git clone https://github.com/opencv/opencv_contrib.git
# Create a copy for version 3.2.0
cp -r opencv_contrib opencv_contrib-3.2
# Switch to version 3.2.0
cd opencv_contrib-3.2 && git checkout 3.2.0
# Return to the root directory
cd ..
# Create a copy for version 4.11.0
cp -r opencv_contrib opencv_contrib-4.11
# Switch to version 4.11.0
cd opencv_contrib-4.11 && git checkout 4.11.0
3. Build & Install
This section demonstrates how to build and install OpenCV version 4.11.0.
3.1 Create a build directory
cd ~/opencv-build/opencv
mkdir build && cd build
3.2 Build with CMake
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.11 \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv-build/opencv_contrib-4.11/modules \
-D BUILD_opencv_gapi=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=ON \
-D ENABLE_PRECOMPILED_HEADERS=OFF ..
Parameters Description:
CMAKE_BUILD_TYPE=Release
:Compile to an optimized release version.CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.11
:Specify the installation path so that it is isolated from other version.BUILD_opencv_gapi
:Compile the OpenCV Graph API.OPENCV_GENERATE_PKGCONFIG
:Generate a.pc
file for pkgconfig.OPENCV_EXTRA_MODULES_PATH
:Specify theopencv_contrib
module path.BUILD_EXAMPLES=ON
:Compile the example code (Optional).
3.3 Compile & Install on Linux
# Compile using all avaliable core
make -j$(nproc)
# Install(requires sudo privileges)
sudo make install
4. Configuration the pkg-config
4.1 Edit the Configuration File
Use Vim to open your ~/.bashrc
file , and append the path to the opencv4.pc
to the PKG_CONFIG_PATH
envronment variable:
# At the end of ~/.bashrc
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv-4.11/lib/pkgconfig/
4.2 Enable the ~/.bashrc
Configuration
To apply the changes, reload your ~/.bashrc
file with the following command:
source ~/.bashrc
4.3 Add OpenCV to Dynamic Linker
Create an opencv4.conf
file in the /etc/ld.so.conf.d/
directory that contains the path to OpenCV`s lib
directory.
# /etc/ld.so.conf.d/opencv4.conf
/usr/local/opencv-4.11/lib
4.4 Refresh the Dynamic Linker Cache on Linux
To Update the dynamic linker cache, run:
sudo ldconfig
4.5 Verify pkg-config
command output
Verify that pkg-config
can correctly locate OpenCV by running:
pkg-config --modversion opencv4
At this point, the output should be:
4.11.0