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
Release: 2024-11-28 20:32:12
Original
709 people have browsed it

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

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!

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