我正在使用opencv 2.3.1在xcode4 os x 10.7

我有一个(演示)代码,它在一些基本背景减法后找到轮廓,并以各种颜色显示它。这部分有效。

我想过滤掉小于特定大小的轮廓,但是当我调用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);
       }
.

这是循环的最后一个:

#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超出我> __ <。 对我来说,这个标准函数似乎非常奇怪,有谁可以在这个揭示一点亮起吗?

有帮助吗?

解决方案

这是一个简单的错字。您正在向Contourarea函数提供所有轮廓:

double area = contourArea(contours);
.

你可能想要

double area = contourArea(contours[idx]);
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top