【问题标题】:OpenCV c++ assertion failed <i < 0> in cv::_InputArray::getMatOpenCV c++ 断言在 cv::_InputArray::getMat 中失败 <i < 0>
【发布时间】:2015-05-20 08:25:12
【问题描述】:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <iostream>
#include <windows.h>

using namespace cv;
using namespace std;

//initial min and max HSV filter values.
//these will be changed using trackbars    


Mat src; Mat HSV; Mat roi; Mat range; Mat eroded; Mat gray;
int thresh = 100;
int max_thresh = 255;

/** @function main */
int main(int argc, char** argv)
{
    createTrackbars();
    VideoCapture cap(0); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
        return -1;


    namedWindow("background", 1);

    int waitTime = 50;
    int counter = 101;

    int roiLeft = 20;
    int roiTop = 50;
    int roiRight = 200;
    int roiBottom = 200;
    Rect rRoi = Rect(roiLeft, roiTop, roiRight, roiBottom);

    Mat background;
    cap >> background;
    background = background(rRoi);
    //cvtColor(background, background, CV_BGR2HSV);

    //imshow("background", background);


    vector<vector<Point> > contours;
    vector<vector < cv::Point >> hull(1);
    vector<Vec4i> hierarchy;
    vector<CvConvexityDefect> defects;

    while (true)
    {
        cap >> src;

        //Create the region of interest.
        Mat iRoi = src.clone()(rRoi);
        Mat iRoiSRC = src(rRoi);

        //Draw a rectangle there.
        rectangle(src, rRoi, Scalar(255, 128, 0), 1, 8, 0);
        //imshow("roi", iRoi);

        //Subtract the static background.
        absdiff(iRoi, background, iRoi);
        //imshow("diff", iRoi);

        //Convert it to a GrayScale and threshold it.
        cvtColor(iRoi, iRoi, CV_BGR2GRAY);
        threshold(iRoi, gray, 15, 255, CV_THRESH_BINARY);

        //Perform a closing.
        Mat erodeElement = getStructuringElement(MORPH_ELLIPSE, Size(erodeSize, erodeSize));
        Mat dilateElement = getStructuringElement(MORPH_ELLIPSE, Size(dilateSize, dilateSize));
        for (int index = 0; index < loopAmount; index++)
        {
            erode(gray, gray, erodeElement);
            dilate(gray, gray, dilateElement);
        }
        //imshow("range", gray);


        //Find the contours.
        findContours(gray, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

        //Pick the biggest contour.
        int biggestContourIndex = 0;
        int largestArea = 0;
        for (int i = 0; i < contours.size(); i++)
        {
            if (contours[i].size() > largestArea)
            {
                largestArea = contours[i].size();
                biggestContourIndex = i;
            }
        }


        vector<int> hullsI;
        vector<Point> hullsP;
        vector<Vec4i> defects;


        //Find the convex hull.
        if (contours.size() > 0)
        {
            convexHull(contours[biggestContourIndex], hullsI, true, true);
            convexHull(contours[biggestContourIndex], hullsP, true, true);
        }

        //Find the convexity defects.
        if (contours.size() > 0)
        {
            if (contours[biggestContourIndex].size() > 3)
            {               
                convexityDefects(contours[biggestContourIndex], hullsI, defects);               
            }
        }




        //Draw the biggest contour and its convex hull.
        Scalar colorOne = Scalar(255, 128, 0);
        Scalar colorTwo = Scalar(0, 0, 255);
        if (contours.size() > 0)
        {
            drawContours(iRoiSRC, contours, biggestContourIndex, colorOne, 2, 8, hierarchy, 0, Point());
            drawContours(iRoiSRC, hullsP, 0, colorTwo, 1, 8, vector<Vec4i>(), 0, Point());
            rectangle(iRoiSRC, boundingRect(contours[biggestContourIndex]), Scalar(0, 255, 0), 1, 8, 0);

        }
        imshow("src", src);

        if (waitKey(waitTime) >= 0) break;

    }

    return(0);
}

屏幕左上角有一个矩形,当我握住它时,我的手就会被识别出来。

我得到的错误出现在第一个drawContours。控制台给我的完整错误是:OpenCV Error: Assertion failed &lt;i &lt;0&gt; in cv::_InputArray::getMat, file C:\buildslave64\win64_amdoc1\2_4_PackSlave-win64-vc11-shared\opencv\modules\core\src\matrix.cpp, line 963

我一直在多个站点上广泛搜索解决方案,包括 stackoverflow,但似乎没有一个解决方案有效。

任何帮助将不胜感激。

我使用 Visual Studio 2013 和 OpenCV-2.4.10

【问题讨论】:

  • 你调试过代码,看看i的负值实际上来自哪里?
  • @πάνταῥεῖ 是的,但我找不到值。
  • 你能在函数调用之前打印最大轮廓索引吗?顺便说一句,contour.size() 不是轮廓的面积而是点的数量!
  • @Micka maximumContourIndex = 1 每次因为它确实检测到我的手是最大的区域。我将更改变量名称。会不清楚。谢谢!
  • 发现vector缺陷;需要转换为点向量,现在它可以工作了。感谢您的帮助!

标签: c++ opencv assertion


【解决方案1】:

vector&lt;CvConvexityDefect&gt; defects; 转换为一个点似乎可以解决问题

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2019-07-24
    • 2018-11-29
    • 2017-12-24
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多