我是C++和OpenCV的新手。我编写了一个简单的程序,你可以在下面找到它,但是当我运行它时,总是会抛出一个异常,由
请注意,如果将
非常感谢。
以下是我的代码:
findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE)
引发类型断言失败的异常。
我需要一个类来表示单个轮廓并集成轮廓分析方法。 我知道OpenCV错误:断言失败(mtype == type0 ||(CV_MAT_CN(mtype)== CV_MAT_CN (type0)&& ((1 << type0) & fixedDepthMask)!= 0))在create中,文件C:\ opencv \ modu les \ core \ src \ matrix.cpp,第1466行。
CONTOUR
与vector<Point>
不同,但由于它扩展了后者,CONTOUR
不应该也是vector<Point>
类型(同样地,vector<CONTOUR>
也应该是vector< vector<Point> >
类型),我错了吗?请注意,如果将
CONTOUR
声明为从vector<vector<Point>>
派生的类,并在下面的代码中将Ctr
声明为CONTOUR
对象而不是vector<CONTOUR>
,则一切都可以正常工作。非常感谢。
以下是我的代码:
#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;
}