linux系统opencv的安装与使用

Posted 鹅要长大

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux系统opencv的安装与使用相关的知识,希望对你有一定的参考价值。

前言

本文主要介绍如何在linux系统安装使用opencv.

具体步骤可参考opencv官网here.

步骤

编译源码之前需要安装相关依赖库;

1.下载源码;

2.解压源码;

3.配置cmake;

注意将配置的错误文件删除,以及cmake目录;

4.编译链接;

5.安装库;

Required Packages

[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

Bash

/opt$ sudo unzip opencv-3.3.1.zip
/opt$cd opencv-3.3.1
/opt/opencv-3.3.1$sudo mkdir build
/opt/opencv-3.3.1$cd build
/opt/opencv-3.3.1/build$sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
/opt/opencv-3.3.1/build$sudo make -j8(wait long time/opt/opencv-3.3.1/build$sudo make install

注意事项

1. 注意你要安装的opencv的版本以及cv12、cv14等;

2.首先需要安装opencv的依赖库,而且必须是在安装opencv之前优先安装依赖库,可参考here

3.测试程序完成之后,需要CMakeLists.txt进行编译,可参考here

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(test_opencv)
SET(OpenCV_DIR /lib/opencv2413/opencv-2.4.13.4/build)
INCLUDE_DIRECTORIES(/lib/opencv2413/opencv-2.4.13.4/build/include)
FIND_PACKAGE(OpenCV REQUIRED)  
ADD_EXECUTABLE(test_opencv test_opencv.cpp)  
TARGET_LINK_LIBRARIES(test_opencv ${OpenCV_LIBS})

FIND_PACKAGE可以设置为多个opencv版本之间的切换;

FIND_PACKAGE(OpenCV 3.3.1 REQUIRED)  

example

find_package( OpenCV 3.1.0 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} ) 
message("OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}") 
message("OpenCV_LIBS: ${OpenCV_LIBS}") 
target_link_libraries( demo ${OpenCV_LIBS} )

4.其他多是调试代码;

如何卸载

cd build
sudo make uninstall 
cd .. 
sudo rm -r build 
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
cd ..
rm -r opencv3.3.1

添加opencv_contrib

to build with modules from opencv_contrib set OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib/modules/>

command

/opt/opencv-3.3.1/build$ sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ .. 

error

CMake Error at cmake/OpenCVModule.cmake:296 (add_subdirectory):
  The binary directory

    /opt/opencv-3.3.1/build/modules/cudaarithm/.cudaarithm

  is already used to build a source directory.  It cannot be used to build
  source directory

    /opt/opencv-3.3.1/opencv_contrib/modules/cudaarithm

  Specify a unique binary directory name.
Call Stack (most recent call first):
  modules/CMakeLists.txt:7 (ocv_glob_modules)

都是类似以上的错误,过程参考herehere,还没有解决。

最后发现是系统有些文件不合适,删除之后再编译就ok啦。。。。发现这个不能解决根本问题。

编译通过但是编译测试程序的时候还是有问题,没有找到对应的文件,个人觉得应该注意opencv_contrib的版本here问题,正在测试。

最后还是两者的版本问题,两个版本一定要一致。

还有就是cmake那个步骤,这次使用的是绝对路径,不知道使用相对路径是否会出问题,试过的童鞋可以回答一下这个问题。

后来使用相同的版本安装编译之后,使用opencv_contrib中的samples中的例程进行测试,可以正常运行,即说明安装成功。

如果还有问题,可能是本身程序有什么BUG引起的,可以进行调试。

test code

/*----------------------------------------------
 * Usage:
 * ./test 0 
 *--------------------------------------------------*/

#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
#include "samples_utility.hpp"

using namespace std;
using namespace cv;

int main( int argc, char** argv ){
  // show help
  if(argc<2){
    cout<<
      " Usage: example_tracking_kcf <video_name>\\n"
      " examples:\\n"
      " example_tracking_kcf Bolt/img/%04.jpg\\n"
      " example_tracking_kcf faceocc2.webm\\n"
      << endl;
    return 0;
  }

  // create the tracker
  Ptr<Tracker> tracker = TrackerKCF::create();

  // set input video
  std::string video = argv[1];
  VideoCapture cap(0);

  Mat frame;

  // get bounding box
  cap >> frame;
  Rect2d roi= selectROI("tracker", frame, true, false);

  //quit if ROI was not selected
  if(roi.width==0 || roi.height==0)
    return 0;

  // initialize the tracker
  tracker->init(frame,roi);

  // do the tracking
  printf("Start the tracking process, press ESC to quit.\\n");
  for ( ;; ){
    // get frame from the video
    cap >> frame;

    // stop the program if no more images
    if(frame.rows==0 || frame.cols==0)
      break;

    // update the tracking result
    bool isfound = tracker->update(frame,roi);
    if(!isfound)
    {
        cout << "The target has been lost...\\n";
        waitKey(0);
        return 0;
    }

    // draw the tracked object
    rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );

    // show image with the tracked object
    imshow("tracker",frame);

    //quit on ESC button
    if(waitKey(1)==27)break;
  }

}
View Code

总结

1.准备安装的opencvopencv_contrib的版本一定要一致,切记!!!

2.按照官网的步骤一步步执行相应的操作,一定要注意cmake那个步骤。

3.安装完成之后可以使用源码中的一些samples代码进行测试。

4.测试的程序一定要没有bug哈~

参考

1.opencv官网

2.CMakeLists编写

3.CMakeLists-practice文档

4.opencv多个版本的cmake切换

5.CSDN博客介绍

以上是关于linux系统opencv的安装与使用的主要内容,如果未能解决你的问题,请参考以下文章

Linux ubuntu16.04 安装opencv4教程(源码编译)

Linux下OpenCV的安装与测试成功教程(解决E: 无法定位软件包 libjasper-dev无法找到directory `opencv.pc‘fatal error:“highgui.h“)

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

Zedboard安装桌面系统ubuntu及opencv

pytorch与opencv 安装(Linux)

基于linux(Centos7)的opencv安装