Opencv读取视频或调用摄像头

 一、Opencv读取视频

注意:需要用到这两个库,使用时记得添加-lopencv_video -lopencv_videoio
#include
#include
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<<"视频宽度="<0)
            break;
    }
    cap.release();
}

二、调用摄像头

    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);
        }
    }

 

你可能感兴趣的:(图像处理/传统方法)