ubuntu 16.04 opencv4.4.0安装 ROS版本下opencv版本替换

安装opencv4.4.0 opencv_contrib4.4.0

  1. 下载 opencv4.4.0 opencv_contrib4.4.0
    这是我csdn的上传地址,不需要积分
    https://download.csdn.net/download/weixin_43436587/12666328
  2. 卸载原来的opencv版本,这个可以搜下
  3. 安装opencv pencv_contrib
    (1)安装依赖
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

(2)解压opencv4.4.0 opencv_contrib4.4.0,两个文件夹在同一个目录下。

cd /home/yan/opencv/
mkdir build
cd build
cmake -D BUILD_TIFF=ON -D ENABLE_PRECOMPILED_HEADERS=OFF -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D PYTHON3_EXCUTABLE=/usr/bin/python3 -D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m -D WITH_CUDA=ON -D OPENCV_GENERATE_PKGCONFIG=ON ..

装GPU 记得改WITH_CUDA为onopencv_contrib4.4.0路径填写正确。
大部分步骤根据教程即可实现安装。

  1. 编译时ubuntu安装opencv无法下载IPPICV的问题 ippicv_2020_lnx_intel64_20191018_general.tgz
    https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20191018/ippicv
    用迅雷瞎下载比较快
    “file:/home/rb/opencv-3.4.10/3rdparty/ippicv/” #https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/将网址改为下载文件的保存地址。
    重新cmake

  2. 如果遇到缺少boostdesc_lbgm.i, vgg_generated_80.i,等错误,可能是网络不好,下载不下来、或是被墙的原因,可以下载如下压缩包,根据报错位置,复制到相应文件夹中。重新cmake,不会从头编译,会接着报错的地方继续。
    下载地址:(https://download.csdn.net/download/weixin_43436587/12667225

  3. 在编译的时候,还会遇到在找不到xfeatures2d.hpp,nonfree.hpp文件的错误,此时打开报错的.cpp文件,是因为 #include<>路径问题。例如#include
    (1)如果被包含的文件与.cpp文件在统一目录下,可以直接修改#include
    (2)如果出错文件所在文件夹,没有这个.hpp文件,可以通过报错路径找到.hpp文件,然后再更改#include<>。
    继续接着编译。

  4. 继续安装

make
sudo make install
  1. 配置环境变量,参考 https://blog.csdn.net/weixin_42106049/article/details/102093424
    make后边可以加j4/j8,根据自己电脑能力开启多线程。
1、sudo gedit /etc/ld.so.conf.d/opencv.conf
     # 打开后可能是空文件,在文件内容最后添加
     /usr/local/lib
2、更新系统库
     sudo ldconfig
3、配置bash
     sudo gedit /etc/bash.bashrc  
     //在末尾添加
     PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
4、更新bash文件
     source ~/.bashrc
     sudo updatedb

  1. 测试
# 进入opencv的安装目录
1、cd opencv/samples/cpp/example_cmake
2、cmake .
3、make
4、./example_cmake
  1. 安装成功之后,在编译opencv程序的时候,会调用ros下的opencv,不会调用自己安装的opencv4.4.0版本。
    通过更改程序cmakelists.txt即可。
    下边是我的ros下的cmakelists.txt
cmake_minimum_required(VERSION 3.1)
project(test_pkg)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
set(OpenCV_DIR /usr/local/include/opencv4)
find_package(catkin REQUIRED COMPONENTS
  #cv_bridge
  roscpp
  rospy
  std_msgs
)

find_package(OpenCV 4 REQUIRED)

message(STATUS "   OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

add_executable(object src/object.cpp)
target_link_libraries(object ${catkin_LIBRARIES} ${OpenCV_LIBS} )

(1)set(OpenCV_DIR /usr/local/include/opencv4) 设定路径为自己opencv的安装路径,
find_package(OpenCV 4 REQUIRED),在opencv后加4。
(2)在踩坑的时候,有的教程会让在find_package()添加cv_bridge,但是,系统默认的cv_bridge会将ros下的opencv版本调用。所以我的文件中,将其注释了,成功找到。但是此处有一个大佬的教程,我没有照着他的做,但是可以参考下
https://blog.csdn.net/fb_941219/article/details/105705759?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-8-105705759.nonecase

补充:
执行pkg-config --modversion opencv查看opencv版本的时候可能会出现问题,需要确保
1.第六步的时候设置了OPENCV_GENERATE_PKGCONFIG=ON
2.上面的PKG_CONFIG_PATH的变量的值 /usr/local/opencv4lib/pkgconfig的路径下有opencv.pc文件。若是opencv4.pc,则将该文件名修改为opencv.pc
执行命令sudo cp opencv4.pc opencv.pc
原文链接:https://blog.csdn.net/thorking01/article/details/95060051

参考:https://blog.csdn.net/weixin_42106049/article/details/102093424
https://blog.csdn.net/thorking01/article/details/95060051
https://blog.csdn.net/qq_42145185/article/details/98759401
https://blog.csdn.net/fb_941219/article/details/105705759

你可能感兴趣的:(ubuntu 16.04 opencv4.4.0安装 ROS版本下opencv版本替换)