zoukankan      html  css  js  c++  java
  • Linux下使用Opencv打开笔记本摄像头

    新建test文件夹,文件夹存在test.cppCMakeLists.txt

    test.cpp
    #include <iostream> #include <string> #include <sstream> #include <opencv2/core.hpp> #include <opencv2/highgui.hpp> #include <opencv2/videoio.hpp> using namespace cv; using namespace std; const char* keys = { "{help h usage ? | | print this message}" "{@video | | Video file, if not defined try to use webcamera}" }; int main(int argc, const char** argv) { CommandLineParser parser(argc, argv, keys); parser.about("Reading a video and camera v1.0.0"); if (parser.has("help")) { parser.printMessage(); return 0; } String videoFile = parser.get<String>(0); if (!parser.check()) { parser.printErrors(); return 0; } VideoCapture cap; if (videoFile != "") { cap.open(videoFile);// read a video file }else { cap.open(0);// read the default caera } if (!cap.isOpened())// check if we succeeded { return -1; } namedWindow("Video", 1); while (1) { Mat frame; cap >> frame; // get a new frame from camera imshow("Video", frame); if (waitKey(30) >= 0) break; } // Release the camera or video file cap.release(); return 0; }
    CMakeLists.txt
    project(test)
    
    cmake_minimum_required(VERSION 2.8.7)
    
    # option to enable OpenMP; only relevant for the KCF version with the
    # VOT scale estimation
    option(WITH_OPENMP "Enable OpenMP" OFF)
    
    if(WITH_OPENMP)
        find_package(OpenMP REQUIRED)
    
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
    endif(WITH_OPENMP)
    
    # add c++11 support
    if(CMAKE_COMPILER_IS_GNUCC)
        ADD_DEFINITIONS ( -std=c++11 )
    endif(CMAKE_COMPILER_IS_GNUCC)
    
    # add OpenCV
    set(OPENCV_DIR_HINT "")
    
    if(WIN32)
        get_filename_component(OPENCV_DIR_PLATFORM $ENV{OPENCV_DIR} DIRECTORY)
        get_filename_component(OPENCV_DIR_HINT ${OPENCV_DIR_PLATFORM} DIRECTORY)
    endif(WIN32)
    
    set(OpenCV_STATIC OFF)
    find_package(OpenCV REQUIRED HINTS ${OPENCV_DIR_HINT})

    编译过程:

    > cd test
    > g++ test.cpp -o test `pkg-config --cflags --libs opencv`
    > ./test

     


  • 相关阅读:
    李开复给中国学生的第二封信:从优秀到卓越
    李开复写给中国学生的一封信:从诚信谈起
    Cocos2D-x培训课程
    CCLabelAtlas创建自定义字体
    cocos2d-x设计模式发掘之五:防御式编程模式
    【转】VS2010中使用AnkhSvn
    cocos2d-x 2.1.4学习笔记01:windows平台搭建cocos2d-x开发环境
    【转】Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境(亲测)
    ffmpeg开发中出现的问题(二)
    ffmpeg开发中出现的问题
  • 原文地址:https://www.cnblogs.com/wangyarui/p/7265171.html
Copyright © 2011-2022 走看看