How to efficiently display OpenCV video in Qt? (1个答案)
4年前关闭。
我一直在尝试使用OpenCV捕获相机提要,并将其在Qt中显示为
QLabel
。按照我发现的指南,此方法在一定程度上可行。但是,如果我在制作
QImage
和设置Pixmap之前尝试调整Mat的大小,则应用程序将完全崩溃。崩溃的类型为“CameraSoftware.exe已停止工作”,因此除了找出崩溃的位置之外,很难进行其他调试。
环境是使用QtCreator 3.6.0(Qt 5.5.1)和OpenCV 3.10的64位窗口。
以下是重要的代码位:
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小部件:
void VideoStreamWidget::receiveFrame(QImage frame){
this->setPixmap(QPixmap::fromImage(frame));
}
特别是在“setpixmap”行上,这是崩溃的地方。
Qlabel通过以下简单方式添加到QMainWindow中:
QVBoxLayout *pictureLayout = new QVBoxLayout;
VideoStreamWidget *video = new VideoStreamWidget();
pictureLayout->addWidget(video);
无论出于何种原因,原始的封面图片均为640x480,我使用的摄像机是全高清摄像机。知道是什么原因造成的吗? Qt窗口的大小似乎无关紧要,因为我可以根据需要直接添加大图片。我只是无法调整大小而不会崩溃。
如果我没有提供足够的信息,请询问。
编辑:我更新了调试器,并在执行setPixmap行时收到此错误消息:
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提供的答案。将输出作为副本发出
emit sendFrame(output.copy());
绝招。我仍然不确定为什么首先会发生这种情况,以及为什么会解决此问题。