当前位置 博文首页 > felicx的博客:Ubuntu之OpenCV

    felicx的博客:Ubuntu之OpenCV

    作者:[db:作者] 时间:2021-09-14 10:17

    ★ 查看linux下的opencv安装版本:

    pkg-config opencv --modversion

    ★ 查看linux下的opencv安装路径:

    sudo find / -iname "*opencv*"

    ★ Ubuntu下多版本OpenCV共存和切换 安装OpenCV参考https://blog.csdn.net/cocoaqin/article/details/78163171
    1、装ROS会自带个OpenCV,默认路径为/usr/local下。如果需要装另一个版本的,就不能再装到/usr/local下了,而是选择其他路径,否则会覆盖掉之前的版本。
    2、首先去https://opencv.org/releases.html下载所需版本的Sources版,也可去https://github.com/opencv/opencv/tree/3.4.1下载。 假设我们安装的第二个OpenCV版本为3.4.1。
    3、解压下载下来的zip包

    $ unzip opencv-3.4.1.zip

    4、安装依赖包

    [compiler]$ sudo apt-get install build-essential
    [required]$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    [optional]$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

    5、编译安装

    $ cd opencv-3.4.1
    $ mkdir build
    $ cd build
    $ mkdir installed
    $ cmake \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DCMAKE_INSTALL_PREFIX=~/opencv-3.4.1/build/installed \
    \
    -DWITH_CUDA=OFF \
    \
    -DBUILD_DOCS=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \ 
    ..
    $ make -j4
    $ sudo make install

    注意: ⅰ、别漏了.. ⅱ、其中~/opencv-3.4.1/build/installed为安装OpenCV3.4.1的路径,这个十分关键 ⅲ、设置OFF的理由如下,可大大加快编译速度,当然还要根据需求进行设置
    6、多版本OpenCv切换

    $ gedit ~/.bashrc

    注释掉

    export PKG_CONFIG_PATH=/usr/lib/pkgconfig ?
    export LD_LIBRARY_PATH=/usr/local/lib 

    在文件末尾增加以下内容

    export PKG_CONFIG_PATH=~/opencv-3.4.1/build/installed/lib/pkgconfig
    export LD_LIBRARY_PATH=~/opencv-3.4.1/build/installed/lib
    $ source ~/.bashrc 

    查询OpenCV版本

    $ pkg-config --modversion opencv

    如果输出3.4.1,就表明配置成功。 如果想使用之前的版本,在~/.bashrc中注释掉增加的内容,去掉之前的注释,然后source ~/.bashrc即可。 如果只有一个版本的OpenCV,在CMakeList.txt中使用以下语句即可

    FIND_PACKAGE(OpenCV REQUIRED)

    上面的这些过程并不能保证在写makefile中调用的是3.41版本的库,可能还是调用之前的2.4.10的库,具体还是要在CMakeList.txt中指定要找的opencv的目录。 在OpenCV编译好后,所在目录中会生成OpenCVConfig.cmake文件,这个文件中指定了CMake要去哪里找OpenCV,其.h文件在哪里等。 存在多版本OpenCV时,需要找到所需版本对应的OpenCVConfig.cmake文件,并将其路径添加到工程的CMakeLists.txt中。

    # cmake needs this line
    cmake_minimum_required(VERSION 2.8)
    set (OpenCV_DIR "~/opencv-3.4.1/build")# 改回默认就注释掉它,换回默认的
    # Define project name
    project(Test)
    # Find OpenCV, you may need to set OpenCV_DIR variable
    # to the absolute path to the directory containing OpenCVConfig.cmake file
    # via the command line or GUI
    find_package(OpenCV REQUIRED)
    # If the package has been found, several variables will
    # be set, you can find the full list with descriptions
    # in the OpenCVConfig.cmake file.
    # Print some message showing some of them
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
    if(CMAKE_VERSION VERSION_LESS "2.8.11")
      # Add OpenCV headers location to your include paths
      include_directories(${OpenCV_INCLUDE_DIRS})
    endif()
    # Declare the executable target built from your sources
    add_executable(demo demo.cpp)
    # Link your application with OpenCV libraries
    target_link_libraries(demo ${OpenCV_LIBS})
    
    
    cs
    下一篇:没有了