我在Xcode4 OS X 10.7上使用openCV 2.3.1
我有一个(演示)代码,可以在一些基本的背景减法后找到轮廓并以各种颜色显示它。这部分有效。
我想过滤掉小于特定大小的轮廓,但是当我调用contourArea()时,我得到以下断言失败:
OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_graphics_opencv/opencv/work/OpenCV-2.3.1/modules/core/src/matrix.cpp, line 912
terminate called throwing an exception
相关代码是:
for( ; idx >= 0; idx = hierarchy[idx][0] ) {
//double area = contourArea(contours);
//cout << area << endl;
Scalar color( rand()&255, rand() &255, rand()&255);
drawContours(dst, contours, idx, color);
}
这是最后一个for循环:
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <iostream>
#include <vector>
#include "opencv2/video/background_segm.hpp"
using namespace std;
using namespace cv;
void refineSegments(const Mat& img, Mat& mask, Mat& dst)
{
int niters = 3;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat temp;
dilate(mask, temp, Mat(), Point(-1,-1), niters);
erode(temp, temp, Mat(), Point(-1,-1), niters*2);
dilate(temp, temp, Mat(), Point(-1,-1), niters);
findContours( temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
dst = Mat::zeros(img.size(), CV_8UC3);
if( contours.size() == 0 )
return;
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] ) {
double area = contourArea(contours);
cout << area << endl;
Scalar color( rand()&255, rand() &255, rand()&255);
drawContours(dst, contours, idx, color);
}
}
我抱怨源代码中的一段代码是:
if( k == STD_VECTOR_VECTOR )
{
int t = type(i);
const vector<vector<uchar> >& vv = *(const vector<vector<uchar> >*)obj;
CV_Assert( 0 <= i && i < (int)vv.size() );
const vector<uchar>& v = vv[i];
return !v.empty() ? Mat(size(i), t, (void*)&v[0]) : Mat();
}
但是我无法做出这样的结论...我是一个传递给getMat的int,但实际上它是什么,为什么它应该小于0超出我&gt; __&lt;。 对我来说这个标准函数做到这一点似乎很奇怪,任何人都可以对此有所了解吗?
答案 0 :(得分:0)
这是一个简单的错字。您将所有轮廓赋予contourArea函数:
double area = contourArea(contours);
你可能想要
double area = contourArea(contours[idx]);