How to capture video frames in Qt?

I am new to Qt, I only know the basics: I create interfaces and connect slots. In a few words, my knowledge is not deep.

I need to open a video file and capture all its frames to get the R, G, B channels and, later, process the optical stream (this is already done) frame for the frame, to finally present it in the window.

Can I get video frames with Qt? I researched a lot, but did not find anything convincing.

+6
source share
2 answers

I donโ€™t know why I couldnโ€™t include the necessary Qt headers for processing frames (they always had unresolved dependencies and some didnโ€™t exist), so I turned to OpenCV 3.0 and did it like this:

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 worked perfectly for my purpose, so I did not continue research.

Thank you anyway.

+1
source

You can use QMediaPlayer to achieve this.

  • Enter QMediaPlayer .
  • Subclass of QAbstractVideoSurface .
  • Define your implementation as an output for the media player via QMediaPlayer::setVideoOutput .
  • Download the media player to the required file and, in the end, it will start calling QAbstractVideoSurface::present(const QVideoFrame & frame) when implementing QAbstractVideoSurface if the video was successfully downloaded. Then you can access the channels and everything, starting with QVideoFrame and draw a frame in the widgets.
+6
source

Source: https://habr.com/ru/post/988992/


All Articles