Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Modify frames from Camera/Video before displaying
Forum Updated to NodeBB v4.3 + New Features

Modify frames from Camera/Video before displaying

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    magicstar
    wrote on 26 Dec 2019, 19:57 last edited by magicstar
    #1

    Hello,
    I am developing QMediaPlayer application for Windows platform using Qt5.12.

    I want to perform some OpenCV operations on video frames before displaying it. I do expect there would be some delay due to expensive OpenCV operations. However, video and audio should be synced.

    Also, I want to have a framework where if I am applying any Opencv operations, the video should play without any delay.

    I see lot of options ->

    1. QT with LibVLC
    2. QAbstractVideoSurface(tried seems slow to convert from QVideoFrame to QImg and Opencv::Mat)
    3. QAbstractVideoFilter
    4. QtAV lib

    However, I am not sure which approach would be best with minimal delay and video audio sync.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Dec 2019, 21:46 last edited by
      #2

      Hi,

      If you want minimal perturbation, you have to go low level and do your preprocessing in advance thus pre-loading frames to have enough time to apply your operations. You'll have to benchmark things. You can't expect something nice if it requires 100ms per frame for a movie at 24fps.

      What kind of operations do you have in mind ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 27 Dec 2019, 07:50
      0
      • S SGaist
        26 Dec 2019, 21:46

        Hi,

        If you want minimal perturbation, you have to go low level and do your preprocessing in advance thus pre-loading frames to have enough time to apply your operations. You'll have to benchmark things. You can't expect something nice if it requires 100ms per frame for a movie at 24fps.

        What kind of operations do you have in mind ?

        M Offline
        M Offline
        magicstar
        wrote on 27 Dec 2019, 07:50 last edited by
        #3

        @SGaist ,
        As of now, I plan to apply

        1. Kalman filter
        2. Color detection
        3. Smoothing, etc.

        In Opencv, these algorithms do work at 24 fps. The major delay is converting from QVideoFrame to OpenCV Mat.

        I want to avoid this conversion time

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Dec 2019, 12:24 last edited by
          #4

          What about doing it the other way around, do everything with OpenCV and only the display at the end with Qt ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply 27 Dec 2019, 14:13
          2
          • S SGaist
            27 Dec 2019, 12:24

            What about doing it the other way around, do everything with OpenCV and only the display at the end with Qt ?

            M Offline
            M Offline
            magicstar
            wrote on 27 Dec 2019, 14:13 last edited by
            #5

            @SGaist ,
            I am have Opencv based VideoPlayer using both Thead and Timer based approach. However, I face following issues:

            1. How much delay should I give. If exact delay is given as below:
              45 secs video would finish in 55 secs to be approx without any operations. I don't understand how can I minimize this delay.
            int delay = (1000/frameRate);
                while(!stop){
                    if (!capture.read(frame))
                    {
                        stop = true;
                    }
                    if (frame.channels()== 3){
                        cv::cvtColor(frame, RGBframe, CV_BGR2RGB);
                        img = QImage((const unsigned char*)(RGBframe.data),
                                          RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
                    }
                    else
                    {
                        img = QImage((const unsigned char*)(frame.data),
                                             frame.cols,frame.rows,QImage::Format_Indexed8);
                    }
                    emit processedImage(img);
                    QThread::msleep(delay);
                }
            
            1. Sync with audio. I see some solutions using opencv SDL. However, I am currently overwhelmed with situations and not exactly sure which way to go.
            A 1 Reply Last reply 31 Dec 2019, 08:38
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 27 Dec 2019, 19:35 last edited by
              #6

              How are you reading the audio data with OpenCV ?
              I just realize something, IIRC, there are frame format that allows construction without copy of the data. Did you check that ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply 27 Dec 2019, 20:15
              0
              • S SGaist
                27 Dec 2019, 19:35

                How are you reading the audio data with OpenCV ?
                I just realize something, IIRC, there are frame format that allows construction without copy of the data. Did you check that ?

                M Offline
                M Offline
                magicstar
                wrote on 27 Dec 2019, 20:15 last edited by
                #7

                @SGaist said in Modify frames from Camera/Video before displaying:

                IIRC

                My mistake. ffmpeg is used to read audio and OpenCV to read video.

                there are frame format that allows construction without copy of the data

                • I am not sure which frame format you are saying and how it would help resolve the issue.
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 27 Dec 2019, 22:02 last edited by
                  #8

                  It looks like ffmpeg is directly supported by OpenCV. Did you check that ?

                  As for the format, if using something like RGB 8 bit, you will have data that are following the same memory layout be it for OpenCV Mat or QImage so no conversation needed.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply 28 Dec 2019, 10:31
                  0
                  • S SGaist
                    27 Dec 2019, 22:02

                    It looks like ffmpeg is directly supported by OpenCV. Did you check that ?

                    As for the format, if using something like RGB 8 bit, you will have data that are following the same memory layout be it for OpenCV Mat or QImage so no conversation needed.

                    M Offline
                    M Offline
                    magicstar
                    wrote on 28 Dec 2019, 10:31 last edited by
                    #9

                    @SGaist

                    I saw that post and there are few tutorials for audio video sync with opencv.

                    However, for opencv Mat to QImg, I need to call BGR to RGB converter. There is still some delay.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 28 Dec 2019, 19:57 last edited by
                      #10

                      Are you using OpenCV to do that conversion ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      M 1 Reply Last reply 29 Dec 2019, 11:58
                      0
                      • S SGaist
                        28 Dec 2019, 19:57

                        Are you using OpenCV to do that conversion ?

                        M Offline
                        M Offline
                        magicstar
                        wrote on 29 Dec 2019, 11:58 last edited by
                        #11

                        @SGaist
                        I have tried both OpenCV convert and rgbswapped of QT. There is not much difference in performance

                        1 Reply Last reply
                        0
                        • M magicstar
                          27 Dec 2019, 14:13

                          @SGaist ,
                          I am have Opencv based VideoPlayer using both Thead and Timer based approach. However, I face following issues:

                          1. How much delay should I give. If exact delay is given as below:
                            45 secs video would finish in 55 secs to be approx without any operations. I don't understand how can I minimize this delay.
                          int delay = (1000/frameRate);
                              while(!stop){
                                  if (!capture.read(frame))
                                  {
                                      stop = true;
                                  }
                                  if (frame.channels()== 3){
                                      cv::cvtColor(frame, RGBframe, CV_BGR2RGB);
                                      img = QImage((const unsigned char*)(RGBframe.data),
                                                        RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
                                  }
                                  else
                                  {
                                      img = QImage((const unsigned char*)(frame.data),
                                                           frame.cols,frame.rows,QImage::Format_Indexed8);
                                  }
                                  emit processedImage(img);
                                  QThread::msleep(delay);
                              }
                          
                          1. Sync with audio. I see some solutions using opencv SDL. However, I am currently overwhelmed with situations and not exactly sure which way to go.
                          A Offline
                          A Offline
                          anil_arise
                          wrote on 31 Dec 2019, 08:38 last edited by anil_arise
                          #12

                          @magicstar this is example with openCV to modify frames:

                          cv::VideoCapture CaptureVideo;
                          bool val = CaptureVideo.open(videoFile.toStdString());
                          if(CaptureVideo.isOpened())
                          {
                              while(1)
                              {
                                  if(isFinished || isStopped)
                                  {
                                      CaptureVideo.release();
                                      break;
                                  }
                                  cv::Mat frame;
                                  CaptureVideo >> frame;
                                  if(!frame.empty())
                                  {
                                      QImage qimg(frame.data,
                                                  frame.cols,
                                                  frame.rows,
                                                  frame.step,
                                                  QImage::Format_RGB888);
                                      // Editing frame with date and time or color change etc.. 
                                      {
                                          // qimg.fill(Qt::red);
                                          QPainter p;
                                          if (!p.begin(&qimg))
                                              qDebug()<<"Not";
                                          p.setPen(QPen(Qt::yellow));
                                          // p.setOpacity(0.5);
                                          p.setFont(QFont("Times", 18, QFont::Bold));
                                          p.drawText(qimg.rect(), Qt::AlignVertical_Mask, "  "+QDateTime::currentDateTime().toString("ddd MMMM dd yyyy  HH:mm:ss"));
                                          p.end();
                                      }
                                  }
                              }
                          }
                          
                          1 Reply Last reply
                          0

                          1/12

                          26 Dec 2019, 19:57

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved