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. Convert between cv::mat and Qimage would make the program crash
Forum Updated to NodeBB v4.3 + New Features

Convert between cv::mat and Qimage would make the program crash

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.6k 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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    @

    #include "opencv2/core/core.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"

    #include <QtGui>
    
    inline QImage const mat_to_qimage_ref(cv::Mat &mat)
    {
      return QImage((unsigned char*)(mat.data), mat.cols, mat.rows, mat.step, QImage::Format_RGB32);
    }
        
    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);
      QImage img("lena2.jpg");
      cv::Mat mat(img.height(), img.width(), CV_8UC4, img.bits(), img.bytesPerLine());
      QImage img = mat_to_qimage_ref(mat); //#1
      //QImage img2((unsigned char*)(mat.data), mat.cols, mat.rows, mat.step, QImage::Format_RGB32); #2
    
      QLabel label;
      label.setPixmap(QPixmap::fromImage(img)); //crash at here
      label.show();
    
      return a.exec&#40;&#41;;
    }
    

    @

    (#2) is ok, but #1 will occur undefined behavior?(my case is crash)

    Besides, if you use it as the codes below, It is okay

    @
    cv::Mat img = cv::imread("lena2.jpg");
    QLabel label;
    label.setPixmap(QPixmap::fromImage(mat_to_qimage_ref(img)));
    label.show();
    @

    Don't know what is happening, something related to cycle dependency?

    1 Reply Last reply
    0

    • Login

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