Ask Your Question

Segmentation fault when cv::Mat is constructed by Image buffer and not by VideoCapture::retreive

asked 2020-05-03 14:10:07 -0500

umbaman gravatar image

updated 2020-05-03 23:52:47 -0500

When I add to a buffer (a Qt QQueue) a cv::MAT that gets acquired using function

Mat grabbedFrame;

the image gets dequed and cloned just fine.

When I create a Mat using the constructor below

Mat PylonToOpenCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1, (uint8_t *) pylonImage.GetBuffer());

trying to deque and clone gives a segmentation fault....

But Using


both images can be viewed....

What could be wrong? What are The differences between the grabbedFrame variable ant the PylonToOpenCvImage one.

The seg fault seems to be here

Mat Mat::clone() const
    Mat m;
    return m;

at the copyTo(m) function from mat.inl.hpp. Although please don't take it for granted

Also sometimes I get the following

what(): /build/opencv-iC2m9y/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp:522: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat


edit retag flag offensive close merge delete


The "sometimes" errors are a clue. Your code fails sometimes because there are errors somewhere. I'd start putting checks for every function call, starting with verifying that pylonImage is valid, before doing anything with it. More often than not, programmers that use OpenCV fail to check their variables and operations in any way, resulting in very flaky code.

mvuori gravatar imagemvuori ( 2020-05-04 01:41:36 -0500 )edit

pylonImage is valid, because I can see the ouput with imshow() function Also I don't know how to check if two separate mat are equal, but not necessarily having the same image.... I don't know if that makes sense

umbaman gravatar imageumbaman ( 2020-05-04 02:08:59 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-05-06 01:15:07 -0500

berak gravatar image

if you use the Mat constructor with an external data pointer, no image data will get copied (a shallow copy), as soon as your pointer from pylon gets invalid, you have a "dangling pointer" inside the Mat

you should reverse the order of operations -- first clone() the Mat, then add it to the queue

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-05-03 14:10:07 -0500

Seen: 522 times

Last updated: May 06 '20