1

我在 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();
}

但我无法对此做出正面或反面... i 是一个传递给 getMat 的 int,但它到底是什么,以及为什么它应该小于 0 超出了我的理解>__<。这个标准功能对我来说似乎很奇怪,任何人都可以对此有所了解吗?

4

1 回答 1

1

这是一个简单的错字。您将所有轮廓提供给 contourArea 函数:

double area = contourArea(contours);

你可能想要

double area = contourArea(contours[idx]);
于 2012-03-14T12:36:50.830 回答