How to grab video frames in Qt?

时光怂恿深爱的人放手 提交于 2019-11-28 12:12:05

You can use QMediaPlayer to achieve this.

  1. Instantiate the QMediaPlayer.
  2. Subclass the QAbstractVideoSurface.
  3. Set your implementation as the output for the media player via QMediaPlayer::setVideoOutput.
  4. Feed the media player the needed file and eventually it will start calling QAbstractVideoSurface::present(const QVideoFrame & frame) on your implementation of QAbstractVideoSurface if the video was loaded successfully. Then you can access the channels and everything from the QVideoFrame and draw the frame on a widget.

I do not know why I could not include the necessary Qt headers to process frames (they seemed to always have unresolved dependencies and some did not exist) so I turned to OpenCV 3.0 and did it this way:

cv::VideoCapture cap(videoFileName);

if(!cap.isOpened())  // check if we succeeded
    return;

while (cap.isOpened())
{
    cv::Mat frame;
    cap >> frame;
    cv::flip(frame, frame, -1);
    cv::flip(frame, frame, 1);

    // get RGB channels
    w = frame.cols;
    h = frame.rows;
    int size          = w * h * sizeof(unsigned char);
    unsigned char * r = (unsigned char*) malloc(size);
    unsigned char * g = (unsigned char*) malloc(size);
    unsigned char * b = (unsigned char*) malloc(size);

    for(int y = 0; y < h;y++)
    {
        for(int x = 0; x < w; x++)
        {
            // get pixel
            cv::Vec3b color = frame.at<cv::Vec3b>(cv::Point(x,y));
            r[y * w + x] = color[2];
            g[y * w + x] = color[1];
            b[y * w + x] = color[0];
        }
    }
}

cap.release();

It has worked perfectly for my purpose so I did not continue researching.

Thanks anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!