convert cv::Mat into QPixmap
-
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" -
@JacobNovitsky said in convert cv::Mat into QPixmap:
I need simply convert cv::Mat into QPixmap
The code to create a
QPixmap
fromcv:Mat
looks correct.Where do you see the wayland error? This is caused by something else and not how you convert
Mat
toQPixmap
Edit:
Why do you create
QGuiApplication
and notQApplication
?QGuiApplication
might not work for everyQWidget
...
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); // . . . }
-
@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 notQApplication
as @Pl45m4 suggested. -
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. -
@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.