在cv :: _ InputArray :: getMat </i>中,OpenCV c ++断言失败<i <=“”0 =“”>

时间:2015-03-17 18:31:43

标签: c++ opencv assertion

#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 <i <0> 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

1 个答案:

答案 0 :(得分:0)

vector<CvConvexityDefect> defects;转换为某个点似乎可以解决问题