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. QImage & openCV cause crashes
Forum Updated to NodeBB v4.3 + New Features

QImage & openCV cause crashes

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimage
4 Posts 3 Posters 1.0k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by Dariusz
    #1

    Hey

    I've got a bit of an issue. I'm trying to process some Mat in openCV & throw it into QImage, but for some reason, QImage crashes with this message :
    f5966c10-7930-447a-9d22-7098d891a708-image.png

    Mat thu = mImage.clone(); //mImage is cv::Mat.
    setGammaExposure(thu, 2.2f, 1.0f);
    if (thu.channels() == 4) {
         cvtColor(thu, thu, COLOR_RGBA2RGB);
       }
    convertTo8(thu);
    mThumbnail = QImage(thu.data, thu.cols, thu.rows, QImage::Format_RGB888).copy();
    thu.release();
    

    The crash happens on QImage part. Not always, since this takes a snapshot of image that gets constantly updated. What I don't understand is why openCV works just fine processing the image, but QImage has a 50/50% chance of crash. Its pretty random when it happens.

    How can I solve this?

    TIA.


    Edit, further testing showed me that its definitelyt on QImage, I added cv::window to show result of the mat and everything there looks "correct" so its down to QImage...

    b983ac5e-b120-4656-b93b-ec18b3ed49da-image.png

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

      Hi,

      Can you show the stack trace you get ?

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

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you show the stack trace you get ?

        D Offline
        D Offline
        Dariusz
        wrote on last edited by Dariusz
        #3

        @SGaist The screenshot above is almost all I get. Before that Its just my function running copying memory and passing it around - nothing to Qt related there.

        I did a number of tests now. From what I can tell, putting my cv::Mat in header and never deleting it helped the crashes... QImage no longer copies the data, only reads from my mat. - that appear to be a lot more stable. However when I try to do this >

        mQImage = QImage(thu.data, thu.cols, thu.rows, QImage::Format_RGB888);
        mThumbnail = mQImage.scaled(QSize(200, 200), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        

        I'm crashing now on scaled function... sigh :- (

        A standalone test goes like this >

            QVector<uchar> data(w * h * 4 * sizeof(float));
            qDebug() << "UCHAR! " << sizeof(uchar) << " Float : " << sizeof(float);
            memcpy(&data[0], image->getPixelData(), w * h * 4 * sizeof(float));
            cv::Mat img = cv::Mat(Size(w, h), CV_32FC4, data.data());
            cv::pow(img, 1 / 2.2, img);
            img.convertTo(img, CV_8U, 255);
            cvtColor(img, img, COLOR_RGBA2RGB);
            QImage i = QImage(img.data, w, h, QImage::Format_RGB888).copy();
            img.release();
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          Where exactly does it crash? Please provide a backtrace.
          And please read the documentation the QImage ctor:

          The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          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