Important: Please read the Qt Code of Conduct -

Video play issue from file using OpenCV library in QT

  • 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;
        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.

  • Lifetime Qt Champion


    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.

  • @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 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.


  • @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 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


  • @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.


  • 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)
    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()
            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
            qDebug() << "camera initialized";
            qDebug() << "camera is opened?:   " <<; /** 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)
          //-----------------START-------------iterate through each frames of the video
        if (mo_cap.isOpened())
                QMutex mutex;
                // prevent other threads from changing the "Stop" value
                if(this->Stop) break;
                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;
            //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 .....*/
    /*** release all your matrix or cvMat .... than close do cicle....*/
    } /** emit you signal **/
      // this->msleep(700);
      // this->Stop;
    void datathread::Mat2QImage(cv::Mat const& src)
            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.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.


Log in to reply