简体   繁体   English

OpenCV C ++断言失败,调用drawContours

[英]OpenCV c++ assertion failed call drawContours

I'm trying to find the biggest square and draw it on the original image. 我正在尝试找到最大的正方形并将其绘制在原始图像上。

When I call 当我打电话

drawContours(input,(screenCnt),-1,Scalar(255,0,0),3);

the following error occurs: 发生以下错误:

E/cv::error(): OpenCV Error: Assertion failed (i < 0) in cv::Mat cv:: InputArray::getMat (int) const, file /build/master_pack-android/opencv/modules/core/src/matrix.cpp, line 1260 E / cv :: error():OpenCV错误:cv :: Mat cv :: InputArray :: getMat (int)const断言失败(i <0)const,文件/ build / master_pack-android / opencv / modules / core / src / matrix.cpp,第1260行

I found a lot of stuff on Github and Google, But I couldn't find any related solutions. 我在Github和Google上发现了很多东西,但是找不到任何相关的解决方案。

Could you help someone who knows how to solve it? 您能帮助一个知道如何解决的人吗?

Here is my code. 这是我的代码。 (c++) (C ++)

bool compareContourAreas(vector<Point> contour1, vector<Point> contour2){
    double i = fabs(contourArea(Mat(contour1)));
    double j = fabs(contourArea(Mat(contour2)));
    return ( i < j );
}


        Mat &input = *(Mat *) matAddrInput;
        Mat &result = *(Mat *) matAddrResult;

        Mat gray;
        Mat edge;
        resize(input,input, cv::Size(), 0.75, 0.75);

        cvtColor(input,gray,CV_RGB2GRAY);
        GaussianBlur(gray,gray,Size(3,3),0);
        Canny(gray,edge,100,200,3, false);

        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;


        findContours(edge,contours,hierarchy,CV_RETR_LIST,CHAIN_APPROX_SIMPLE);
        sort(contours.begin(),contours.end(),compareContourAreas);

        vector<Point> approx;
        vector<Point> screenCnt;

        for(size_t i = 0; i < contours.size(); i++){
            approxPolyDP((Mat(contours[i])),approx,arcLength(Mat(contours[i]),true) * 0.02, true);
            if(approx.size() == 4){
                screenCnt = approx;
                break;
            }
        }
        if(screenCnt.size() != 0){
            drawContours(input,(screenCnt),-1,Scalar(255,0,0),3); << error
        }

drawContours needs as input an array of contours, not just a single contour. drawContours需要输入一系列轮廓,而不仅仅是一个轮廓。 You can create directly the array in the call, like: 您可以在调用中直接创建数组,例如:

drawContours(input, std::vector<std::vector<cv::Point>>{screenCnt},-1,Scalar(255,0,0),3); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM