2011-09-13 6 views
0

私はOpenCVのCの一部でトラブルを抱えている++コード:C++ベクトルは:: OpenCVのコードで問題を消去

double getVectorMedian(vector<double> values) 
{ 
    size_t size = values.size(); 
    double median; 

    sort(values.begin(), values.end()); 

    if(size % 2 == 0) 
    { 
     median = (values[size/2 - 1] + values[size/2])/2; 
    } 
    else 
    { 
     median = values[size/2]; 
    } 

    return median; 
} 

void cleanSquares(const vector<vector<Point> >& squares) 
{ 
    float tolerance = 0.2; 
    size_t size = squares.size(); 
    vector<double> areas(size); 

    for(size_t i = 0; i < size; i++) 
    { 
     areas[i] = fabs(contourArea(Mat(squares[i]))); 
    } 

    double medianArea = getVectorMedian(areas); 
    double minArea = medianArea * (1 - tolerance); 
    double maxArea = medianArea * (1 + tolerance); 

    for(unsigned int i = size - 1; i >= 0; i--) 
    { 
     if(areas[i] > maxArea || areas[i] < minArea) 
     { 
      squares.erase(squares.begin() + i); // Here I get the error 
     } 
    } 
} 

私は取得していますエラーが私は

no matching function for call to ‘std::vector<std::vector<cv::Point_<int> > >::erase(__gnu_cxx::__normal_iterator<const std::vector<cv::Point_<int> >*, std::vector<std::vector<cv::Point_<int> > > >) const’ main.cpp /find_notes/src line 154 C/C++ Problem 

ですOpenCV squares.cppサンプルプログラムを変更し、画像内に見つかった四角形の中央領域とは多すぎるすべての四角形を削除したい。

cleanSquaresの最後の私は後方ループを作り、それぞれの正方形はあまり異なり、その場合には、私は正方形のベクトルからそのベクトルを消去するかどうかを確認。私は間違って何をしていますか?

答えて

1

void cleanSquares(const vector<vector<Point> >& squares)

squares const参照である、あなたはそれから消去することはできません。あなたの最終的な意図にしたがって、constを落とすか、価値を引きます。

あなたはそれで快適に感じる場合、私はまた、消去(remove_if(...))イディオムを使用することをお勧めします。トリックをしました。もちろん、

+0

、。高速回答ありがとう! :-) – Tirithen

+0

私も(size_tのI =サイズ - 1; I - > 0;)のためにforループを変更しなければならなかった、ループが適切に逆方向にカウントするために取得するにはここでの説明http://stackoverflow.com/questions/ 3623263 /逆反復-で-符号なしループ変数 – Tirithen

+0

あなたは消去(remove_if(...))イディオムを使用した場合、あなたもループを書かなければならなかったではないだろうという問題を抱えていないだろう。将来それを研究することを検討してください。 –