当前位置:   article > 正文

关于opencv的相关问题总结_sudo apt install libopencv-dev 无法修正错误

sudo apt install libopencv-dev 无法修正错误


最近被opencv折磨惨了,有的程序包需要opencv3,有的依赖于opencv4,还有环境的一些问题,记录一下,填一填坑

1.ubuntu18.04下多版本安装以及共存问题

1)最开始在ubuntu下通过命令行装了默认的opencv3.2.0

sudo apt-get install libopencv-dev python-opencv
  • 1

2)在已有opencv3的基础上安装编译opencv4.4.0 + opencv_contrib-4.4.0 并配置环境

自从进入3.X时代以后,OpenCV将代码库分成了两部分,分别是稳定的核心功能库和试验性质的contrib库。(下载时,二者的版本需要一一对应

opencv网址:https://opencv.org/releases/
opencv_contrib : https://github.com/opencv/opencv_contrib/releases

下载完成后,将两个都解压到主目录,并且将opencv_contrib-4.4.0 放到opencv4.4.0目录下。按照下列步骤完成编译。

cd opencv-4.4.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_ENABLE_NONFREE=YES -D OPENCV_EXTRA_MODULES_PATH=/home/yourname/opencv-4.4.0/opencv_contrib-4.4.0/modules/ ..
make      #如果多线程make的话可能会报错
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果要用到cuda加速则需要DNN模块,编译的命令换成如下:

cmake -D CMAKE_BUILD_TYPE=RELEASE       -D CMAKE_INSTALL_PREFIX=/usr/local       -D CUDA_ARCH_BIN='7.5'      -D WITH_CUDA=ON    -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON     -D INSTALL_C_EXAMPLES=ON       -D OPENCV_EXTRA_MODULES_PATH=/home/zqh/software/opencv-4.4.0/opencv_contrib-4.4.0/modules  -D BUILD_EXAMPLES=ON -D OPENCV_GENERATE_PKGCONFIG=yes  ..
  • 1

环境配置

首先配置链接库路劲

sudo gedit /etc/ld.so.conf.d/opencv4.conf
#输入 
/usr/local/lib
  • 1
  • 2
  • 3

前面的cmake… 那一行命令默认的路径在/usr/local 下面
保存后:

sudo ldconfig
  • 1

然后配置bashrc:

sudo gedit /etc/bash.bashrc 
#添加如下内容:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH
  • 1
  • 2
  • 3
  • 4

保存后:

source /etc/bash.bashrc
sudo updatedb
  • 1
  • 2

3)验证下是否配置成功

pkg-config --modversion opencv4
  • 1

在这里插入图片描述
4)在整个过程中踩了不少坑,将opencv3卸载掉了,于是我又在opencv-4的基础上安装编译了opencv3.4.0 (在调用darknet时,opencv的版本不能高于3.4.0,不然会报错,报错内容下面具体讲)

具体安装编译参考:https://blog.csdn.net/learning_tortosie/article/details/80594399

2.ROS下编译与opencv相关的错误及解决办法

1)error "OpenCV 4.x+ requires enabled C++11 support"

这个错误产生的原因是我将opencv3.2.0删掉了,但是darknet仅支持opencv3.4.0以下版本,后来重新安装编译opencv3.4.0后顺利解决;

2)在这里插入图片描述
解决办法:
主要是CmakeLists.txt文件要配置好opencv动态链接库

set(OpenCV_DIR "~/opencv-3.4.0/build")   
find_package(OpenCV 3.4.0  REQUIRED)

target_link_libraries(calibration_publisher
  ${catkin_LIBRARIES} ${OpenCV_LIBS}
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

参考链接:https://blog.csdn.net/u012816621/article/details/51732932

3.cv_bridge 与opencv4.4.0冲突问题解决

ros-melodic cv_bridge默认配置的是opencv3,所以需要改cv_bridge的配置文件
1)运行命令,更改cv_bridgeConfig.cmake如下

cd /opt/ros/melodic/share/cv_bridge/cmake/
sudo gedit cv_bridgeConfig.cmake
  • 1
  • 2
#if(NOT "include;/usr/include;/usr/include/opencv " STREQUAL " ")
  #set(cv_bridge_INCLUDE_DIRS "")
  #set(_include_dirs "include;/usr/include;/usr/include/opencv")

if(NOT "include;/usr/local/include/opencv4 " STREQUAL " ")
  set(cv_bridge_INCLUDE_DIRS "")
  set(_include_dirs "include;/usr/local/include/opencv4;/usr/local/include;/usr/include")


#set(libraries "cv_bridge;/usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2.0;/usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2.0;/usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2.0")
set(libraries "cv_bridge;/usr/local/lib/libopencv_core.so.4.4.0;/usr/local/lib/libopencv_imgproc.so.4.4.0;/usr/local/lib/libopencv_imgcodecs.so.4.4.0;/usr/local/lib/libopencv_calib3d.so.4.4.0;/usr/local/lib/libopencv_highgui.so.4.4.0;/usr/local/lib/libopencv_dnn.so.4.4.0")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2)更改cv_bridgeConfig.cmake

sudo gedit cv_bridgeConfig.cmake
  • 1
#set(OpenCV_VERSION 3.2.0)
#set(OpenCV_VERSION_MAJOR 3)
#set(OpenCV_VERSION_MINOR 2)
#set(OpenCV_VERSION_PATCH 0)
#set(OpenCV_SHARED ON)
#set(OpenCV_CONFIG_PATH /usr/share/OpenCV)
#set(OpenCV_INSTALL_PATH /usr)
#set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto)
#set(OpenCV_USE_MANGLED_PATHS FALSE)
#set(OpenCV_MODULES_SUFFIX )


set(OpenCV_VERSION 4.4.0)
set(OpenCV_VERSION_MAJOR 4)
set(OpenCV_VERSION_MINOR 3)
set(OpenCV_VERSION_PATCH 13)
set(OpenCV_SHARED ON)
set(OpenCV_CONFIG_PATH /usr/local/share/opencv4)
set(OpenCV_INSTALL_PATH /usr/local/)
set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_cvv;opencv_datasets;opencv_dpm;opencv_face;opencv_fuzzy;opencv_hdf;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_tracking;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto)
set(OpenCV_USE_MANGLED_PATHS TRUE)
set(OpenCV_MODULES_SUFFIX )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/119759
推荐阅读
相关标签
  

闽ICP备14008679号