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

How to Convert a cv::Mat to a QImage in C ?

Susan Sarandon
Susan SarandonOriginal
2024-11-28 20:32:12810browse

How to Convert a cv::Mat to a QImage in C  ?

How to Convert OpenCV cv::Mat to QImage

Problem:

How can I convert an OpenCV cv::Mat type to QImage in C ? Converting IPlimage to QImage using existing code is not sufficient.

Solution:

Michal Kottman's response is generally accurate, but it may fail for certain images. To address this issue, you can use the following solution:

QImage imgIn = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);

Explanation:

The key difference in this approach is the addition of img.step, which specifies the byte width of each row in the cv::Mat. Without this parameter, Qt may not always render images correctly.

By including img.step, you can ensure compatibility with a broader range of images and prevent rendering issues.

The above is the detailed content of How to Convert a cv::Mat to a QImage in C ?. 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