OpenCV, llamando a Contourarea causa falla de afirmación en Getmat
Pregunta
Estoy usando OPENCV 2.3.1 en Xcode4 OS X 10.7
Tengo un código (demo) que encuentra contornos después de una resta de fondo básica y lo muestra en varios colores.Esta parte funciona
Me gustaría filtrar los contornos más pequeños que un tamaño específico, pero cuando llamo a Contourarea (), obtengo la siguiente afirmación falla:
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
El código relevante es:
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);
}
que es el último para el bucle de:
#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);
}
}
quejo la pieza de código en la fuente es:
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();
}
Pero no puedo hacer que la cabeza o las colas de esto ... i es un int que se pasa a Getmat, pero lo que realmente es, y por qué debería ser inferior a 0 está más allá de mí> __ <. Parece muy extraño para mí que esta función estándar hace esto, ¿alguien puede arrojar un poco de luz sobre esto?
Solución
Es un tipografía simple.Está dando todos los contornos a la función de contorno:
double area = contourArea(contours);
probablemente quisiste
double area = contourArea(contours[idx]);