Table of contents
2.1 OpenCV source code download
2.2 cmake installation (not necessary)
2.3 opencv installation dependencies and support module view
2.4 Install the pkgconfig package
2.5 Install opencv dependency package
2.6 Start to actually install the opencv package
2.7 Opencv environment path configuration
1. Source code download
opencv can download the source code from the official website (Courses - OpenCV), github, and gitee to compile the library with specific functions you need, or you can get the compiled library from SourceForge and apply it directly.
gitee镜像:opencv: Open Source Computer Vision Library
It is recommended to use a domestic mirror: git clone [email protected]:mirrors/opencv.git
Cmake download: Index of /files, download the required version by yourself, different opencv version compilation requires different cmake version requirements, it is recommended to 2.8.12 or above, this is the version of opecv42.
Two, source code compilation
2.1 OpenCV source code download
This article uses the vmware15 Linux system with centos7 installed, and the git tool is pre-installed. Enter the system, open the command tool, and download the source code:
git clone [email protected]:mirrors/opencv.git
git clone [email protected]:cubone/opencv_contrib.git
After the source code download is complete, enter the opencv directory, open CmakeLists.txt, you can see the cmake version requirements required by different opencv versions:
#
# Configure CMake policies
#
if(POLICY CMP0026)
cmake_policy(SET CMP0026 NEW)
endif()
if(POLICY CMP0042)
cmake_policy(SET CMP0042 NEW) # CMake 3.0+ (2.8.12): MacOS "@rpath" in target's install name
endif()
if(POLICY CMP0046)
cmake_policy(SET CMP0046 NEW) # warn about non-existed dependencies
endif()
if(POLICY CMP0051)
cmake_policy(SET CMP0051 NEW)
endif()
if(POLICY CMP0054) # CMake 3.1: Only interpret if() arguments as variables or keywords when unquoted.
cmake_policy(SET CMP0054 NEW)
endif()
if(POLICY CMP0056)
cmake_policy(SET CMP0056 NEW) # try_compile(): link flags
endif()
if(POLICY CMP0066)
cmake_policy(SET CMP0066 NEW) # CMake 3.7: try_compile(): use per-config flags, like CMAKE_CXX_FLAGS_RELEASE
endif()
if(POLICY CMP0067)
cmake_policy(SET CMP0067 NEW) # CMake 3.8: try_compile(): honor language standard variables (like C++11)
endif()
if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW) # CMake 3.9+: `RPATH` settings on macOS do not affect `install_name`.
endif()
if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW) # CMake 3.12+: Include file check macros honor `CMAKE_REQUIRED_LIBRARIES`
endif()
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW) # CMake 3.13+: option() honors normal variables.
endif()
Therefore, if you need a certain opencv version, you need to download the relevant version support yourself.
2.2 cmake installation (not necessary)
This article installs the cmake-3.25.3 version:
wget https://cmake.org/files/v3.25/cmake-3.25.3.tar.gz --no-check-certificate
//root
//可以先删除旧版本(非必要):yum remove -y cmake
cp cmake-3.25.3.tar.gz /usr/local/
tar zxvf cmake-3.25.3.tar.gz
mv cmake-3.25.3 cmake
cd cmake/
./configure
make && make install
Then modify the profile and set the cmake path
gedit /etc/profile
//修改,例如在文末加入:
export PATH=$PATH:/usr/local/cmake/bin
//使其生效
source /etc/profile
//测试
cmake --version
//完成后删除安装包
rm -f /usr/local/cmake-3.25.3.tar.gz
2.3 opencv installation dependencies and support module view
Create a compilation output directory, and then create a Makefile through the cmake command
//opencv所在父目录
mkdir build_opencv
cd build_opencv
//按需要添加模块支持
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
//输出Makefile文件
//make help命令可以查看支持哪些模块
例如:
The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
... edit_cache
... install
... install/local
... install/strip
... list_install_components
... package
... package_source
... rebuild_cache
... test
......
Read the output information of the cmake command carefully before installing to see if it is not supported by missing packages, for example:
In the conclusion of the cmake command output, carefully consider whether the functions and dependencies we need are supported. For example, the graphics window needs gtk, vtk, etc. support, and for example, the video needs FFMPEG support, and so on. Compared with windows, the installation of opencv under linux is mainly to clarify which opencv modules are needed and which dependent packages these modules need to install .
2.4 Install the pkgconfig package
Go to Index of /releases to download the pkg-config package:
//指令安装,本文安装方式
yum -y install pkgconfig
//安装完后,路径/usr/lib64/pkgconfig,/usr/share/pkgconfig
//如果版本不合适,手动下载安装
wget https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz --no-check-certificate
//解压
tar -zxvf pkg-config-0.29.2.tar.gz
//配置
cd pkg-config-0.29.2/
./configure --prefix=/usr/local/pkg-config --with-internal-glib
//编译
make
//root 用于
make install
//安装路径在/usr/local/pkg-config
whereis pkgconfig
2.5 Install opencv dependency package
Install GTK, etc., centos7 supports yum command to install directly
yum -y install epel-release
yum -y install gtk2 gtk2-devel gtk2-devel-docs
yum -y install libpng-devel
yum -y install jasper-devel
yum -y install openexr-devel
yum -y install libwebp-devel
yum -y install libjpeg-turbo-devel
yum -y install libtiff-devel
yum -y install tbb-devel eigen3-devel
yum -y install boost boost-thread boost-devel
yum -y install libgnomeui-devel
yum -y install libv4l-devel
yum -y install libdc1394-devel
yum -y install gstreamer-plugins-base-devel
yum -y install python-devel numpy
yum -y install gnome-devel gnome-devel-docs
#yum localinstall –nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
#yum localinstall –nogpgcheck https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
#rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
#yum -y install ffmpeg ffmpeg-devel
After installing some dependent packages, restart cmake to check whether these packages are found. The following cmake instructions are for reference only. Please refer to your own system environment and required configuration. For related macros, please check the CMakeLists.txt file of the source code for confirmation:
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ \
-D WITH_GTK=ON \
-D ENABLE_CXX11=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D WITH_CUDA=OFF \
-D BUILD_opencv_cudacodec=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF \
-D WITH_EIGEN=OFF \
-D WITH_VTK=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_opencv_python2=OFF \
../opencv
2.6 Start to actually install the opencv package
Compiling the entire opencv is a time-consuming task. If developers only want to use a certain module, they can compile it on demand, for example:
[***@pyfree build_opencv]$ make -j2 opencv_core
[ 14%] Built target ittnotify
[100%] Built target opencv_core
[***@pyfree build_opencv]$
Since cmake does not specify to compile static libraries, dynamic libraries are compiled by default. After the compilation is complete, you can switch to the root user for installation:
//默认编译
make
//安装
su root
//密码
make install
PS, if there is no relevant dependent package, many opencv modules are canceled during cmake configuration, so the compiled packages are limited. If there is no package or library you want, you may need to repeat cmake, make, and make install directly, so It is best not to delete this compiled package after the installation is complete. You need to wait for the version to be installed and stabilized.
2.7 Opencv environment path configuration
Since cmake specifies the /usr/local installation path, the default installation path is adopted:
OpenCV 默认安装路径是/usr/local目录下, 相关文件部署情况:
/usr/local/bin - executable files
/usr/local/lib - libraries (.so)
/usr/local/cmake/opencv4 - cmake package
/usr/local/include/opencv4 - headers
/usr/local/share/opencv4 - other files
//由于cmake配置时,没有做安装目录指定,采用默认路径,也没有指定编译静态库,默认的是动态库编译
For opencv, the most important thing for our developers is to pay attention to the header file and library path. The storage path and related content information of this article are as follows:
//头文件所在位置
[root@pyfree ***]# ls /usr/local/include/opencv4/opencv2/
calib3d features2d highgui.hpp objdetect stitching.hpp
calib3d.hpp features2d.hpp imgcodecs objdetect.hpp video
core flann imgcodecs.hpp opencv.hpp video.hpp
core.hpp flann.hpp imgproc opencv_modules.hpp videoio
cvconfig.h gapi imgproc.hpp photo videoio.hpp
dnn gapi.hpp ml photo.hpp
dnn.hpp highgui ml.hpp stitching
[root@pyfree ***]#
//动态库文件位置
[root@pyfree ***]# ls /usr/local/lib64 | grep libopencv
libopencv_calib3d.so
libopencv_calib3d.so.407
libopencv_calib3d.so.4.7.0
libopencv_core.so
libopencv_core.so.407
libopencv_core.so.4.7.0
libopencv_dnn.so
libopencv_dnn.so.407
libopencv_dnn.so.4.7.0
libopencv_features2d.so
libopencv_features2d.so.407
libopencv_features2d.so.4.7.0
libopencv_flann.so
libopencv_flann.so.407
libopencv_flann.so.4.7.0
libopencv_gapi.so
libopencv_gapi.so.407
libopencv_gapi.so.4.7.0
libopencv_highgui.so
libopencv_highgui.so.407
libopencv_highgui.so.4.7.0
libopencv_imgcodecs.so
libopencv_imgcodecs.so.407
libopencv_imgcodecs.so.4.7.0
libopencv_imgproc.so
libopencv_imgproc.so.407
libopencv_imgproc.so.4.7.0
libopencv_ml.so
libopencv_ml.so.407
libopencv_ml.so.4.7.0
libopencv_objdetect.so
libopencv_objdetect.so.407
libopencv_objdetect.so.4.7.0
libopencv_photo.so
libopencv_photo.so.407
libopencv_photo.so.4.7.0
libopencv_stitching.so
libopencv_stitching.so.407
libopencv_stitching.so.4.7.0
libopencv_videoio.so
libopencv_videoio.so.407
libopencv_videoio.so.4.7.0
libopencv_video.so
libopencv_video.so.407
libopencv_video.so.4.7.0
[root@pyfree ***]#
Add the dynamic library to the library path:
//下面指令需要安装pkgconfig包,以及opencv开启-D OPENCV_GENERATE_PKGCONFIG=ON 支持
ln -sf /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/
ldconfig
//--1--,root
gedit /etc/profile
//修改PKG_CONFIG_PATH,需要已经安装pkgconfig包
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib64/pkgconfig:/usr/share/pkgconfig
//修改LD_LIBRARY_PATH,将opencv库路径加入,例如文末加入
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64:/usr/lib64:/lib64
//修改保存,关闭文件,运行以下命令生效
source /etc/profile
//--2--,root
cd /etc/ld.so.conf.d/
vi opencv.conf
//或:gedit /etc/ld.so.conf.d/opencv.conf
//添加opencv动态库路径在文件中,如/usr/local/lib64
//配置生效,运行以下命令
ldconfig
After installing gtk and other packages in some versions, a dependency error will occur, resulting in a logout error interface at startup. This requires ctrl+alt+F2 to enter the command mode, and then enter the root account and password.
root
#输入密码
yum update
#...等待
输入 y
Restart after updating, a new version of the kernel may be generated. When starting, it is best to select the original kernel version to log in (that is, the version that we originally installed a series of opencv related software packages).
3. Case test
3.1 Case Design
The project directory is as follows:
#一个读取图片的测试项目
test
bin
1.png #png图片
2.bmp #bmp图片
src
main.cpp
Makefile_linux
main.cpp
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: " << argv[0] << " ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Makefile_linux project configuration information is as follows:
#/bin/sh
CX= g++
BIN := ./bin
TARGET := testlinux.exe
FLAGS := -std=c++11
SRCDIR := ./src
INCLUDEDIR := -I"/usr/local/include/opencv4"
DLLDIR := "/usr/local/lib64"
LIBDIR := -L $(DLLDIR) -lopencv_core -lopencv_highgui -lopencv_imgcodecs
#LIBDIR := $(DLLDIR)/libopencv_core.so $(DLLDIR)/libopencv_highgui.so $(DLLDIR)/libopencv_imgcodecs.so
source := $(wildcard $(SRCDIR)/*.cpp)
$(TARGET) :
$(CX) $(FLAGS) $(INCLUDEDIR) $(source) $(LIBDIR) -o $(BIN)/$(TARGET)
clean:
rm $(BIN)/$(TARGET)
3.2 Compilation test
Run makfe -f Makefile_linux command to compile
[***@pyfree test]$ make -f Makefile_linux
g++ -std=c++11 -I"/usr/local/include/opencv4" ./src/main.cpp -L "/usr/local/lib64" -lopencv_core -lopencv_highgui -lopencv_imgcodecs -o ./bin/testlinux.exe
[***@pyfree test]$
After the compilation is complete, the program will be output in the bin directory, run the program:
cd bin
./testlinux.exe 1.png
./testlinux.exe 2.bmp