我是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