Ask Your Question
1

Opencv error: Assertation failed when findContours is used

asked 2017-04-09 12:19:07 -0600

kuppy gravatar image

I'm using visual studio 2015 and openCV 3.2 I have caught this exception:

OpenCV Error: Assertion failed ((_contours.kind() == _InputArray::STD_VECTOR_VECTOR || _contours.kind() == _InputArray::STD_VECTOR_MAT || _contours.kind() == _InputArray::STD_VECTOR_UMAT)) in cv::findContours, file C:\build\master_winpack-build-win64-vc14\opencv\modules\imgproc\src\contours.cpp, line 1902

when I used findConours method.

Code:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"

#include <stdlib.h>
#include <stdio.h>


using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{

    VideoCapture cap(0);
    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    int counter = 0;
    char filename[512];
    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        Mat im;
        // Convert image to YCrCb
        cvtColor(frame, im, COLOR_RGB2YCrCb);

        //skin tone
        Mat skin;
        inRange(im, Scalar(0,133,77), Scalar(255,173,127),skin);

        Mat contours;
        findContours(skin,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);

        for (int i = 0; i<contours.rows; i++)
        {
            for (int j = 0; i < contours.cols; j++)
            {
                double area = contourArea(j);
                if (area > 1000)
                {
                    drawContours(frame, contours, i, Scalar(0, 250, 0), 3);
                }
            }
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        char c = waitKey(33);
        if (c == 27) 
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }
    destroyWindow("MyVideo");
    cap.release();
    return 0;
}

How can I solve this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-04-09 16:17:04 -0600

Each contour is stored as a vector of points (e.g. std::vector<std::vector<cv::Point> >)

the problem in your code is the line below

    Mat contours;
    findContours(skin,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);

correct it like:

    vector<vector<Point> > contours;
    findContours(skin,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
edit flag offensive delete link more

Comments

Thank you, but after I replaced another problem has arisen in the same row: Unhandled exception at 0x00007FFB72FFD5B8 (ucrtbase.dll) in try opencv.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

kuppy gravatar imagekuppy ( 2017-04-10 16:35:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-09 11:56:14 -0600

Seen: 2,648 times

Last updated: Apr 09 '17