opencv学习之VideoCapture类

更多文章请关注微信公众号:机器视觉专业论坛 
这里写图片描述

apturing video from cameras or for reading video files and image sequences. Here is how the
class can be used: : VideoCapture类主要用于从相机中、视频文件中、相片序列中读取单帧图像。具体用法如下,图像序列只能为jpg格式
@code
    #include "opencv2/opencv.hpp"

    using namespace cv;

    int main(int, char**)
    {
        VideoCapture cap(0); // 打开默认相机
        if(!cap.isOpened())  // 检查操作是否成功
            return -1;

        Mat edges;
        namedWindow("edges",1);
        for(;;)
        {
            Mat frame;
            cap >> frame; // 从相机中抓取一帧图像
            cvtColor(frame, edges, COLOR_BGR2GRAY);
            GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
            Canny(edges, edges, 0, 30, 3);
            imshow("edges", edges);
            if(waitKey(30) >= 0) break;
        }
        // 相机自动关闭
        return 0;
    }

  /** @overload
    @param filename name of the opened video file (eg. video.avi) or image sequence (eg.
    img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
    */
    CV_WRAP VideoCapture(const String& filename);

    /** @overload
    @param device id of the opened video capturing device (i.e. a camera index). If there is a single
    camera connected, just pass 0. 相机序列号,0为默认设备
    */
    CV_WRAP VideoCapture(int device);






VideoCaptureOpenCV中的一个,用于从视频文件、图像序列或相机中捕获视频。它可以读取常见的视频格式,如AVI、MP4等。使用VideoCapture可以轻松地在OpenCV中处理视频流。 下面是一个使用VideoCapture读取视频文件并显示每一帧的简单示例: ```python import cv2 cap = cv2.VideoCapture('example.mp4') while(cap.isOpened()): ret, frame = cap.read() if ret == True: cv2.imshow('frame',frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows() ``` 在这个例子中,我们首先创建了一个VideoCapture对象并打开了一个名为'example.mp4'的视频文件。然后我们开始一个while循环,不断读取视频的每一帧。在每一帧中,我们使用imshow函数来显示帧,并等待25毫秒。如果用户按下'q'键,程序就会停止。最后,我们释放VideoCapture对象并关闭所有窗口。 除了从视频文件中读取视频,VideoCapture还可以从相机中捕获视频。例如,您可以使用以下代码从摄像头中实时捕获视频: ```python import cv2 cap = cv2.VideoCapture(0) while(cap.isOpened()): ret, frame = cap.read() if ret == True: cv2.imshow('frame',frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows() ``` 在这个例子中,我们将VideoCapture对象初始化为0,这表示我们将从默认相机捕获视频。然后我们开始一个while循环,不断读取相机的每一帧,并且与之前的例子一样,使用imshow函数显示帧,并等待25毫秒。如果用户按下'q'键,程序就会停止。最后,我们释放VideoCapture对象并关闭所有窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值