Video distorted after Writing Video using Open CV with QT

I have a code in C++ based on QT running on windows built using visual studio 2017.

In this there is a vector of pixmap images.

QVector<QPixmap> imageVector;

The images in the vector we need to write to video file for which we use open cv.

VideoWriter video;
    for (long i = 0; i < imageVector.size(); ++i)
    {
        QPixmap originalPixmap = imageVector.at(i);
        QImage qImageSingle = originalPixmap->toImage(); // Convert QPixmap to QImage

            if(qImageSingle.width() != 0 && qImageSingle.height() != 0)
            {
                video.open("C:\\temp\\signals.avi", 
            //                      CV_FOURCC_DEFAULT,
            ///                     CV_FOURCC('M','J','P','G'), 
                                    CV_FOURCC('D','I','V','X'), 
                                    25, Size(qImageSingle.width(), qImageSingle.height()), 1);
                break;
            }
    }


    for (long i = 0; i < imageVector.size(); ++i)
    {
        QPixmap originalPixmap = imageVector.at(i);
        QImage qImageSingle = originalPixmap->toImage(); // Convert QPixmap to QImage

        if(qImageSingle.width() != 0 && qImageSingle.height() != 0)
        {
            Mat trialFrame;
            trialFrame = Mat(qImageSingle.height(), qImageSingle.width(), CV_8UC3, qImageSingle.bits(), qImageSingle.bytesPerLine()).clone();
            if (trialFrame.channels()== 3)
            {
                Mat trialRGBframe;
                cvtColor(trialFrame, trialRGBframe, CV_BGR2RGB);
                if(trialRGBframe.empty() == true)
                {
                }
                else
                {
                    video.write(trialRGBframe);
                }
            }
        }
    }

    video.release();

The images in the vector we need to write to video file for which we use open cv.

Created Video Screenshot

Original videos screenshot is clear.

How to rectify this issue and create proper video?

debug using imwrite on a single frame. if imwrite shows the same issue, it’s not related to VideoWriter.

next: figure out how to convert your Qt data to cv::Mat. you’ll have to understand “pixel format”, i.e. how colors are packed and padded.

if you don’t know the pixel format in your Qt data, you need to find that out. hint: it’s probably a 4-byte format.

2 Likes

crosspost: