部署脚本
#!/bin/bash #安装 sudo apt install build-essential cmake git libgtk2.0-dev libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-dev libavresample-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev -y mkdir ~/opencv_install cd ~/opencv_install wget https://github.com/opencv/opencv/archive/refs/tags/4.8.0.tar.gz git clone https://gitee.com/mirrors/opencv_contrib tar zvxf *.tar.gz && sudo mv opencv-*.* opencv && sudo chmod -R 777 opencv mkdir ~/opencv_install/opencv/build &&sudo chmod -R 777 ~/opencv_install/opencv/build && cd ~/opencv_install/opencv/build sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=~/opencv_install/opencv/build -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=ON -D OPENCV_ENABLE_NONFREE=ON -D WITH_TBB=ON-D OPENCV_EXTRA_MODULES_PATH=~/opencv_install/opencv_contrib/modules -D BUILD_opencv_python3=OFF -D BUILD_opencv_python2=OFF -D BUILD_EXAMPLES=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv4.pc .. sudo chmod 777 -R ~/opencv_install/opencv/build sudo make -j8 sudo make install #配置 sudo touch /etc/ld.so.conf sudo chmod 777 /etc/ld.so.conf sudo chmod 777 /etc/bash.bashrc sudo echo /usr/local/lib >> /etc/ld.so.conf sudo echo PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig >> /etc/bash.bashrc sudo echo export PKG_CONFIG_PATH >> /etc/bash.bashrc sudo mkdir /usr/local/lib/pkgconfig sudo touch /usr/local/lib/pkgconfig/opencv.pc sudo chmod 777 /usr/local/lib/pkgconfig/opencv.pc
版本信息修改
输入命令
sudo vim /usr/local/lib/pkgconfig/opencv.pc
将下列信息复制到文件中
prefix=/usr/local exec_prefix=${prefix} includedir=${prefix}/include libdir=${exec_prefix}/lib Name: opencv Description: The opencv library Version:4.8.0 Cflags: -I${includedir}/opencv4 Libs: -L${libdir} -lopencv_stitching -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
执行export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
执行ldconfig
验证安装
1、执行pkg-config opencv --modversion将会打印版本号
2、进入cd ~/opencv_install/opencv/samples/cpp/example_cmake
3、输入 cmake . && make
4、./opencv_example可弹出黑窗
VScode 报错踩坑:
报错:无法打开 源 文件 "opencv2/videoio.hpp"
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/local/include/opencv4/" //此处为opencv4头文件等文件的默认安装目录 ], "defines": [], "cStandard": "c17", "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 }
编译报错
1、无法找到头文件
fatal error: opencv2/****
请将/usr/local/include/opencv4路径下的 opencv2/文件夹放置在 /usr/local/include/ 路径
2、编译源码错误
undefined reference to `cv::****
核对/usr/local/lib/pkgconfig/opencv.pc 文件中Libs: -L${libdir}行链接库数目与名称是否与 /usr/local/lib中opencv链接库是否一致
3、银河麒麟默认路径未发现头文件及lib库文件
银河麒麟库文件及头文件默认保存在 /user/lib/x86_64-linux-gnu/和 /user/include/x86_64-linux-gnu/中,需要修改sudo vim /usr/local/lib/pkgconfig/opencv.pc文件路径