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.
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);
}
This is sent back to a QLabel widget:
void VideoStreamWidget::receiveFrame(QImage frame){
this->setPixmap(QPixmap::fromImage(frame));
}
This is where it crashes specifically, on the "setpixmap" line.
The Qlabel is added to a QMainWindow with simply:
QVBoxLayout *pictureLayout = new QVBoxLayout;
VideoStreamWidget *video = new VideoStreamWidget();
pictureLayout->addWidget(video);
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.
EDIT: 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).