当来自 OpenCV 的图片太大时 Qt 崩溃 [英] Qt crashes when picture from OpenCV is too large

查看:247
本文介绍了当来自 OpenCV 的图片太大时 Qt 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 OpenCV 来捕获相机提要,并在 Qt 中将其显示为 QLabel.按照我发现的指南,这在一定程度上有效.但是,如果我在制作 QImage 和设置 Pixmap 之前尝试调整 Mat 的大小,应用程序就会彻底崩溃.崩溃属于CameraSoftware.exe 已停止工作"类型,因此除了找出崩溃的位置之外,很难进行调试.

I've been trying to use OpenCV to capture a camera feed, and show it in Qt as a QLabel. Following a guide I found, this works to a certain degree. However, the application just outright crashes if I try to resize the Mat before making a QImage and setting the Pixmap. The crash is of the type "CameraSoftware.exe has stopped working", so it's hard to debug much other than finding out where it crashes.

环境是带有 QtCreator 3.6.0 (Qt 5.5.1) 和 OpenCV 3.10 的 64 位窗口.

Environment is 64 bit windows with QtCreator 3.6.0 (Qt 5.5.1) and OpenCV 3.10.

这里是代码的重要部分:

Here's the important bits of codes:

void VideoStreamOpenCVWorker::receiveGrabFrame()
{
    if(!toggleStream) return;

    (*cap) >> frameOriginal;
    if(frameOriginal.empty()) return;

    process();

    qDebug() << frameProcessed.cols << "x" << frameProcessed.rows;

    QImage output((const unsigned char *) frameProcessed.data, frameProcessed.cols, frameProcessed.rows, QImage::Format_RGBA8888);

    emit sendFrame(output);
}


void VideoStreamOpenCVWorker::process()
{
    cv::cvtColor(frameOriginal, frameProcessed, cv::COLOR_BGR2RGBA);
    cv::Size size(641,481);
    cv::resize(frameProcessed, frameProcessed, size);
}

这被发送回 QLabel 小部件:

This is sent back to a QLabel widget:

void VideoStreamWidget::receiveFrame(QImage frame){
    this->setPixmap(QPixmap::fromImage(frame));
}

这是它在setpixmap"行上特别崩溃的地方.

This is where it crashes specifically, on the "setpixmap" line.

Qlabel 被添加到 QMainWindow 中,只需:

The Qlabel is added to a QMainWindow with simply:

QVBoxLayout *pictureLayout = new QVBoxLayout;
VideoStreamWidget *video = new VideoStreamWidget();
pictureLayout->addWidget(video);

无论出于何种原因,原始上限图片为 640x480,我使用的相机是全高清相机.知道是什么原因造成的吗?Qt 窗口大小似乎无关紧要,因为我可以根据需要直接添加大图.我无法在不崩溃的情况下调整任何大小.

The original cap picture is 640x480 for whatever reason, the camera I'm using is a full HD camera. Any idea what's causing this? The Qt window size doesn't seem to matter, as I can add a large picture directly if I want to. I just can't resize anything without crashing.

如果我没有提供足够的信息,请询问.

If I haven't provided enough information, ask away.

我更新了调试器并在执行 setPixmap 行时收到此错误消息:

I updated my debugger and got this error message upon executing the setPixmap line:

The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x7ffe38fdadbb, code: 0xc0000005:
read access violation at: 0x0, flags=0x0 (first chance).

推荐答案

感谢 Mailerdaimon 提供答案.将输出作为副本发出

Thanks to Mailerdaimon for providing the answer. Emitting the output as a copy

emit sendFrame(output.copy());

可以解决问题.我仍然不确定为什么会发生这种情况以及为什么这可以解决问题.

does the trick. I am still unsure why exactly this is happening in the first place and why this fixes the problem.

这篇关于当来自 OpenCV 的图片太大时 Qt 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆