0

I'm having troubles with a part of my OpenCV C++ code:

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
        }
    }
}

The error I'm getting is

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

I'm modifying the OpenCV squares.cpp sample program and want to remove all squares that differs too much from the median area of the squares found in the image.

Last in cleanSquares I make a backwards loop and check if each square differs too much and in that case I want to erase that vector from the squares vector. What am I doing wrong?

tirithen
  • 3,219
  • 11
  • 41
  • 65

1 Answers1

1

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

squares is a const reference, you cannot erase from it. Drop the const, or take by value, according to your final intentions.

I would also recommend using the erase( remove_if( ... ) ) idiom, if you feel comfortable with it.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • Of course, that did the trick. Thanks for the fast answer! :-) – tirithen Sep 13 '11 at 20:45
  • I also had to change the for loop to for( size_t i = size - 1; i --> 0; ) to get the loop to properly count backwards, explanation here http://stackoverflow.com/questions/3623263/reverse-iteration-with-an-unsigned-loop-variable – tirithen Sep 13 '11 at 21:27
  • You wouldn't have had that problem if you used the erase( remove_if( ... ) ) idiom, you wouldn't even had to write a loop. Consider researching it in the future. – K-ballo Sep 13 '11 at 21:30