Convert Qt 6 Image to OpenCV Image

Convert Qt 6 Image to OpenCV Image

Qt and OpenCV are powerful libraries in the realm of software development, offering versatile tools for creating graphical user interfaces (GUIs) and computer vision applications. Sometimes, you might need to connect these two frameworks while dealing with images. This tutorial demonstrates how to convert Qt 6 image to OpenCV image.

The following code utilizes the Qt to load an image. The code then converts the image format to BGR888. The conversion is necessary because OpenCV commonly uses the BGR color format for its image representation, while Qt typically uses the ARGB format. Afterward, an Mat object is created, representing the image in OpenCV format, with dimensions matching the original image. Finally, the resulting OpenCV image is saved.

#include <QImage>
#include <opencv2/opencv.hpp>

int main()
{
    QImage qimg("test.jpg");
    qimg.convertTo(QImage::Format_BGR888);

    cv::Mat img(qimg.height(), qimg.width(), CV_8UC3, qimg.bits(), qimg.bytesPerLine());
    cv::imwrite("test2.jpg", img);

    return 0;
}

By using simple code, you can seamlessly integrate Qt and OpenCV in the applications, unlocking the potential to combine GUI development with advanced image processing capabilities.

Leave a Comment

Cancel reply

Your email address will not be published.