c++ - OpenCV 错误 : Assertion failed in matrix. cpp 第 522 行,/matrix.cpp:522:错误:(-215)

标签 c++ opencv

我正在尝试在检测到的脸部上方创建一个 ROI,以放置帽子,如图所示:请单击此处: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 Error: Assertion failed (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 Mat, file /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/ma‌​trix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/ma‌​trix.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 Aborted (core dumped)

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

最佳答案

该错误意味着您的 ROI 位于图像之外,因此您的条件错误。

由于很容易混淆,我通常会应用这个基于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
}

关于c++ - OpenCV 错误 : Assertion failed in matrix. cpp 第 522 行,/matrix.cpp:522:错误:(-215),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43141613/

相关文章:

c - 如何生成cvblob.lib?

c++ - 初始化列表中 const 引用成员的初始化

c++ - 在 cmake 中启用 Qspectre 和 Control Flow Guard 开关

opencv - 与 plt.show 一起使用时 cv2.imshow 不起作用

c++ - 在 64 位 mac 上编译 OpenCV 代码

python - 使用 opencv/Numpy 使用 python 在彩色图像中查找白色像素

opencv - 如何使用 OpenCV 3 训练 SVM 进行 "Euro"硬币识别?

c++ - 在c程序中使用c++方法

c++ - 奇怪的双指针空检查

c++ - 如何链接 ENet 和 MSVC