2012-03-14 29 views
0

我使用的OpenCV 2.3.1 Xcode4 OS X 10.7OpenCV的,叫contourArea導致斷言失敗getMat

我有一個(演示)代碼後,一些基本的背景減除發現的輪廓,並顯示在各種顏色。這部分工作。

我想篩選出輪廓比特定尺寸較小,但是當我打電話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(); 
} 

但我不能讓頭部或尾部這個......我是被傳遞到getMat一個int,但它確實是,爲什麼它應該是什麼小於0是超越我> __ <。 對我來說這似乎很奇怪,這個標準函數做到這一點,任何人都可以對此有所瞭解?

+0

你能告訴你有什麼話對這些參數傳遞; -Mat和麪具,地墊和dst.Please – Gypsa 2012-08-27 12:49:40

回答

0

這是一個簡單的錯字。你是給所有輪廓到contourArea功能:

double area = contourArea(contours); 

你可能想

double area = contourArea(contours[idx]); 
+0

謝謝,那正是我想要的。 > __ <對不起,這個愚蠢的問題,還不完全熟悉openCV ... – 2012-03-14 13:35:09