Opencv + Opencv_contrib的源码编译安装以及C++调用和cmakelist编写
记录一下源码编译安装opencv和利用C++调用opencv库遇到的坑
opencv官网
opencv_contrib官网
参考文档1
报错参考文档
1. 编译安装opencv
1.1 下载源码
- 首先在官网上下载对应版本的源码包xxx.tar格式的
- 先解压opencv的源码包,再将opencv_contrib源码包解压到opencv源码包内部(个人癖好)
1.2 安装需要的各种依赖
sudo apt install build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
- 设置
1.3 准备构建环境和cmake指令
- 在opencv的那个库里
mkdir build && cd build
- 打开终端:出去
export http_proxy="127.0.0.1:7897"
export https_proxy="127.0.0.1:7897"
- cmake指令
cmake \
-D CMAKE_BUILD_TYPE=Release \ # 发行版
-D CMAKE_INSTALL_PREFIX=/usr/local \ # 安装位置
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \ # py 执行文件路径
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ # 指定编译opencv_contrib的路径
-D BUILD_opencv_python3=ON \ # 兼容 py3
-D BUILD_opencv_python2=OFF \
-D PYTHON3_EXCUTABLE=/usr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.6 \ # 这里看一下自己系统的python版本
-D WITH_CUDA=ON \ # 支持 CUDA
# 这个 Flag 开关非常重要,
# 开启了才会生成一个被 pkg-config 命令使用的 opencv4.pc 文件,
# 之后在编译需链接 opencv 库的 C++ 项目时会用到该命令。
-D OPENCV_GENERATE_PKGCONFIG=ON \
..
ps: 实际使用的例子
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 -D OPENCV_EXTRA_MODULES_PATH=/home/gsh/Tools/opencv-4.10.0/opencv_contrib-4.10.0/modules -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D PYTHON3_EXCUTABLE=/usr/bin/python3 -D PYTHON3_INCLUDE_DIR=/usr/include/python3.8 -D WITH_CUDA=ON -D OPENCV_GENERATE_PKGCONFIG=ON ..
1.4 编译安装
# 开启多线程加速,编译时出现的非致命警告可以忽略
make -j 36
# 将 opencv4 安装到 CMAKE_INSTALL_PREFIX 指定的路径
sudo make install
这里需要等很久,要有耐心
1.5 修改环境变量
gedit ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH # Linker 链接器查找库的路径
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig # pkg-config 存放包配置的路径
这里可以去看一下opencv4.pc文件
用命令:sudo find / -iname opencv4.pc
来找到opencv4.pc文件
应该就在:/usr/local/lib/pkgconfig下
内容为
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.10.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_signal -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcufft -L-L/usr/local/cuda -llib64 -L-L/usr/lib -lx86_64-linux-gnu
Cflags: -I${includedir}
1.6 查看版本
重新source后就可以查看了
- 查看opencv版本
pkg-config --modversion opencv4
- 查看opencv的Include目录/头文件路径
pkg-config --cflags opencv4
- 查看opencv的Libs目录/库目录
pkg-config --libs opencv4
2. cmakelist.txt的编写
cmake_minimum_required(VERSION 3.25)
project(opencvtest)
find_package(OpenCV REQUIRED) #加这一句
# 下面 就是 显示 一下
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
#if(POLICY CMP0146)
# cmake_policy(SET CMP0146 NEW)
#endif()
find_package(CUDA REQUIRED)
message(STATUS " libraries: ${CUDA_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS}) #加这一句
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp) #自动生成
add_executable(opencvtest main.cpp)
target_link_libraries(opencvtest ${OpenCV_LIBS})#加这一句
加载camke的时候会报一个警告,没事不影响
注意第一行cmake的版本要求是小于3.26即可,大于的话会报错
3. 查看c++ 调用的opencv 版本
# include "opencv2/opencv.hpp"
using namespace cv;
int main(){
printf(CV_VERSION);
}
显示一张图
#include <iostream>
#include <opencv2/opencv.hpp>
#include "string"
using namespace std;
int main() {
string image_path = "/home/gsh/Downloads/snowman-snow-covered-winter-snow-4608x3456-9163.jpg";
cv::Mat image = cv::imread(image_path);
// cv::namedWindow("image", cv::WINDOW_NORMAL);
cv::namedWindow("image", cv::WINDOW_AUTOSIZE);
cv::imshow("image", image);
int k = cv::waitKey(0);
std::cout << "Hello, World!" << std::endl;
return 0;
}