Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Video play issue from file using OpenCV library in QT
QtWS25 Last Chance

Video play issue from file using OpenCV library in QT

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
8 Posts 3 Posters 4.5k Views
  • 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
    Manohar_SL
    wrote on 3 Feb 2017, 10:54 last edited by
    #1

    Hi,

    Am new to OpenCV, trying to play video from file from Qt but unsuccessful.
    Following is the code am trying
    int VideoTest::Videoplay()
    {

    VideoCapture cap("/home/manu/Desktop/song.mp4");
    if(!cap.isOpened())  // check if we succeeded
        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;
    

    }

    Instead of file name if i give camera number am able to open camera and display it, But video file is not opening. Please help with the solution.

    Manohar SL
    Embedded Qt & QML Developer

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Feb 2017, 23:12 last edited by
      #2

      Hi,

      That's not really a problem with Qt, you should check with the OpenCV folks. Note that I'm suspecting a missing codec since you are trying to read an .mp4 file.

      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 4 Feb 2017, 12:21
      1
      • S SGaist
        3 Feb 2017, 23:12

        Hi,

        That's not really a problem with Qt, you should check with the OpenCV folks. Note that I'm suspecting a missing codec since you are trying to read an .mp4 file.

        M Offline
        M Offline
        Manohar_SL
        wrote on 4 Feb 2017, 12:21 last edited by
        #3

        @SGaist Thanks for the response.
        i have installed ffmpeg, do i need to install codec as well? Am working on Ubuntu machine.
        I will post the issue in OpenCV forum.

        Manohar SL
        Embedded Qt & QML Developer

        G 1 Reply Last reply 23 Feb 2017, 14:16
        1
        • M Manohar_SL
          4 Feb 2017, 12:21

          @SGaist Thanks for the response.
          i have installed ffmpeg, do i need to install codec as well? Am working on Ubuntu machine.
          I will post the issue in OpenCV forum.

          G Offline
          G Offline
          gfxx
          wrote on 23 Feb 2017, 14:16 last edited by
          #4

          @Manohar_SL but you try to display video where? I not see qt widget in your code .... any how ... in witch opencv version you try? I std install of ubuntu 16 all codec you need is installed ..... so during opencv compile you can missing some option .... if you use cmake-gui these is simple to detect in advance to using make install command ... for exe. if not use OPTION QT .... not have opencv support for qt ....

          the code you show is only c++ ... so no qt windows appear when you run it? => no qt support in your opencv build.

          regards
          Giorgio

          bkt

          M 1 Reply Last reply 24 Feb 2017, 08:19
          1
          • G gfxx
            23 Feb 2017, 14:16

            @Manohar_SL but you try to display video where? I not see qt widget in your code .... any how ... in witch opencv version you try? I std install of ubuntu 16 all codec you need is installed ..... so during opencv compile you can missing some option .... if you use cmake-gui these is simple to detect in advance to using make install command ... for exe. if not use OPTION QT .... not have opencv support for qt ....

            the code you show is only c++ ... so no qt windows appear when you run it? => no qt support in your opencv build.

            regards
            Giorgio

            M Offline
            M Offline
            Manohar_SL
            wrote on 24 Feb 2017, 08:19 last edited by
            #5

            @gfxx imshow("edges", edges); this will open mat frame and display i guess am not pro in opencv. opencv version am using is 2.4.9.

            Manohar SL
            Embedded Qt & QML Developer

            G 1 Reply Last reply 24 Feb 2017, 09:37
            0
            • M Manohar_SL
              24 Feb 2017, 08:19

              @gfxx imshow("edges", edges); this will open mat frame and display i guess am not pro in opencv. opencv version am using is 2.4.9.

              G Offline
              G Offline
              gfxx
              wrote on 24 Feb 2017, 09:37 last edited by gfxx
              #6

              @Manohar_SL said in Video play issue from file using OpenCV library in QT:

              this will open mat frame and display i guess am not pro in opencv. opencv version am using is 2.4.9.

              mmmm .... regarding qt-opencv, example of imgshow with only c++ code:

              Non Qt opencv

              example of imgshow c++ with qt support enable (the same piece of code):

              enable Qt support

              this to do well clear to me you've installed. What do you see when you cast your code? window with qt support or not?

              sorry these wath is means?:

              i guess am not pro in opencv

              regards
              Giorgio

              bkt

              M 1 Reply Last reply 24 Feb 2017, 09:48
              0
              • G gfxx
                24 Feb 2017, 09:37

                @Manohar_SL said in Video play issue from file using OpenCV library in QT:

                this will open mat frame and display i guess am not pro in opencv. opencv version am using is 2.4.9.

                mmmm .... regarding qt-opencv, example of imgshow with only c++ code:

                Non Qt opencv

                example of imgshow c++ with qt support enable (the same piece of code):

                enable Qt support

                this to do well clear to me you've installed. What do you see when you cast your code? window with qt support or not?

                sorry these wath is means?:

                i guess am not pro in opencv

                regards
                Giorgio

                M Offline
                M Offline
                Manohar_SL
                wrote on 24 Feb 2017, 09:48 last edited by
                #7

                @gfxx Opencv installation is fine am able to load image using opencv and Qt and also am able to view my webcam example using opencv and Qt. opencv is working fine with Qt integration only problem am facing is with video display so sample code will be much appreciated.

                Regards
                Manu

                Manohar SL
                Embedded Qt & QML Developer

                1 Reply Last reply
                1
                • G Offline
                  G Offline
                  gfxx
                  wrote on 24 Feb 2017, 10:31 last edited by gfxx
                  #8

                  cooked and eaten ...... on Unthread ..... because if you use a desk application is better ... especially if it makes other things. I also use opengl to do the video render sometimes .....

                  cv:Point ...bla bla
                  cv::Mat ...bla bla
                  
                  
                  using namespace cv;
                  
                  VideoCapture mo_cap;
                  
                  datathread::datathread (QObject *parent, bool bthw) :
                      QThread(parent), Stop(bthw)
                  {
                  
                  }
                  
                  datathread::~datathread()
                  {
                      this->Stop;
                      mo_cap.release();
                  }
                  
                  cv::Point datathread::tMatrix1(0,0);
                  cv::Point datathread::tMatrix2(90,640);
                  cv::Point datathread::tMatrix3(480,0);
                  cv::Point datathread::tMatrix4(480,640);
                  
                  cv::Mat datathread::dest_image100 = Mat::zeros(640, 480,  CV_8UC3);
                  cv::Mat datathread::temp_image = Mat::zeros(640, 480,  CV_8UC1);
                  
                  int datathread::thExposure = 450;
                  int datathread::thGain = 120;
                  int datathread::MemoryRectSelect = 0;
                  int datathread::SearchArea = 0;
                  
                  int datathread::RequestMbusSend = 0;
                  bool datathread::RequestMbusUnlok = false;
                  bool datathread::RequestKCCUnlok = false;
                  bool datathread::RobotFirsTimeQuote = false;
                  
                  int datathread::Original_Exposure_Value = 160;
                  
                  
                  
                  void datathread::run()
                  {
                  
                          cv::ocl::setUseOpenCL(true);
                  
                          std::ofstream tfile;
                  
                      if (!mo_cap.isOpened())  /** these because my camera take some time before become active **/
                      {
                          qDebug() << "camera close";
                  
                         // VideoCapture mo_cap; // open the default camer
                          this->msleep(500);
                          qDebug() << "camera initialized";
                          this->msleep(1000);
                          qDebug() << "camera is opened?:   " << mo_cap.open(0); /** now is more probable that the camere is active ... sometime it whitout these delay the thread run without camera than crash .... a common problem ...*/
                      }
                  
                  
                  
                      info = ("trasmission start....");
                      emit _msg(info);
                      emit ok();
                      Size scaled (130,120);
                  
                      cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0);
                  
                      AR2 = areaprodotto + gapareaprodotto;
                      AR1 = areaprodotto - gapareaprodotto;
                      AR3 = areaprodotto;
                  
                  
                     // int wKey = -1;
                      selettorestringa = 0;
                      selettoreriga1 = 0;
                      selettoreriga2 = 0;
                      controllokeypoint2 = 0;
                      valorekeypoint2 = 0;
                  
                      s_p[0] = Point(0,0);
                      s_p[1] = Point(0,640);
                      s_p[2] = Point(480,0);
                      s_p[3] = Point(480,640);
                  
                  
                     /* img = cv2.imread('simple.jpg',0)   // **** ORB EXAMPLE ****
                  
                      # Initiate STAR detector ******   img to ---> /home/opencv02/Immagini/dl_P.png
                      orb = cv2.ORB()
                  
                      # find the keypoints with ORB
                      kp = orb.detect(img,None)
                  
                      # compute the descriptors with ORB
                      ##  kp, des = orb.compute(img, kp)
                  
                      # draw only keypoints location,not size and orientation
                      img2 = cv2.drawKeypoints(img,kp,color=(0,255,0), flags=0)
                      plt.imshow(img2),plt.show()*/
                  
                  
                        //-----------------START-------------iterate through each frames of the video
                      if (mo_cap.isOpened())
                      {
                        do
                        {
                  
                              QMutex mutex;
                              // prevent other threads from changing the "Stop" value
                              mutex.lock();
                              if(this->Stop) break;
                              mutex.unlock();
                  
                              if (thGain != ex_gain)   /* ----------  these for manage camera parameter .... v4l issue for some of these .... on opencv forum my solutyon is posted ... but other post a better one .....*/
                              {
                                  thGain = ex_gain;
                                  mo_cap.set(CV_CAP_PROP_GAIN, thGain);
                              }
                              if (thExposure != ex_exposure_abs)
                              {
                                  thExposure = ex_exposure_abs;
                                  mo_cap.set(CV_CAP_PROP_EXPOSURE, thExposure); /***** these not manage from v4l so you must use my correction (or other one) and install new v4l.cpp file *****/
                              }
                  
                  
                          controlkeypoint2 = 0;
                  
                  
                          mo_cap >> dest_image1111;
                          //mo_cap.read(dest_image1111);
                          //qDebug() << "errore 2 ";
                          cvtColor(dest_image1111, dest_image1, COLOR_BGR2RGB);
                  
                          d_p[0] = tMatrix1;
                          d_p[1] = tMatrix2;
                          d_p[2] = tMatrix3;
                          d_p[3] = tMatrix4;
                  
                  /*.................................... i use a series of image transformation .....*/
                  /*.... my horrible but very horrible code .....*/
                  
                  Mat2QImage(dest_image1111);
                  
                  target.release();
                  
                  /*** release all your matrix or cvMat .... than close do cicle....*/
                  } /** emit you signal **/
                  
                     this->Stop;
                  
                  
                    // this->msleep(700);
                    // this->Stop;
                  
                  } 
                  
                  
                  
                  
                  
                  void datathread::Mat2QImage(cv::Mat const& src)
                  {
                  
                      if(!src.empty())
                      {
                          cv::Mat temp; // make the same cv::Mat
                          cvtColor(src, temp,CV_BGR2RGB); // cvtColor Makes a copt, that what i need
                          QImage dest((const uchar *) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
                          dest.bits(); // enforce deep copy, see documentation
                          emit _imma_orig(dest);
                      }
                  
                  }
                  
                  
                  

                  Now you have your QImage and display everywhere in your qt app .....

                  Note: framerate & buffer on camera (not setting by your app or pc, because these is not real, on camera there is a custom/oem drive for these setting, these is real.... all these different by producer from producer ... your pc can make somethings but the camera drive decide for your pc) produce a display delay ... these increase or decrease depending by your pc hardware ... some time by your app .... show image with opengl whith opencv 2.4.x is more efficient and delay is less .... with opencv 3.x the is not really right .... when update opencv to 3.x I cancel my old qtopengl widget and use only QImage and Qt widget because the difference become very poor .....

                  I hope these help.

                  regards
                  giorgio

                  bkt

                  1 Reply Last reply
                  1

                  • Login

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