抱着盒子的姑娘和擦汗的男人
0. 一些概念
1. 卸载
1.1 apt 方式安装的卸载方式
sudo apt-get --purge remove libopencv
sudo apt-get --purge remove libopencv-dev
sudo apt-get --purge remove libopencv*
sudo apt-get --purge remove opencv
sudo apt-get --purge remove *opencv*
sudo apt-get autoremove
sudo apt-get autoclean
手动sudo rm-rf
的方式把卸载过程中提示的没删除的文件夹删除:
查看卸载残留:
sudo find /usr/ -name "*opencv*"
手动删除残留文件:
sudo find /usr/ -name "opencv*" | xargs sudo rm -rf
1.2 make install 方式安装的卸载方式
cd ~/opencv-ws/opencv/build
make uninstall
查看卸载残留:
sudo find /usr/ -name "*opencv*"
手动删除残留文件:
sudo find /usr/ -name "opencv*" | xargs sudo rm -rf
2. 安装
安装可参考:
【1】https://opencv.org/get-started/
【2】https://docs.opencv.org/4.x/df/d65/tutorial_table_of_content_introduction.html
2.1 apt 方式安装
sudo apt-get install libopencv-dev
默认安装位置:usr/local
2.2 编译源码方式安装
通过apt安装的opencv其实并不是完整的opencv,因为如cudev、cudaarithm、cudawarping等包并没有包含在 libopencv-contrib-dev package 中。因此,通过编译源码的方式安装opencv是非常有必要的。
- 先安装一些依赖
sudo apt-get install libx11-dev liblapack-dev libeigen3-dev libv4l-dev libzbar-dev libpthread-stubs0-dev libjpeg-dev libpng-dev
- 下载源码
mkdir -p ~/opencv-ws
cd ~/opencv-ws
git clone https://github.com/opencv/opencv
git clone https://github.com/opencv/opencv_contrib
cd opencv && mkdir build && cd build
- 配置
cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -DWITH_CUDA=ON -DBUILD_opencv_cudev=ON -DBUILD_opencv_cudaarithm=ON -DBUILD_opencv_cudawarping=ON ../
注意:在Cmake过程中会从互联网下载一些文件以满足库依赖性,连接故障可能导致某些模块或功能关闭或表现不同。
- 编译并安装
make -j$(nproc)
$ sudo make install
默认安装位置:usr/local
3. 检查OpenCV是否编译了FFmpeg支持
参考:文心一言 搜索关键句“检查OpenCV是否编译了FFmpeg支持”
- 方法1,检查OpenCV的编译配置
查看CMake的输出或CMakeCache.txt文件来确认FFmpeg是否被成功包含。 - 方法2,使用OpenCV的cv::getBuildInformation()
ifOpencvSupportFFmpeg.cpp:
CMakeLists.txt:#include <cstdio> #include <opencv2/opencv.hpp> #include <iostream> int main(int argc, char **argv){ std::string info = cv::getBuildInformation(); std::cout << info << std::endl; // 你可以搜索字符串来查找"FFmpeg" if (info.find("FFmpeg") != std::string::npos || info.find("ffmpeg") != std::string::npos || info.find("FFMPEG") != std::string::npos) { std::cout << "FFmpeg support is enabled." << std::endl; } else { std::cout << "FFmpeg support is not enabled." << std::endl; } return 0; }
# 顶层 cmake_minimum_required(VERSION 3.10) project(RTSPio VERSION 0.1) # 寻找OpenCV库 find_package(OpenCV REQUIRED) # ifOpencvSupportFFmpeg # 添加可执行文件 add_executable(ifOpencvSupportFFmpeg) target_sources(ifOpencvSupportFFmpeg PRIVATE ifOpencvSupportFFmpeg.cpp) # 添加头文件 target_include_directories(ifOpencvSupportFFmpeg PRIVATE /usr/include/opencv4/) # 链接OpenCV库 target_link_libraries(ifOpencvSupportFFmpeg ${OpenCV_LIBS})
4. 检查OpenCV是否编译了某模块/库的支持
方法同上一节。