C++ vector::erase problems with OpenCV code
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
开发者_Go百科 }
}
}
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?
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.
精彩评论