OpenCv断言失败 [英] OpenCv assertion failed

查看:185
本文介绍了OpenCv断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在2D图像中寻找脸部的应用程序,后来在同一个图像里面我想找到嘴,但我现在有一些问题。这是我的代码到目前为止:

I am working on application for finding face in 2D image and later inside same image I want to find mouth, but I have some problem right now. This is my code so far:

for (int i = 0; i < faces.size(); i++)
{
        Point pt1(faces[i].x, faces[i].y);
        Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
        rectangle(frame, pt1, pt2, Scalar(255,0 , 0), 2, 8, 0);

                    //I WANT ROI(FOR MOUTH DETECTION) TO BE ONLY HALF OF THE RECTANGLE WITH FACE
        Rect mouthROI;
        mouthROI.x = (faces[i].x);
        mouthROI.y = faces[i].y*(1.5);
        mouthROI.width = (faces[i].x + faces[i].height);
        mouthROI.height = (faces[i].y + faces[i].width);

                    //I CHECK IF NEW RECTANGLE IS EXACTLY BOTTOM HALF OF PREVIOUS ONE
        Point ptAA(mouthROI.x, mouthROI.y);
        Point ptBB(mouthROI.width, mouthROI.height);
        rectangle(frame, ptBB, ptAA, Scalar(0,0 , 255), 2, 2, 0);


        Mat image_roi = frame(mouthROI);

        cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
        equalizeHist( frame_gray, frame_gray );

                    // DETECTING MOUTH INSIDE ROI OF EARLIER DETECTED FACE
        mouth_cascade.detectMultiScale(image_roi, mouths, 1.1, 2, 0, Size(30, 30));

        for(int i = 0; i < mouths.size(); i++)
        {
            Point pt1(mouths[i].x, mouths[i].y); // Display detected faces on main window - live stream from camera
            Point pt2((mouths[i].x + mouths[i].height), (mouths[i].y + mouths[i].width));
            rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
        }

}

不幸的是,此代码不工作。我得到这样的错误:

Unfortunately this code does not work. I get error like this:

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 cv::Mat::Mat(const cv::Mat&, const cv::Rect&), file C:/build/2_4_PackSlave-win32-vc11-shared/opencv/modules/core/src/matrix.cpp, line 323

如何修复此错误。

推荐答案

您初始化的方式 mouthROI 是不正确的。应该是这样的,

The way you have initialized mouthROI is incorrect. It should be like this instead,

<previous code>
mouthROI.width = (faces[i].height);
mouthROI.height = (faces[i].width);

Point ptAA(mouthROI.x, mouthROI.y);
Point ptBB(mouthROI.x+mouthROI.width, mouthROI.y+mouthROI.height);

请记住,cv :: Rect不会使用 position 矩形,它采用左上角的位置和宽度高度。请参阅文档了解详情。

Remember that cv::Rect doesn't take the position of the rectangle, it takes the position of the upper left corner and the width and the height.. Refer to the documentation for further details.

HTH

这篇关于OpenCv断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆