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
Release: 2024-12-03 12:17:10
Original
662 people have browsed it

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);
Copy after login
Copy after login

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);
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template