Qt World Summit: Submit your Presentation

Issues with opencv and image

  • trying to convert local webcam stream from cv::Mat to QImage but the output is weird. I've tried a bunch of things and searched for hours; I'm officially stuck.

    here is the code snippet in question

    void AppName::SlotFrameReady(cv::Mat image, qint64 captureTime, qint64 processTime)
      //  cv::Mat imageholder;
     //  cv::cvtColor(image, imageholder, CV_BGRA2RGBA);
     //   QImage img((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_Grayscale8);
     //    QImage img((const unsigned char*)(imageholder.data), imageholder.cols, imageholder.rows, QImage::Format_RGB32);
     QImage img((const unsigned char*)(image.data), image.cols, image.rows, image.step, QImage::Format_RGB888);

    This is what I've tried - adding image.step, tried every QImage format, tried img.invertPixels() and img.invertRGB/invertRGBA()

    I have also tried this, found on stackexchange:

    void AppName::SlotFrameReady(const cv::Mat4b &image, qint64 captureTime, qint64 processTime)
    QImage dest(image.cols, image.rows, QImage::Format_RGBA8888);
    for (int y = 0; y < image.rows; ++y) {
            const cv::Vec4b *srcrow = image[y];
            QRgb *destrow = (QRgb*)dest.scanLine(y);
            for (int x = 0; x < image.cols; ++x) {
                    destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255);

    but it results in the same broken output.

    I've also tried creating a temporary image to run cvtColor and convert (tried CV_BGRA2RGB and BGRA2RGBA) and this gives the same result.

    type() output is 24 which, if I am correct, is CV_8UC4 (aka 4 channels of unsigned char).

    If I use any sort of above I get the following (although some formats will show incorrect color instead of just grayscale. This is with RGB8888): http://i.imgur.com/79k3q8U.png

    if I output in grayscale everything works as it should: removed link bc rep isn't enough

    The one thing I haven't tried is writing the mat to a file and reading it into a qimage. My thinking is that this is very inelegant and will be too slow for my needs.

    another thing to note - the video view update function transforms the qimage into a qpixmap for display. could this be where the error is?

    the relevant section of VideoView where it is converted to PixMap and pushed to display:

    QPixmap bitmap = QPixmap::fromImage(*image).transformed(transform, Qt::SmoothTransformation);

    using the FaceTime camera built into my macbook pro as well as with 2 other USB cams I've tried (logitech c270 and a no-name garbage cam)

    On mac 10.11 with QT creator 5 and opencv 2, have also tried opencv3.1 with no change. Have assumed it's an issue with opencv and I asked on their forums awhile back. While I did receive a lot of very helpful responses was not able to find a solution.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What image format should you be getting from OpenCV ? Are you sure you are getting BGRA ?

  • @SGaist Hello,
    I believe opencv works in BGR. Image type is reported as CV_8UC4 which (if I'm correct) translates to an unsigned 8 bit 4 channel image which is why I've been trying BGRA - trying to convert to a 3 channel (e.g. BGR) usually crashes at runtime


  • Lifetime Qt Champion

    The OpenCV conversion crashes ?

Log in to reply