OpenCV错误:在matrix.cpp第522行中断言失败,/ matrix.cpp:522:错误:( - 215)

时间:2017-03-31 13:12:00

标签: c++ opencv

我正在尝试在检测到的脸部上方创建一个投资回报率,如图所示放置一个帽子:Plz点击此处:ROI created above face to place a hat

我确保创建的ROI与图像的界限有关。它看起来像这样:  //创建投资回报率  //其中face是检测到的面部ROI

    if (0<=face.x && 0<=face.x-face.width*0.08<=image.cols && 0<=face.x+face.width+face.width*0.08<=image.cols 
         && 0<=face.y && 0<=face.y-face.height*0.28<=image.rows)
    {
      Mat ROI_hat = image(Rect(abs(face.x-face.width*0.08),abs(face.y-face.height*0.28),abs(face.x+face.width+face.width*0.08),abs(face.y)));
      rectangle(image,Point(abs(face.x-face.width*0.08),abs(face.y-face.height*0.28)),Point(abs(face.x+face.width+face.width*0.08),abs(face.y)),Scalar(255, 0, 0), 1, 4);

      cout<<"Within the bounds of Image"<<endl;
    }
    else{
     cout<<" Out of bounds of Image "<<endl;
        }

没有负值,并且对于每个帧,它表示ROI正在接受边界。但我仍然得到断言错误:

  

OpenCV错误:断言失败(0&lt; = roi.x&amp;&amp; 0&lt; = roi.width&amp;&amp; roi.x + roi.width&lt; = m.cols&amp;&amp; 0&lt; ; = roi.y&amp;&amp; 0&lt; = roi.height&amp;&amp; roi.y + roi.height&lt; = m.rows)在Mat中,文件/home/user/OpenCV_Installed/opencv-3.2.0 / modules / core / src / ma trix.cpp,第522行在抛出&#39; cv :: Exception&#39;的实例后终止调用what():/ home / user / OpenCV_Installed / opencv-3.2.0 / modules / core / src / ma trix.cpp:522:error:( - 1515)0&lt; = roi.x&amp;&amp; 0&lt; = roi.width&amp;&amp; roi.x + roi.width&lt; = m.cols&amp;&amp; 0&lt; = roi.y&amp;&amp; 0&lt; = roi.height&amp;&amp; roi.y + roi.height&lt; = m.rows in function Mat Aborted(core dumped)

有人可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:2)

错误表示您的投资回报率不在图片范围内,因此您的条件有误。

由于很容易混淆,我通常应用这个小技巧,该技巧基于roi虚拟 roi roiImg的交集,其中包含所有图像:

Rect roiImg(0, 0, image.cols, image.rows);
Rect roi = ... // Very complex way of setting up the ROI

if( (roi.area() > 0) && ((roiImg & roi).area() == roi.area()) ) {
    // roi is inside the image, and is non-empty
    // VALID roi
} else {
    // roi is at least partially outside of the image, or it's empty
    // INVALID roi
}