install opencv

时间:2024-07-25 18:33:26

OpenCV是一个基于开源发行的跨平台计算机视觉库,它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

工具/原料

  • Windows7
  • VMware Workstation 12 Pro
  • ubuntu-15.10
  • opencv-3.0.0.zip

一、安装关联库

  1. 1

    安装编译工具:

    sudo apt-get install build-essential

    编译程序有了这个软件,它才知道头文件和库函数在哪,最后才形成一个开发环境。

    install opencv
  2. 2

    安装 cmake、git、pkg-config等辅助工具:

    sudo apt-get install cmake git pkg-config libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev

    install opencv
  3. 3

    安装关联库:

    sudo apt-get install python-dev python-opencv python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

    install opencv
    END

二、编译OpenCV

  1. 1

    从网上下载OpenCV-3.0.0.zip。

    下载地址:

    http://opencv.org/downloads.html  (404 Not found)

    https://opencv.org/opencv-3-0.html

    the source code for all the platforms can be downloaded from GitHub: https://github.com/opencv/opencv/archive/3.0.0.zip

    Wish you pleasant experience with OpenCV 3.0!

    install opencv
  2. 2

    如果使用虚拟机,最好将OpenCV-3.0.0.zip移动到用户主目录下进行编译,一定不能在虚拟机在Windows系统里的共享文件夹下进行编译,否则会因为文件系统(Windows7的文件系统是NTFS,ubuntu-15.10的文件系统是)不同在编译过程中出错。鄙人在共享文件夹下进行编译,编译进行到%15时报错,我修复后又在编译进行到%21时报错,反正是各种错误,我是没有能力继续修复了......

    install opencv
    install opencv
  3. 3

    最好将OpenCV-3.0.0.zip移动到用户主目录下进行编译:

    mv [OpenCV-3.0.0.zip存放路径] ~/opencv_3.0.0.zip

    unzip opencv_3.0.0.zip

    cd opencv-3.0.0

    mkdir release

    cd release

    install opencv
    install opencv
  4. 4

    CMake是一个跨平台的编译工具,想深入学习cmake命令的可以百度一下。

    编译命令:

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..  (会报错)

    make时报错如下:

    //usr/lib/x86_64-linux-gnu/libsoxr.so.0: undefined reference to `GOMP_parallel@GOMP_4.0' collect2: error: ld returned 1 exit status

    使用以下命令:

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_OPENMP=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D WITH_GTK=ON WITH_GTK_2_X=OFF -D WITH_CUBLAS=ON -D WITH_NVCUVID=ON -D WITH_LAPACK=ON -D OPENCV_EXTRA_MODULES_PATH=/my/opencv_contrib/modules/path ..

    install opencv
    install opencv
  5. 5

    make命令(-j表示用几个线程来编译,这样可以加快编译速度,不过这个与makefile的质量有关,有的工程用了-j会编译出错):

    make -j $(nproc)

    make 后将报错,内容如下:

    modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/build.make:338: recipe for target 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o' failed make[2]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o] Error 1 make[2]: *** Waiting for unfinished jobs.... CMakeFiles/Makefile2:9578: recipe for target 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all' failed make[1]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs....

    解决如下:

    install opencv
    这是因为opecv3.0与cuda8.0不兼容导致的。解决办法:直接进入 home/wang/opencv/modules/cudalegacy/src文件夹,修改graphcuts.cpp文件内容,如图: 
    install opencv
    其中,#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)||(CUDART_VERSION>=8000) 是我们修改的。 
    修改以后重新make

    上述命令后一般大约要耗时1个小时,甚至更多,视个人电脑硬件配置而定。

    执行完以后不要忘了make install:

    sudo make install

    install opencv
    install opencv
  6. 6

    如果编译过程出错后想要重新编译,把release目录下的CMakeCache.txt删除掉即可。

    install opencv
    END

三、设置环境变量

  1. 1

    添加库路径:

    sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

    install opencv
  2. 2

    更新系统库:

    sudo ldconfig

    install opencv
    END

四、用cmake进行测试

  1. 1

    DisplayImage.cpp中的源代码:

    #include <stdio.h>

    #include <opencv2/opencv.hpp>

    using namespace cv;

    int main(int argc, char** argv)

    {

    if ( argc != 2 )

    {

    printf("[usage]: %s <Image_Path>\n", argv[0]);

    return -1;

    }

    Mat image;

    image = imread( argv[1], 1 );

    if ( !image.data )

    {

    printf("No image data!\n");

    return -1;

    }

    namedWindow("Display Image", WINDOW_AUTOSIZE );

    imshow("Display Image", image);

    waitKey(0);

    return 0;

    }

    install opencv
  2. 2

    CMakeLists.txt中的源代码:

    cmake_minimum_required(VERSION 2.8)

    project( DisplayImage )

    find_package( OpenCV REQUIRED )

    add_executable( DisplayImage DisplayImage.cpp )

    target_link_libraries( DisplayImage ${OpenCV_LIBS} )

    install opencv
  3. 3

    编译命令:

    cmake .

    make

    运行命令:

    ./DisplayImage 01.jpg

    install opencv
    install opencv
    install opencv
    install opencv
    END

五、用make进行测试

  1. opencv_test.cpp中的源代码:

    #include "cv.h"

    #include "highgui.h"

    #include <iostream>

    using namespace std;

    #define PICTURE "./01.jpg"

    int main(void)

    {

    IplImage* img = cvLoadImage(PICTURE, 0);

    cvNamedWindow( "test", 0 );

    cvShowImage("test", img);

    cvWaitKey(0);

    cvReleaseImage( &img );

    cvDestroyWindow( "test" );

    return 0;

    }

    install opencv
  2. Makefile中的源代码:

    CXX          = g++

    CFLAGS    = -Wall

    LDFLAGS  = `pkg-config --cflags --libs opencv`

    SRCS = $(wildcard *.cpp)

    TARGETS = $(patsubst %.cpp, %, $(SRCS))

    all:$(TARGETS)

    $(TARGETS):$(SRCS)

    $(CXX) -o $@ $< $(LDFLAGS) $(CFLAGS)

    clean:

    -rm -rf $(TARGETS) *~ .*swp

    .PHONY: clean all

    install opencv
  3. 3

    编译命令:

    make

    运行命令:

    ./opencv_test

    代码写得比较简单,所以显示效果是一张黑白图片。

    install opencv
    install opencv
    六 遇到的其他错误
    第二步cmake里面 需要下载文件ippicv_linux_20141027.tgz,可以使用网盘下载到指定文件夹下opencv-3.0.0/3rdparty/ippicv
    /downloads/linux-xxxxx/
    参考如下:

    Linux平台上安装OpenCV

    https://jingyan.baidu.com/article/14bd256e466474bb6d2612db.html

    Ubuntu16.04配置Darknet实现YOLO

    https://blog.****.net/u014797226/article/details/78429936

    opencv question

    http://answers.opencv.org/questions/

    http://answers.opencv.org/question/198341/makefile160-recipe-for-target-all-failed-make-all-error-2/