I'm a newbie of C++ and opencv. I've written a simple program that you can find below but when I run it I always get an exception thrown by findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE)
raised by type assertion failed
OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN (type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file C:\opencv\modu les\core\src\matrix.cpp, line 1466.
I need a class that represents a single contour and integrates contour analisys method. I know that CONTOUR
is a different type respect to vector<Point>
but since it extends the latter, shouldn't CONTOUR
be also a vector<Point>
type (and in the same way vector<CONTOUR>
be also a vector< vector<Point> >
) ? Am I wrong?
Please note that if you declare CONTOUR
as a class derived from vector<vector<Point>>
and declare Ctr
in the code below as a CONTOUR
object in place of vector<CONTOUR>
everything work out fine.
Many thanks in advance.
Here is my code
#include "opencv2/opencv.hpp"
#include <vector>
using namespace cv;
using namespace std;
class CONTOUR : public vector<Point>
{
public:
CONTOUR() : vector<Point>(){ };
CONTOUR(const CONTOUR& orig) : vector<Point> (orig){ };
virtual ~CONTOUR(){ };
CONTOUR& operator=(const CONTOUR& rhs)
{
vector<Point> :: operator = (rhs);
return *this;
}
CONTOUR& operator=(const vector<Point>& rhs)
{
vector<Point> :: operator = (rhs);
return *this;
}
};
/** @function main */
int main(int argc, char** argv)
{
VideoCapture Camera;
if(Camera.open(0))
{
Mat img;
namedWindow("VIDEO", CV_WINDOW_AUTOSIZE);
for(;;)
{
Camera >> img;
if(!img.empty())
{
CONTOUR ctr;
RNG n(12345);
GaussianBlur(img, img, Size(5,5), 1.0, 1.0);
cvtColor(img, img, CV_BGR2GRAY);
Canny(img, img, 20, 80, 3);
findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
Mat shape = Mat::zeros( img.size(), CV_8UC3 );
for( unsigned int i = 0; i< ctr.size(); i++ )
{
Scalar color(n.uniform(0,255), n.uniform(0,255), n.uniform(0,255));
drawContours(shape, ctr, i, color, 1, 8);
}
imshow("VIDEO", shape);
if(waitKey(30) >= 0)
{
break;
}
}
}
}
else
{
cout << "Camera not opened" << endl;
}
return 0;
}