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 cv::Mat into QPixmap

convert cv::Mat into QPixmap

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 839 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.
  • J Offline
    J Offline
    JacobNovitsky
    wrote on 30 Dec 2024, 18:12 last edited by
    #1

    I need simply convert cv::Mat into QPixmap
    My first attempt for two last days led me to forum :)
    Any pointers are highly appreciated!

    pro.file

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++17
    
    SOURCES += main.cpp
        
    INCLUDEPATH += /usr/local/Qt-6.4.2/include
    LIBS += -L/usr/local/Qt-6.4.2/lib -lQt6Core -lQt6Gui
    
    # OpenCV
    INCLUDEPATH += /usr/include/opencv4
    LIBS += `pkg-config --cflags --libs opencv4`
    
    # Remove redundant OpenCV libraries and make sure only pkg-config handles them
    
    HEADERS +=
    

    code:

    #include <opencv2/opencv.hpp>
    #include <QPixmap>
    #include <QImage>
    #include <QLabel>
    #include <QGuiApplication>  // For QGuiApplication
    #include <QApplication>  // For QGuiApplication
    
    QPixmap MatToQPixmap(const cv::Mat &image) {
        // Check if image is empty
        if (image.empty()) {
            std::cerr << "Empty image" << std::endl;
            return QPixmap();
        }
    
        // Convert the cv::Mat to QImage
        QImage qimage;
    
        // For color images (CV_8UC3, RGB format)
        if (image.type() == CV_8UC3) {
            cv::Mat rgb;
            cv::cvtColor(image, rgb, cv::COLOR_BGR2RGB); // Convert BGR to RGB
            qimage = QImage(rgb.data, rgb.cols, rgb.rows, static_cast<int>(rgb.step), QImage::Format_RGB888);
        }
        // For grayscale images (CV_8UC1)
        else if (image.type() == CV_8UC1) {
            qimage = QImage(image.data, image.cols, image.rows, static_cast<int>(image.step), QImage::Format_Grayscale8);
        }
        // Handle other types if needed (e.g., CV_8UC4, CV_16UC3, etc.)
        else {
            std::cerr << "Unsupported image format" << std::endl;
            return QPixmap();
        }
    
        // Convert QImage to QPixmap
        QPixmap pixmap = QPixmap::fromImage(qimage);
    
        // Check if pixmap conversion is successful
        if (pixmap.isNull()) {
            std::cerr << "Failed to convert cv::Mat to QPixmap." << std::endl;
        }
    
        return pixmap;
    }
    
    int main(int argc, char *argv[]) {
        // Initialize QGuiApplication or QApplication (needed for QPixmap)
        QGuiApplication app(argc, argv);
    
        // // Load the image (example)
        cv::Mat image = cv::imread("/home/j/images/1.png");  // Replace with your image path
    
        // // Convert to QPixmap
        QPixmap pixmap = MatToQPixmap(image);
    
        // // Display the image using QLabel
        QLabel label;
        label.setPixmap(pixmap);
        label.show();
    
        // Enter the application's event loop
        return app.exec();
    }
    

    qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "" (Jacob: I consider its just warning, nothing fatal here, I see it usually, am I correct?)
    qtc.process_stub: Inferior error: QProcess::Crashed "Process crashed"

    P I 2 Replies Last reply 30 Dec 2024, 18:25
    0
    • J JacobNovitsky
      30 Dec 2024, 18:12

      I need simply convert cv::Mat into QPixmap
      My first attempt for two last days led me to forum :)
      Any pointers are highly appreciated!

      pro.file

      QT       += core gui
      
      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
      
      CONFIG += c++17
      
      SOURCES += main.cpp
          
      INCLUDEPATH += /usr/local/Qt-6.4.2/include
      LIBS += -L/usr/local/Qt-6.4.2/lib -lQt6Core -lQt6Gui
      
      # OpenCV
      INCLUDEPATH += /usr/include/opencv4
      LIBS += `pkg-config --cflags --libs opencv4`
      
      # Remove redundant OpenCV libraries and make sure only pkg-config handles them
      
      HEADERS +=
      

      code:

      #include <opencv2/opencv.hpp>
      #include <QPixmap>
      #include <QImage>
      #include <QLabel>
      #include <QGuiApplication>  // For QGuiApplication
      #include <QApplication>  // For QGuiApplication
      
      QPixmap MatToQPixmap(const cv::Mat &image) {
          // Check if image is empty
          if (image.empty()) {
              std::cerr << "Empty image" << std::endl;
              return QPixmap();
          }
      
          // Convert the cv::Mat to QImage
          QImage qimage;
      
          // For color images (CV_8UC3, RGB format)
          if (image.type() == CV_8UC3) {
              cv::Mat rgb;
              cv::cvtColor(image, rgb, cv::COLOR_BGR2RGB); // Convert BGR to RGB
              qimage = QImage(rgb.data, rgb.cols, rgb.rows, static_cast<int>(rgb.step), QImage::Format_RGB888);
          }
          // For grayscale images (CV_8UC1)
          else if (image.type() == CV_8UC1) {
              qimage = QImage(image.data, image.cols, image.rows, static_cast<int>(image.step), QImage::Format_Grayscale8);
          }
          // Handle other types if needed (e.g., CV_8UC4, CV_16UC3, etc.)
          else {
              std::cerr << "Unsupported image format" << std::endl;
              return QPixmap();
          }
      
          // Convert QImage to QPixmap
          QPixmap pixmap = QPixmap::fromImage(qimage);
      
          // Check if pixmap conversion is successful
          if (pixmap.isNull()) {
              std::cerr << "Failed to convert cv::Mat to QPixmap." << std::endl;
          }
      
          return pixmap;
      }
      
      int main(int argc, char *argv[]) {
          // Initialize QGuiApplication or QApplication (needed for QPixmap)
          QGuiApplication app(argc, argv);
      
          // // Load the image (example)
          cv::Mat image = cv::imread("/home/j/images/1.png");  // Replace with your image path
      
          // // Convert to QPixmap
          QPixmap pixmap = MatToQPixmap(image);
      
          // // Display the image using QLabel
          QLabel label;
          label.setPixmap(pixmap);
          label.show();
      
          // Enter the application's event loop
          return app.exec();
      }
      

      qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "" (Jacob: I consider its just warning, nothing fatal here, I see it usually, am I correct?)
      qtc.process_stub: Inferior error: QProcess::Crashed "Process crashed"

      P Offline
      P Offline
      Pl45m4
      wrote on 30 Dec 2024, 18:25 last edited by Pl45m4
      #2

      @JacobNovitsky said in convert cv::Mat into QPixmap:

      I need simply convert cv::Mat into QPixmap

      The code to create a QPixmap from cv:Mat looks correct.

      Where do you see the wayland error? This is caused by something else and not how you convert Mat to QPixmap

      Edit:

      Why do you create QGuiApplication and not QApplication?

      QGuiApplication might not work for every QWidget...
      And why include both anyway?!

      #include <QGuiApplication>  // For QGuiApplication
      #include <QApplication>  // For QGuiApplication
      

      Change to

      int main(int argc, char *argv[]) {
          // Initialize QGuiApplication or QApplication (needed for QPixmap)
          QApplication app(argc, argv);
          // . . . 
      }
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      J 1 Reply Last reply 31 Dec 2024, 07:02
      1
      • J JacobNovitsky
        30 Dec 2024, 18:12

        I need simply convert cv::Mat into QPixmap
        My first attempt for two last days led me to forum :)
        Any pointers are highly appreciated!

        pro.file

        QT       += core gui
        
        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
        
        CONFIG += c++17
        
        SOURCES += main.cpp
            
        INCLUDEPATH += /usr/local/Qt-6.4.2/include
        LIBS += -L/usr/local/Qt-6.4.2/lib -lQt6Core -lQt6Gui
        
        # OpenCV
        INCLUDEPATH += /usr/include/opencv4
        LIBS += `pkg-config --cflags --libs opencv4`
        
        # Remove redundant OpenCV libraries and make sure only pkg-config handles them
        
        HEADERS +=
        

        code:

        #include <opencv2/opencv.hpp>
        #include <QPixmap>
        #include <QImage>
        #include <QLabel>
        #include <QGuiApplication>  // For QGuiApplication
        #include <QApplication>  // For QGuiApplication
        
        QPixmap MatToQPixmap(const cv::Mat &image) {
            // Check if image is empty
            if (image.empty()) {
                std::cerr << "Empty image" << std::endl;
                return QPixmap();
            }
        
            // Convert the cv::Mat to QImage
            QImage qimage;
        
            // For color images (CV_8UC3, RGB format)
            if (image.type() == CV_8UC3) {
                cv::Mat rgb;
                cv::cvtColor(image, rgb, cv::COLOR_BGR2RGB); // Convert BGR to RGB
                qimage = QImage(rgb.data, rgb.cols, rgb.rows, static_cast<int>(rgb.step), QImage::Format_RGB888);
            }
            // For grayscale images (CV_8UC1)
            else if (image.type() == CV_8UC1) {
                qimage = QImage(image.data, image.cols, image.rows, static_cast<int>(image.step), QImage::Format_Grayscale8);
            }
            // Handle other types if needed (e.g., CV_8UC4, CV_16UC3, etc.)
            else {
                std::cerr << "Unsupported image format" << std::endl;
                return QPixmap();
            }
        
            // Convert QImage to QPixmap
            QPixmap pixmap = QPixmap::fromImage(qimage);
        
            // Check if pixmap conversion is successful
            if (pixmap.isNull()) {
                std::cerr << "Failed to convert cv::Mat to QPixmap." << std::endl;
            }
        
            return pixmap;
        }
        
        int main(int argc, char *argv[]) {
            // Initialize QGuiApplication or QApplication (needed for QPixmap)
            QGuiApplication app(argc, argv);
        
            // // Load the image (example)
            cv::Mat image = cv::imread("/home/j/images/1.png");  // Replace with your image path
        
            // // Convert to QPixmap
            QPixmap pixmap = MatToQPixmap(image);
        
            // // Display the image using QLabel
            QLabel label;
            label.setPixmap(pixmap);
            label.show();
        
            // Enter the application's event loop
            return app.exec();
        }
        

        qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "" (Jacob: I consider its just warning, nothing fatal here, I see it usually, am I correct?)
        qtc.process_stub: Inferior error: QProcess::Crashed "Process crashed"

        I Offline
        I Offline
        IgKh
        wrote on 30 Dec 2024, 22:02 last edited by
        #3

        @JacobNovitsky said in convert cv::Mat into QPixmap:

        qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "" (Jacob: I consider its just warning, nothing fatal here, I see it usually, am I correct?)

        This is of no concern. It means that you are running under a Wayland compositor but don't have the Qt wayland plugin installed. That's fine, it will just fallback to working with Xwayland through the xcb plugin.

        @JacobNovitsky said in convert cv::Mat into QPixmap:

        qtc.process_stub: Inferior error: QProcess::Crashed "Process crashed"

        This is of great concern. Run your project under the debugger and look at the resulting full stack trace to see where exactly it crashed. It could likely indeed be due to trying to create a QLabel widget instance when the application instance is not QApplication as @Pl45m4 suggested.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on 31 Dec 2024, 02:01 last edited by Bonnie
          #4

          I used to find this project very helpful: https://github.com/dbzhang800/QtOpenCV
          It converts between cv::Mat and QImage for a lot of pixel formats, but it is quite old and I'm not sure whether the code would work for Qt6 and the latest opencv.

          1 Reply Last reply
          1
          • P Pl45m4
            30 Dec 2024, 18:25

            @JacobNovitsky said in convert cv::Mat into QPixmap:

            I need simply convert cv::Mat into QPixmap

            The code to create a QPixmap from cv:Mat looks correct.

            Where do you see the wayland error? This is caused by something else and not how you convert Mat to QPixmap

            Edit:

            Why do you create QGuiApplication and not QApplication?

            QGuiApplication might not work for every QWidget...
            And why include both anyway?!

            #include <QGuiApplication>  // For QGuiApplication
            #include <QApplication>  // For QGuiApplication
            

            Change to

            int main(int argc, char *argv[]) {
                // Initialize QGuiApplication or QApplication (needed for QPixmap)
                QApplication app(argc, argv);
                // . . . 
            }
            
            J Offline
            J Offline
            JacobNovitsky
            wrote on 31 Dec 2024, 07:02 last edited by
            #5

            @Pl45m4 this worked for me, thanks

            1 Reply Last reply
            0
            • BondrusiekB Offline
              BondrusiekB Offline
              Bondrusiek
              wrote on 1 Jan 2025, 21:04 last edited by
              #6

              @JacobNovitsky hi,
              you can see also this article: https://asmaloney.com/2013/11/code/converting-between-cvmat-and-qimage-or-qpixmap/ maybe it helps you.

              1 Reply Last reply
              0

              1/6

              30 Dec 2024, 18:12

              • Login

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