Qt World Summit: Submit your Presentation

Issue with VideoWriter

  • Hi

    I'm trying to create video file using cv VideoWriter class in c++. I'm getting frames convert them to QImage img and display them on GUI well. I want to create video file .avi / .mp4 with the frames when I click on button on GUI.

    Attached procedure to creating the VideoWriter and write frames .

                        QString savedPath = "/home/nvidia/Pictures/";
                        CvSize size = cvSize(2048, 1148);
                        char cur[1024];
                        timespec time;
                        clock_gettime(CLOCK_REALTIME, &time);
                        tm nowtm;
                        localtime_r(&time.tv_sec, &nowtm);
                        sprintf(cur, "%s%04d%02d%02d%02d%02d%02d.avi", savedPath.toLocal8Bit().data(), nowtm.tm_year+1900, nowtm.tm_mon+1, nowtm.tm_mday, nowtm.tm_hour, nowtm.tm_min, nowtm.tm_sec);
                        video = cv::VideoWriter(cur,cv::VideoWriter::fourcc('M','J','P','G'),25.0, size, 1);
                        m_isCreateVideoWriter = true;
                        //Clicked 'Stop Record' button
                        //Close video writer
                        if (CamObject::getInstance()->m_bFinishRec)
                            m_isRecording = false;
                            m_isCreateVideoWriter = false;
                        // Recording...
                            videoFrame = QImageToCvMat(img); //img = QImage(frame.width, frame.height, QImage::Format_Grayscale8);

    Also I created method QImageToCvMat because their is not write method with QImage :

    cv::Mat QImageToCvMat(QImage &inImage)
        cv::Mat mat(inImage.height(), inImage.width(),
                    CV_8UC1, inImage.bits(), inImage.bytesPerLine());
        return mat;

    When I debug I'm getting signal aborted error on write frame line and the program collapse.


    Any one familiar with this issue or can kindly give advice?

  • I haven't used VideoWriter
    But from the documentation:

    cv::VideoWriter::VideoWriter ( const String & filename,
    int fourcc,
    double fps,
    Size frameSize,
    bool isColor = true
    isColor If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames.


    virtual void cv::VideoWriter::write ( InputArray image )
    image The written frame. In general, color images are expected in BGR format.

    Since you are writing grayscale images, I think you should pass 0 as isColor while currently you are using 1.
    Also I think this may be not quite related to Qt...Wouldn't it be better to find an OpenCV forum to ask this kind of questions?
    (Of course if the crash is due to the image conversion, then it is kind of Qt related.)

Log in to reply