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. What format should I use to convert QImage to cv::Mat?
QtWS25 Last Chance

What format should I use to convert QImage to cv::Mat?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.3k 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.
  • A Offline
    A Offline
    Aaron Kim
    wrote on last edited by
    #1
    auto image = this->myLabel->pixmap()->toImage();
    this->background = Mat(image.height(), image.width(), CV_8UC3, const_cast<uchar*>(image.bits()), static_cast<unsigned int>(image.bytesPerLine())).clone();
    this->myLabel->setPixmap(mat2QPixmap(this->background, QImage::Format_RGB888));
    

    This code does not seem to work.
    0_1534946300904_capture.PNG

    I guess that this happens because I chose the incorrect format.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Yes could be wrong format.
      Try code from here
      http://qtandopencv.blogspot.com/2013/08/how-to-convert-between-cvmat-and-qimage.html
      Cant promise it works as i never used cv.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Are you sure your cv::Mat object contains RGB data ?

        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
        1
        • A Offline
          A Offline
          Aaron Kim
          wrote on last edited by
          #4

          @SGaist As you can see, the data were extracted from the QImage

          auto image = this->myLabel->pixmap()->toImage();
          

          So it must be RGB data.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            RGB, most likely, however it's likely not RGB888. You should check the format of your QImage object. As stated in the QPixmap::toImage documentation, it can be QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without alpha.

            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
            1

            • Login

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