Qt-在工作线程崩溃中将cv :: Mat转换为QImage

时间:2018-07-18 18:06:21

标签: c++ multithreading qt qimage opencv-mat

简介

我想要的东西非常简单:我想开始通过主ui线程中的按钮读取二进制文件的过程,让一个单独的线程处理包括转换为QImage在内的处理并将该图像返回到应该在标签中显示的主ui线程。

因此,我使用Qt的信号/插槽机制及其线程功能。

我已经有一个单线程工作解决方案,但是当使用线程尝试时,它会崩溃在我不理解的完全任意步骤上,因为整个过程是完全封装的,并且不是很紧要时间...


有效的单线程解决方案:

TragVisMainQMainWindow

class TragVisMain : public QMainWindow 

按下按钮readBinSingle将启动该过程:

void TragVisMain::on_readBinSingle_clicked()
{
    // Open binary file
        FILE *imageBinFile = nullptr;
        imageBinFile = fopen("imageFile.bin", "rb");
        if(imageBinFile == NULL) {
            return;
        }

    // Get binary file size
        fseek(imageBinFile, 0, SEEK_END); // seek to end of file
        size_t size = static_cast<size_t>(ftell(imageBinFile)); // get current file pointer
        fseek(imageBinFile, 0, SEEK_SET);

    // Read binary file
        void *imageData = malloc(size);
        fread(imageData, 1, size, imageBinFile);

    // Create cv::Mat
        cv::Mat openCvImage(1024, 1280, CV_16UC1, imageData);
        openCvImage.convertTo(openCvImage, CV_8UC1, 0.04); // Convert to 8 Bit greyscale

    // Transform to QImage
        QImage qImage(
            openCvImage.data,
            1280,
            1024,
            QImage::Format_Grayscale8
        );

    // Show image in label, 'imageLabel' is class member
        imageLabel.setPixmap(QPixmap::fromImage(qImage));
        imageLabel.show();
}

这就像一个咒语,但是ui确实被阻止了。


无法使用多线程解决方案

您将看到代码与上面的基本相同,只是移至另一个类DoCameraStuff。因此,以下是TragVisMain头文件中用于此目的的主要组件:

namespace Ui {
    class TragVisMain;
}

class TragVisMain : public QMainWindow
{
        Q_OBJECT
    public:
        explicit TragVisMain(QWidget *parent = nullptr);
        ~TragVisMain();

    private:
        Ui::TragVisMain *ui;
        DoCameraStuff dcs;
        QLabel imageLabel;
        QThread workerThread;

    public slots:
        void setImage(const QImage &img); // Called after image processing
        // I have also tried normal parameter 'setImage(QImage img)', non-const referenc 'setImage(const QImage &img)' and pointer 'setImage(QImage *img)'

    private slots:
        void on_readBin_clicked(); // Emits 'loadBinaryImage'
        // void on_readBinSingle_clicked();

    signals:
        void loadBinaryImage(); // Starts image processing
};

DoCameraStuff仅仅是QObject

class DoCameraStuff : public QObject
{
        Q_OBJECT
    public:
        explicit DoCameraStuff(QObject *parent = nullptr);

    public slots:
        void readBinaryAndShowBinPic();

    signals:
        void showQImage(const QImage &image);

};

dcs的构造函数中发生将workerThread移动到connectTragVisMain信号和时隙的情况:

TragVisMain::TragVisMain(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::TragVisMain),
    dcs()
{
    ui->setupUi(this);
    dcs.moveToThread(&workerThread);

    // Object should be deletable after the thread finished!
    connect(&workerThread, &QThread::finished, &dcs, &QObject::deleteLater);

    // For starting the image processing
    connect(this, &TragVisMain::loadBinaryImage, &dcs, &DoCameraStuff::readBinaryAndShowBinPic);

    // Showing QImage after image processing is done
    connect(&dcs, &DoCameraStuff::showQImage, this, &TragVisMain::setImage);

    // Start workerThread
    workerThread.start();
}

通过按下readBinSingle按钮来开始图像处理:

void TragVisMain::on_readBin_clicked()
{
    emit loadBinaryImage(); // slot: readBinaryAndShowBinPic
}

该过程发生在DoCameraStuff::readBinaryAndShowBinPic中:

void DoCameraStuff::readBinaryAndShowBinPic() {
    // Exact same code from single thread solution:
        // Open binary file
            FILE *imageBinFile = nullptr;
            imageBinFile = fopen("imageFile.bin", "rb");
            if(imageBinFile == NULL) {
                return;
            }

        // Get binary file size
            fseek(imageBinFile, 0, SEEK_END); // seek to end of file
            size_t size = static_cast<size_t>(ftell(imageBinFile)); // get current file pointer
            fseek(imageBinFile, 0, SEEK_SET);

        // Read binary file
            void *imageData = malloc(size);
            fread(imageData, 1, size, imageBinFile);

        // Create cv::Mat
            cv::Mat openCvImage(1024, 1280, CV_16UC1, imageData);
            openCvImage.convertTo(openCvImage, CV_8UC1, 0.04); // Convert to 8 Bit greyscale

        // Transform to QImage
            QImage qImage(
                openCvImage.data,
                1280,
                1024,
                QImage::Format_Grayscale8
            );

    // Send qImage to 'TragVisMain'
    emit showQImage(qImage);
}

TragVisMain::setImage中显示图像:

void TragVisMain::setImage(const QImage &img)
{
    imageLabel.setPixmap(QPixmap::fromImage(img));
    imageLabel.show();
}

问题

多线程尝试只是使整个应用程序崩溃,而在不同步骤中没有任何消息。但老实说,我没有任何想法。对我来说,DoCameraStuff类是standard worker class类,它在成员函数中执行与时间无关的工作,而没有任何关键关系。

我还检查了DoCameraStuff::readBinaryAndShowBinPic中某些已使用的函数是否不是线程安全的,但是在<cstdio>cv::MatQImage中找不到任何问题。等效条件。

所以:

  1. 为什么多线程尝试会崩溃?
  2. 需要应用哪些更改,以便线程中的进程不会崩溃?

我一直感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

由于QImage包装了临时数据,然后cvMat对其进行了重新分配,因此它没有工作的机会。至少您应该发出图像的副本(直译为qImage.copy()。理想情况下,您将cvMat生命周期管理包装在QImage的删除器中,因此不需要复制,并且矩阵以及包裹它的图片都会被破坏。由于通常需要在cv::MatQImage之间进行格式转换,因此最好在源cv::Mat与另一个{ cv::Mat封装了QImage拥有的内存。该解决方案避免了内存重新分配,因为QImage可以保留在执行转换的类中,因此还可以与Qt 4兼容,其中QImage不支持删除器。

请参见this answer for a complete example of an OpenCV video capture Qt-based widget viewerthis answer for a complete example of multi-format conversion from cv::Mat to QImage