Ask Your Question

cv::flip keeps throwing bad ptr exceptions [closed]

asked 2017-03-16 10:48:43 -0500

yrazin gravatar image

updated 2017-03-16 11:02:58 -0500

berak gravatar image

No matter which flip code I call with cv::flip, I keep getting bad ptr exceptions. For flipcode <1, it errors out at flipVert (copy.cpp, l.418) on src1. const uchar* src1 is defined at l.405 with a simple equation, not sure why that should be an issue. For flipcode==1, the error is in flipHoriz (copy.cpp, l.395).

I am calling cv::flip(imgcv,imgcv2,flipcode) after I convert a QImage into a Mat using:

Mat imgcv = QImage2Mat(img);
Mat imgcv2;

/*static*/ Mat DisplayVideoQt::QImage2Mat(QImage const& src)
    QImage temp = src.copy();
    Mat res(temp.height(),temp.width(),CV_8UC3,(uchar*)temp.bits(),temp.bytesPerLine());
    cvtColor(res,res, CV_BGR2RGB);
    return res;
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by yrazin
close date 2017-03-16 11:39:35.286374

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-16 11:09:43 -0500

berak gravatar image

please clone() your Mat before leaving DisplayVideoQt::QImage2Mat :

    return res.clone();

the Mat constructor you're using won't copy pixels (only the pointer), also cvtColor won't make a new Mat (same size/channel count), so, right now, you have a "dangling pointer", when you leave the function.

edit flag offensive delete link more



ps: imho, you can skip the src.copy()

berak gravatar imageberak ( 2017-03-16 11:31:58 -0500 )edit

Perfect that worked- thank you!!

yrazin gravatar imageyrazin ( 2017-03-16 11:39:25 -0500 )edit

Question Tools

1 follower


Asked: 2017-03-16 10:48:43 -0500

Seen: 63 times

Last updated: Mar 16 '17