Opencv error: Assertation failed when findContours is used
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?