我试图平均直方图峰值周围的键。所以,我首先找到峰值然后我的尝试是添加一个并从峰值索引中减去一个以找到峰值周围的平均值。我使用距离函数找到索引。但是,我无法找到正确的解决方案,因为代码不会编译。有人可以帮忙吗?
int iterator_distance;
for ( std::map<float, int>::iterator it = histogram_x.begin(); it != histogram_x.end(); it++) {
if (max_occurence.x <= it->second ) {
max_occurence.x = it->second;
max_voted.x = it->first;
iterator_distance = std::distance(histogram_x.begin(), it);
//std::cout << x.first << " histogram " << x.second << "endx\n";
}
}
// Average around the peak
if ( iterator_distance > 2 ) {
max_voted.x = (histogram_x.begin()+iterator_distance-1)->first + (histogram_x.begin()+iterator_distance)->first + (histogram_x.begin()+iterator_distance+1)->first;
}
以下是错误。该错误表明plus运算符不能与迭代器和float一起使用。但那是我的问题,我该如何解决这个问题?
error: no match for ‘operator+’ (operand types are ‘std::map<float, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const float, int> >}’ and ‘int’)
max_voted.x = (histogram_x.begin()+iterator_distance)->first + (histogram_x.begin()+iterator_distance-1)->first;
答案 0 :(得分:0)
这应该适用于C ++ 11:
assert(histogram_x.size() > 0);
auto peak_it = std::max_element(histogram_x.cbegin(), histogram_x.cend(),
[](const pair<float, int>& lhs, const pair<float, int>& rhs) {
reutrn lhs.second < rhs.second; });
max_voted.x = peak_it->first;
if ((peak_it != histogram_x.begin()) && (peak_it != std::prev(histogram_x.end()))
max_voted.x += std::prev(peak_it)->first + std::next(peak_it)->first /* / 3.0f ??? */;