[CMakeList][转载]利用CmakeList构建opencv程序

[CMakeList][转载]利用CmakeList构建opencv程序,第1张

打开VS2019创建CMake项目

CmakeList.txt

# requirement of cmake version

cmake_minimum_required(VERSION 3.10)

set(Project_Name OpencvTest)

set(SRC_LIST OpencvTest.cpp)

set(OpenCV_LIB "D:\\lufiles\\opencv310\\build\\x64\\vc14\\lib")

set(Opencv_Include "D:\\lufiles\\opencv310\\build\\include")

# set(OpenCV_LIB "D:\\lufiles\\opencv310\\build\\")  # 这两个目录中都包含 OpenCVConfig.cmake 文件,两种设置都可以构建项目

PROJECT(${Project_Name})

include_directories(${Opencv_Include})

link_directories(${OpenCV_LIB})

add_executable(${Project_Name} ${SRC_LIST})

target_link_libraries(${Project_Name} opencv_world310d.lib)

OpencvTest.cpp

#include <opencv2/core.hpp>

#include <opencv2/videoio.hpp>

#include <opencv2/highgui.hpp>

#include <iostream>

#include <stdio.h>

using namespace cv

using namespace std

int main(int, char**)

{

    Mat frame

    //--- INITIALIZE VIDEOCAPTURE

    VideoCapture cap

    // open the default camera using default API

    // cap.open(0)

    // OR advance usage: select any API backend

    int deviceID = 0             // 0 = open default camera

    int apiID = cv::CAP_ANY      // 0 = autodetect default API

    // open selected camera using selected API

    cap.open(deviceID + apiID)

    // check if we succeeded

    if (!cap.isOpened()) {

        cerr <<"ERROR! Unable to open camera\n"

        return -1

    }

    //--- GRAB AND WRITE LOOP

    cout <<"Start grabbing" <<endl

        <<"Press any key to terminate" <<endl

    for ()

    {

        // wait for a new frame from camera and store it into 'frame'

        cap.read(frame)

        // check if we succeeded

        if (frame.empty()) {

            cerr <<"ERROR! blank frame grabbed\n"

            break

        }

        // show live and wait for a key with timeout long enough to show images

        imshow("Live", frame)

        if (waitKey(5) >= 0)

            break

    }

    // the camera will be deinitialized automatically in VideoCapture destructor

    return 0

}

OpenCV2和OpenCV3是目前主流的两个版本,有些程序使用OpenCV2,同时有些程序使用了OpenCV3,但是OpenCV2和OpenCV3的API和结构并不完全相同,比如在OpenCV3以后,SIFT和SURF之类的属性被移到了contrib中了,因此我们需要在一台计算机上同时安装并使用多个版本OpenCV。

二、 安装OpenCV2和OpenCV3

1. 安装依赖库

安装官方要求必选包:

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

安装官方可选包:

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

添加对OpenGL的支持所需要的包:

sudo apt-get install freeglut3-dev mesa-common-dev libgtkglext1 libgtkglext1-dev1

安装视频处理所需的包

sudo apt-get install checkinstall yasm libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libgtk2.0-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg1

优化opencv的功能包

sudo apt-get install libatlas-base-dev gfortran qt5-default qtcreator1

2.下载源代码

下载OpenCV2.4.13.3源代码:

git clone https://github.com/opencv/opencv/tree/2.4.13.31

下载OpenCV3.3.0源代码:

git clone https://github.com/opencv/opencv/tree/3.3.01

下载OpenCV3.3.0_contrib源代码:

git clone https://github.com/opencv/opencv_contrib/tree/3.3.01

2.安装OpenCV2和OpenCV3 (无CUDA版本)

安装OpenCV2.4.13.3

$ cd opencv-2.4.13.3/

$ mkdir build

$ cd build

$ cmake -DCMAKE_BUILD_TYPE=Release \

-DCMAKE_INSTALL_PREFIX=/usr/local/opencv2 \

-DWITH_TBB=ON \

-DWITH_IPP=ON \

-DWITH_QT=ON \

-DWITH_GTK=ON \

-DWITH_V4L=ON \

-DWITH_LIBV4L=ON \

-DWITH_OPENGL=ON \

-DWITH_FFMPEG=ON \

-DBUILD_EXAMPLES=ON \

-DINSTALL_PYTHON_EXAMPLES=ON \

-DINSTALL_C_EXAMPLES=ON \

-DPYTHON_EXECUTABLE=/usr/bin/python \

-DPYTHON_INCLUDE_DIR=/usr/include/python2.7 \

-DPYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python2.7 \

-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \

-DPYTHON_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ ..

$ make -j $(($(nproc) + 1))

$ sudo make install1234567891011121314151617181920212223

安装OpenCV3.3.0:

首先将OpenCV3.3.0_contrib-3.3.0放到opencv-3.3.0目录下,然后执行下面命令。

$ cd opencv-3.3.0/

$ mkdir build

$ cd build

$ cmake -DCMAKE_BUILD_TYPE=Release \

-DCMAKE_INSTALL_PREFIX=/usr/local/opencv3 \

-DOPENCV_EXTRA_MODULES_PATH=/home/bdd/opencv-3.3.0/opencv_contrib-3.3.0/modules \

-DWITH_TBB=ON \

-DWITH_QT=ON \

-DWITH_GTK=ON \

-DWITH_V4L=ON \

-DWITH_LIBV4L=ON \

-DWITH_OPENGL=ON \

-DWITH_FFMPEG=ON \

-DBUILD_EXAMPLES=ON \

-DINSTALL_PYTHON_EXAMPLES=ON \

-DINSTALL_C_EXAMPLES=ON \

-DPYTHON_EXECUTABLE=/usr/bin/python \

-DPYTHON_INCLUDE_DIR=/usr/include/python2.7 \

-DPYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python2.7 \

-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \

-DPYTHON_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ ..

$ make -j $(($(nproc) + 1))

$ sudo make install1234567891011121314151617181920212223

其中OPENCV_EXTRA_MODULES_PATH:指定的OpenCV3.3.0_contrib路径


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11373486.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存