Opencv读取视频或调用摄像头

 一、Opencv读取视频

注意:需要用到这两个库,使用时记得添加-lopencv_video -lopencv_videoio
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;

void main(){
    VideoCapture cap;
    cap.open("./face.mp4");

    if(!cap.isOpened())
        return;

    int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);  //帧宽度
    int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //帧高度
    int frameRate = cap.get(CV_CAP_PROP_FPS);  //帧率 x frames/s
    int totalFrames = cap.get(CV_CAP_PROP_FRAME_COUNT); //总帧数

    cout<<"视频宽度="<<width<<endl;
    cout<<"视频高度="<<height<<endl;
    cout<<"视频总帧数="<<totalFrames<<endl;
    cout<<"帧率="<<frameRate<<endl;

    Mat frame;
    while(1)
    {
        cap>>frame;//等价于cap.read(frame);
        if(frame.empty())
            break;
        imshow("video", frame);
        if(waitKey(20)>0)
            break;
    }
    cap.release();


    ///方法2.
    std::string camera_input = ".....";  //视频地址
    cv::VideoCapture vc(camera_input);
    if (!vc.isOpened()) {
        // error in opening the video input
        std::cerr << "Failed to open camera.\n";
        return -1;
    }
    cv::Mat rawInput;
    while (true) {
        bool ret = vc.read(rawInput);
        if (!ret) {
            std::cerr << "ERROR: Cannot read frame from stream\n";
            continue;
        }
    }
}

二、调用摄像头

    cv::VideoCapture cap(0);  //视频
    while(/*cap.read(img)*/1) {
        ti.reset();
        //double t1 = (double)getTickCount();
        
        //t1 = (double)getTickCount() - t1;
        //time += t1 * 1000 / cv::getTickFrequency();
        time += ti.elapsedMilliSeconds();
        count ++;
        if(count % 1000 == 0) {
            printf("face detection average time = %f.\n", time / count);
        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值