Opencv VideoCapture打开摄像头接口讲解

VideoCapture打开摄像头接口讲解

  1. VideoCapture
  2. bool open (int index)
  3. VideoCapture cap(index)
  4. open(int cameraNum, int apiPreference)

源码
cap_ffmpeg_impl.hpp

VideoCapture打开视频流文件

  1. bool open (const String& filename)
  2. VideoCapture cap(const String & filename)
  3. bool open(const String & filename, int apiPreference)

VideoCapture 关闭和空间释放

  1. 关闭和空间释放
  2. ~VideoCapture
  3. release()

读取一帧视频

read(OutputArray image)//cap.cpp
bool grab()   //读取并解码
virtual bool retrieve(OutputArray image, int flag = 0);//图像色彩转化
vc >> mat

bool VideoCapture::read(OutputArray image)
{
    CV_INSTRUMENT_REGION()

    if(grab())
        retrieve(image);
    else
        image.release();
    return !image.empty();
}


#include<iostream>
#include<stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
using namespace std;

int main(int argc, char *argv)
{
    VideoCapture video;
    video.open("1.mp4");
    if (!video.isOpened())
    {
        cout << "open video failed!" << endl;
        getchar();
        return -1;
    }
    cout << "open video success!" << endl;
    namedWindow("video");
    Mat frame;
    for (;;)
    {
        if (!video.read(frame))
        {
            break;
        }
        if (frame.empty()) break;
        imshow("video", frame);
        waitKey(5);
    }
    getchar();


    return 0;
}


#include<iostream>
#include<stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
using namespace std;

int main(int argc, char *argv)
{
    VideoCapture video;
    video.open("1.mp4");
    if (!video.isOpened())
    {
        cout << "open video failed!" << endl;
        getchar();
        return -1;
    }
    cout << "open video success!" << endl;
    namedWindow("video");
    Mat frame;
    for (;;)
    {
        //if (!video.read(frame))
        //{
        //  break;
        //}
        //读帧与解码
        if (!video.grab())
        {
            break;
        }
        //转换颜色格式
        if (!video.retrieve(frame))
        {
            break;
        }
        if (frame.empty()) break;
        imshow("video", frame);
        waitKey(5);
    }
    getchar();


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值