使用STD :: MAP在直方图峰周围的平均值

Averaging value around the histogram peak using std::map

本文关键字:周围 平均值 STD MAP 使用 直方图      更新时间:2023-10-16

我试图平均直方图周围的键。因此,我首先找到峰值,然后我的尝试是添加一个峰并从峰值索引中减去一个峰,以找到峰周围的平均键。我使用距离函数找到索引。但是,我找不到正确的解决方案,因为代码无法编译。有人可以帮忙吗?

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 << "endxn";
    }
}
// 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;
}

以下是错误。错误说,加上运算符不能与迭代器和浮动一起使用。但这就是我的问题,我该如何解决这个问题?

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;

这应该在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 ??? */;