Ask Your Question
0

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

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

yrazin gravatar image

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

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;
cv::flip(imgcv,imgcv2,flipcode);

/*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
1

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

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

Comments

1

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

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

Perfect that worked- thank you!!

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

Question Tools

1 follower

Stats

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

Seen: 330 times

Last updated: Mar 16 '17