本章内容


     1.摄像头基本使用

 

opencv 摄像头基本使用

输出结果

opencv 摄像头基本使用

源码

//#include <QCoreApplication>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>

int main(int argc, char *argv[])
{
    /* 本章内容
     1.摄像头基本使用
    */
    /*
     * apie接口: CV_WRAP explicit VideoCapture(int index, int apiPreference = CAP_ANY);
     *参数分析:
        @param index :摄像头id号
        @param apiPrederence 设备类型
    */
    cv::VideoCapture vCap;
    vCap.open(0); // 打开摄像头 ,cv::CAP_V4L
    if(!vCap.isOpened()){
        std::cout << "摄像头打开失败" << std::endl;
        return -1;
    }
    cv::Mat frames;
    bool ret=0;
    int keyV=0;
    while(1){
        ret = vCap.read(frames);
        if(!ret){
            std::cout << "read cap fail" << std::endl;
            continue;
        }
        cv::imshow("cap", frames);
        keyV = cv::waitKey(20);
        if (keyV == 'q'){
            break;
        }
    }
    vCap.release(); // 释放摄像头,让出资源
    std::cout << "按任意按键退出程序" << std::endl;
    cv::waitKey(0);

    return 1;
}

 

 

 

 

相关文章: