我是C++和opencv的新手。我写了一个简单的程序,你可以在下面找到,但是当我运行它,我总是得到通过findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE)
抛出按类型断言引起的异常失败findcontours断言失败
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.
我需要一个代表一个轮廓和整合轮廓analisys类方法。我知道CONTOUR
是与vector<Point>
不同的类型,但是因为它扩展了后者,所以不应该是CONTOUR
也是vector<Point>
类型(并且以相同的方式vector<CONTOUR>
也是vector< vector<Point> >
)?我错了吗?
请注意,如果你声明CONTOUR
从vector<vector<Point>>
派生的类和地方vector<CONTOUR>
一切的声明在下面的代码作为CONTOUR
对象Ctr
迎刃而解。
非常感谢提前。
这里是我的代码
#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;
}
+1。我会给这个答案+5 – baci
谢谢你的答案。编辑我的问题希望它更清楚一点。 – Arloong
@Arloong在这个网站上,如果你[发布你的解决方案作为答案](http://stackoverflow.com/help/self-answer)和[接受](http://stackoverflow.com/help/someone -answers)是最有帮助的。除此之外,我**强烈建议您不要使用您发布的解决方案。当然,它可行,但会带来不必要的复杂性。 – Aurelius