opencv, Contourarea를 호출하면 Getmat에서 어설 션 오류가 발생합니다
문제
Xcode4 OS X 10.7
에 OpenCV 2.3.1을 사용하고 있습니다.몇 가지 기본 배경 뺄셈 후 윤곽을 찾은 (데모) 코드가있어 다양한 색상으로 표시됩니다.이 부분은 작동합니다.
Contourawea ()를 호출 할 때 윤곽을 필터링하고자하는 윤곽을 필터링하고 싶습니다.
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);
}
.
다음의 마지막 루프 인
#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보다 작아야하는지 __ __ __ __ __. 이 표준 기능이 이것이 이것에 작은 빛을 던질 수있는 사람이 너무 이상하게 보일 수 있습니까?
해결책
간단한 오타입니다.당신은 contourasea 함수에 모든 윤곽을주고 있습니다 :
double area = contourArea(contours);
.
아마
를 원했을 것입니다.double area = contourArea(contours[idx]);
. 제휴하지 않습니다 StackOverflow