Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issues with opencv and image
Forum Update on Monday, May 27th 2025

Issues with opencv and image

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quixotic120
    wrote on 25 Mar 2016, 23:27 last edited by
    #1

    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);
    
    m_VideoView->Update(&img);
    
    }
    

    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);
            }
         }
    
    m_VideoView->Update(&dest);
    

    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);
    setPixmap(bitmap); 
    

    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.

    Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Mar 2016, 00:04 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 26 Mar 2016, 06:01
      0
      • S SGaist
        26 Mar 2016, 00:04

        Hi and welcome to devnet,

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

        Q Offline
        Q Offline
        quixotic120
        wrote on 26 Mar 2016, 06:01 last edited by quixotic120
        #3

        @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

        thanks!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 26 Mar 2016, 21:13 last edited by
          #4

          The OpenCV conversion crashes ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          25 Mar 2016, 23:27

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved