0

我正在制作一个 Qt GUI 应用程序,它使用自定义QLabel类(名称为ImageInteraction)来显示来自流式摄像机的图像,同时还允许鼠标在图像上交互。由于 GUI 具有其他功能,定制的 QLabel 类完成从相机中提取图像并通过while在另一个线程中运行的函数中的循环更新其显示的图像的工作。代码如下:

void ImageInteraction::startVideo()
{
    if (!capture.open(streamUrl))
    {
        QMessageBox::warning(this, "Error", "No input device availabe!");
    }
    else
    {
        QFuture<void> multiprocess = QtConcurrent::run(this, &ImageInteraction::loadVideo);
    }
}

void ImageInteraction::loadVideo()
{
    while(loopContinue){
    cv::Mat frame;
    capture.read(frame);
    if(!frame.empty())
    {
        cv::cvtColor(frame, frame, CV_BGR2RGBA);
        cv::resize(frame, frame, cv::Size(this->width(), this->height()), 0, 0);
        QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGBA8888);
                this->setPixmap(QPixmap::fromImage(image));
            }
    }
    capture.release();
 }

Here captureis of typecv::VideoCapture并且loopContinue是一个布尔类型,最初设置为true. 有一个closeEvent()函数调用停止从相机捕获图像的方法。

void MainWindow::closeEvent(QCloseEvent *event)
{
    liveVideo->stopVideoThread();//liveVideo is a pointer to an object of ImageInteraction
    event->accept();
}

其中stopVideoThread只需将布尔标志设置loopContinuefalse并具有以下简单代码:

void ImageInteraction::stopVideoThread()
{
    mutex.lock();//QMutex mutex;
    loopContinue = false;
    mutex.unlock();
}

据我了解,一旦方法被调用并设置为 false while,方法中的循环loadVideo应该停止。但实际上,当按下关闭按钮时,显然它不会停止循环并且应用程序崩溃并显示一条消息:stopVideoThreadloopContinuewhile

The inferior stopped because it received a signal from the operating system.

Signal name : SIGSEGV
Signal meaning : Segmentation fault

我是否错误地使用QtConcurrent::run了方法和QMutex对象?你能确定问题是什么吗?仅供参考,操作系统是 ubuntu 14.04,IDE 是 QtCreator。

谢谢!

4

1 回答 1

1

以下只是上述评论中提到的改进的一个想法。

class ImageInteraction
{
public:
    ~ImageInteraction()
    {
        multiprocess_.waitForFinished();
    }

    void startVideo()
    {
        if (!capture.open(streamUrl))
        {
            QMessageBox::warning(this, "Error", "No input device availabe!");
        }
        else
        {
            multiprocess_ = QtConcurrent::run(this, &ImageInteraction::loadVideo);
        }
    }

    void loadVideo()
    {
        while(loopContinue_)
        {
            cv::Mat frame;
            capture.read(frame);
            if(!frame.empty())
            {
                cv::cvtColor(frame, frame, CV_BGR2RGBA);
                cv::resize(frame, frame, cv::Size(this->width(), this->height()), 0, 0);
                QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGBA8888);
                this->setPixmap(QPixmap::fromImage(image));
            }
        }
        capture.release();
    }

    void stopVideoThread()
    {
        loopContinue_ = false;
        //multiprocess_.waitForFinished(); // you can call this here if you want to make sure that the thread has finished before returning
    }

private:
    QFuture<void> multiprocess_;
    std::atomic<bool> loopContinue_;
};
于 2016-02-11T16:37:33.707 回答