Converting OpenCV cv::Mat to QImage
In computer vision applications, it is often necessary to convert between different image formats. One common conversion is from OpenCV's cv::Mat to Qt's QImage format. OpenCV is a powerful computer vision library, while Qt is a cross-platform application framework.
Solution
To convert a cv::Mat to a QImage, simply use the following code:
QImage imgIn = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
This code takes the data from the cv::Mat and creates a new QImage object. The img.step parameter specifies the number of bytes per row in the cv::Mat.
However, it's important to note that the code provided by Michal Kottman may fail in certain cases. To ensure proper conversion, it is recommended to use the code with the img.step parameter included, as shown below:
QImage imgIn = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
The above is the detailed content of How to Convert an OpenCV cv::Mat to a QImage?. For more information, please follow other related articles on the PHP Chinese website!