OpenCV、ContourAreaを呼び出すと、GetMatでアサーション失敗が発生します

StackOverflow https://stackoverflow.com//questions/9700339

  •  13-12-2019
  •  | 
  •  

質問

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);
       }
.

:最後のループ:

#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よりも小さいべきなのは> __ <。 この標準機能はこれを行うことができます、誰もがこれに少し光を当てることができますか?

役に立ちましたか?

解決

それは単純な字型の字型です。あなたはすべての輪郭をContourea関数に与えています:

double area = contourArea(contours);
.

おそらく欲しい

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top