OpenCV VideoCapture Failure from QThread
-
Hi,
@rtavakko said in OpenCV VideoCapture Failure from QThread:
void Display::updateImage(const uchar *imageData, int width, int height, int step)
{
if(imageData != nullptr)
{
displayImage = QImage(imageData, width, height, step, QImage::Format_RGBA8888);
update();
}
}The constructor you are using does not make a copy of the data and you have to ensure that the lifetime of the buffer pointed to by imageData stays valid as long as the displayImage is.
From the looks of it, you should either call copy so that displayImage doesn't depend on the lifetime of imageData or create imageData once with the right dimensions and use memcpy to transfer the data. Re-create displayImage if the size changes at some point.
-
@JKSH This is how I emit the OpenCV mat data:
emit processedImage(2, videoFrameOut.ptr(0,0),videoFrameOut.cols, videoFrameOut.rows, videoFrameOut.step);
The first parameter is just used to determine which display should draw the image and the rest are dimensions and stride which I think are ok.
@SGaist You are correct, I changed the line below to make a copy:
displayImage = QImage(imageData, width, height, step, QImage::Format_RGBA8888).copy(0,0,width,height);
I think this fixed the issue. I got a couple of crashes in 'memcpy' but that was when loading files a little too quickly. I think most likey resizing the data quickly was the issue. I will do more testing this week and post the results.
UPDATE: I did a lot more testing and making a local copy fixed the issue. The issue was that I was modifying the array before it was fully copied and this caused crashes when the array was downsized. I will need to revise the overall structure to make sure everything is done sequentially.
Thanks!