Home >Backend Development >C++ >How to Convert an OpenCV cv::Mat to a QImage?

How to Convert an OpenCV cv::Mat to a QImage?

Barbara Streisand
Barbara StreisandOriginal
2024-12-03 12:17:10631browse

How to Convert an OpenCV cv::Mat to a QImage?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn