安装opencv4
首先在ubuntu22.04上安装opencv4,可以参考之前的博客ubuntu22.04安装opencv4和opencv_contrib-CSDN博客
安装opencv3
由于有使用opencv3的需求,所以需要继续安装opencv3 ;
安装编译
这里安装的版本是opencv3.4.5,大致安装思路不变,就是要更改安装的默认路径,之前安装opencv4的时候默认安装路径是/usr/local,现在更改为/home/user/opencv3;
cd opencv-3.4.5
mkdir build
cd build
# 注意 这里把路径改成了 /home/user/opencv3
cmake -D CMAKE_INSTALL_PREFIX=/home/user/opencv3 -D CMAKE_BUILD_TYPE="Rlease" -D OPENCV_GENERATE_PKGCONFIG=ON ..
make -j8
sudo make install
环境配置
修改lib:
sudo gedit /etc/ld.so.conf.d/opencv.conf
# 在文件末尾添加下面语句
/home/user/opencv3/lib
# 执行生效
sudo ldconfig
配置环境
# 编辑~/.bashrc
vim ~/.bashrc
#添加如下内容
# opencv3.4.5
export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/home/user/opencv3/lib/pkgconfig
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./home/user/opencv3/lib
#保存退出
source ~/.bashrc
测试
pkg-config opencv –modversion
opencv_version
opencv多版本的切换
默认调用opencv4,每次调用opencv3的时候都要修改CMakeLists.txt:
将find_package(OpenCV REQUIRED)修改为find_package(OpenCV 3.4.5),并添加/修改opencv3安装路径;
set(OpenCV_DIR /home/user/opencv3/share/OpenCV) # 增加/修改为opencv3安装路径
find_package(OpenCV 3.4.5)
# find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBS})
参考博客: