This article aims to help you convert QImage to cv::Mat format in your Qt application, leveraging OpenCV's image processing capabilities. We'll integrate OpenCV and Qt to process video frames and display them using a label.

Conversion Process

1. Include Necessary Headers

In your main function, add the OpenCV header:

#include <opencv2/opencv.hpp>

Within the VideoProcessor class, include the QPixmap header:

#include <QPixmap>

2. Add a Private Member Variable

Declare a private member variable in the VideoProcessor class:

QPixmap m_pixmap;

3. Modify the processFrame Function

Update the VideoProcessor class's processFrame function as follows:

void VideoProcessor::processFrame(int requestId, const QImage& img)
{
    cv::Mat dstImage;
    
    // Convert QImage to cv::Mat
    if (img.format() == QImage::Format_RGB888) {
        dstImage = cv::Mat(img.height(), img.width(), CV_8UC3, const_cast<uchar*>(img.bits()), img.bytesPerLine()).clone();
    } else {
        dstImage = cv::Mat(img.height(), img.width(), CV_8UC1, const_cast<uchar*>(img.bits()), img.bytesPerLine()).clone();
    }
    
    // Perform image processing operations on dstImage
    // ...

    // Convert cv::Mat back to QImage
    QImage processedImage(dstImage.data, dstImage.cols, dstImage.rows, static_cast<int>(dstImage.step), QImage::Format_RGB888);

    // Convert QImage to QPixmap
    m_pixmap = QPixmap::fromImage(processedImage);

    emit frameProcessed(processedImage);
}

4. Modify the frameProcessed Slot Function

Update the frameProcessed slot function in the main function to display the QPixmap on a label:

QObject::connect(&videoProcessor, &VideoProcessor::frameProcessed, [](const QImage& frame) {
    QPixmap pixmap = videoProcessor.getPixMap();
    show_onlabel(pixmap);
});

5. Add a Public Member Function

In the VideoProcessor class, add a public member function to retrieve the value of the m_pixmap private member variable:

QPixmap VideoProcessor::getPixMap() const
{
    return m_pixmap;
}

6. Define the show_onlabel Function

In the main function, create the show_onlabel function to display the QPixmap on a label. Replace the placeholder comment with your label's implementation:

void show_onlabel(const QPixmap& pixmap)
{
    // Display QPixmap on your label
    // ...
}

Conclusion

By following these steps, you've successfully converted QImage to cv::Mat, enabling you to use OpenCV for advanced image processing within your Qt application. The provided code demonstrates how to process video frames from a camera, apply image transformations, and display the results on a label. Remember to replace the show_onlabel function's placeholder with your specific label implementation.

QImage to cv::Mat:  Streamline Your Image Processing with OpenCV and Qt

原文地址: https://www.cveoy.top/t/topic/pPMc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录