1

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).
17
  • 1
    Why it's hard to debug? Run your application under debugger. Aug 26, 2016 at 9:09
  • I know where it crashes, I just don't know how, why or what to do about it. The reason it's hard to debug is because I don't get an error message I can read out. The application just hardlocks, and one of those windows "thisapplication.exe has stopped working" instead of the usual error message you're suppose to have.
    – Excludos
    Aug 26, 2016 at 9:17
  • @Excludos make sure that frameProcessed has enough memory to store image.
    – Nikita
    Aug 26, 2016 at 10:40
  • 1
    @Excludos try to create QImage with operator new (allocate on heap)and send pointer to slot.
    – Nikita
    Aug 26, 2016 at 13:22
  • 2
    Or try an explicit copy: emit sendFrame(output.copy()); Aug 26, 2016 at 13:25

1 Answer 1

2

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.

3
  • 2
    It happens because the QImage wraps a frame whose lifetime and contents aren't bound to the lifetime of QImage. The source frame might reallocate or be destroyed while the QImage still refers to the data. Aug 26, 2016 at 18:02
  • That sounds like a decent explanation if not for the fact that it works if you don't resize it.
    – Excludos
    Aug 29, 2016 at 8:00
  • 1
    Thanks it solved the problem !!! Dec 5, 2019 at 21:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.