C/C++教程

qt下使用opencv读取视频

本文主要是介绍qt下使用opencv读取视频,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

测试环境:ubuntu18.04+opencv4.2+Qt

一个基础的opencv读取视频并显示demo。

video.pro文件:

SOURCES += main.cpp

INCLUDEPATH += /usr/local/opencv4/include/opencv4 \
               /usr/local/opencv4/include/opencv4/opencv2

LIBS += -L/usr/local/opencv4/lib -lopencv_freetype -lopencv_highgui -lopencv_videoio -lopencv_imgproc -lopencv_core -lopencv_imgcodecs -lopencv_video

main.cpp文件:

#include <QCoreApplication>
#include <QMetaType>
#include <QDebug>
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qRegisterMetaType<cv::Mat>(cv::Mat&);

    cv::VideoCapture cap;
    cap.open("./data/videos/TownCentreXVID.avi");
    if(!cap.isOpened())
    {
        qDebug() << " ===> please check the video path.";
        return 0;
    }

    int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);

    cv::Mat frame;
    int nFrame = 0;

    cv::namedWindow(video, 0);
    cv::resizeWindow(video, 960, 480);
    cv::moveWindow(video, 200, 100);

    while(true)
    {
        int frame_id = cap.get(cv::CAP_PROP_POS_FRAMES);
        std::cout <<  " frame_id = "  << frame_id << std::endl;

        double t = (double)cv::getTickCount();
        cap.read(frame);
        if (frame.empty())
        {
            qDebug() <<  "===> image empty.";
            continue;
        }
        nFrame += 1;

        t = ((double)cv::getTickCount() - t) * 1000 / cv::getTickFrequency();
        cout <<  "nFrame = " << nFrame << ", Decode time for a frame :"  << t << "ms" << endl;

        cv::imshow(video, frame);
        cv::waitKey(10);
    }
    cap.release();
    return a.exec();
}

结果:

这篇关于qt下使用opencv读取视频的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!