Congratulations to our 2022 Qt Champions!

Conversion from QVideoFrame to Opencv

  • Hi all
    I am facing issue in converting qvideoframe to opencv format. The pixelFormat() for the videoframe is "Format_YUV420p".
    The piece of code where I do the conversion is shared below

    cv::Mat result(frame.height(), frame.width(), CV_8UC3,(uchar*)frame.bits());
    cv::Mat tmp;
    cvtColor(result, tmp, cv::COLOR_YUV2GRAY_I420);
    return tmp;


  • Lifetime Qt Champion


    That's a bit light on information.
    What image are you expecting ?
    What image are you getting ?
    What version of Qt are you using ?
    What version of OpenCV are you using ?
    What platform are you running your code on ?

  • HI
    I am working on
    Qt5.9 , Linux Kernel 4.15.
    Opencv 3.2
    Compiler: ARM EABI5

    Currently , I am able to convert QVideoFrame to QImage using the below function
    [Code] size_t byteSize = sizeof(uchar) * 4 * vidFrame.width() * vidFrame.height();
    void data = malloc(byteSize);
    image = new QImage((uchar*)data,vidFrame.width(), vidFrame.height(), QImage::Format_ARGB32_Premultiplied,
    m_cleanImg ,data);

    The QImage needs to be converted to cv::Mat , so that my Qr Scanner can read the Image. The Qr Scanner is written using zbar and opencv and the code is shared below
    QVideoFrame cloneFrame(frame);
    int width = cloneFrame.width(); int height = cloneFrame.height();
    ImageScanner scanner;
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    Mat grey;
    Mat iFormat(img.width(), image.height(), CV_8UC4, (void*)image.bits(),image.bytesPerLine());
    size_t byteSize = sizeof(uchar) * 4 * cloneFrame.width() * cloneFrame.height();
    void data = malloc(byteSize);
    Image image(width,height,"Y800",(uchar
    )data,width*height); //Zbar library
    int n = scanner.scan(image);
    for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
    std::vector<Point> vp;
    // do something useful with results
    std::cout<<"decoded: "<<symbol->get_type_name()<<"symbol: "<<symbol->get_data()<<"\n";
    int n = symbol->get_location_size();
    for(int i=0;i<n;i++){
    RotatedRect r = minAreaRect(vp);
    Point2f pts[4];
    for(int i=0;i<4;i++)



  • Lifetime Qt Champion

    Before diving further in, may I suggest taking a look at this KDAB article using QZxing ? It might simplify your setup.

  • @SGaist
    I have started working on the QZXing library , and will update soon in the forum with any issues

  • I am able to integrate the QZXING library and the source code with my application. But the new issue that comes is the QVideoFrames are of YUYV type
    and there is no frame conversion provided with QZXING library.
    Can i get support as i need to convert the yuyv frames to image (png) format.

  • @FVKamlesh maybe this post is applicable to you. See the response about using Qt private API (at the risk of lost compatibility in the future)

  • @Pablo-J.-Rogina
    Thanks . I will update any progress that happens

  • Lifetime Qt Champion

    Just in case, the conversion from YUYV is not overly complicated, you can take a look at the OpenCV implementation and apply it to your image data.

  • Using the OpenCV library, I was able to convert YUYV format to JPG format. The QZXing filter is able to read the JPG format but performance is slow.
    The camera takes around 20-30seconds to read the converted jpg format.

Log in to reply