c++ - 确定直方图的所有局部最大值的函数

标签 c++ opencv

是否有一个 OpenCV 函数可以给我一个直方图所有局部最大值的列表?也许有一个函数可以让我指定一个最小峰值/阈值,并告诉我所有高于该阈值的局部最大值的 bin。

如果没有,是否有一个函数可以将 bin 从最高(最频繁)到最低(最不频繁)排序。然后我可以抓取所有前 20 个左右的 bin,并且我有 20 个最大的局部最大值。

最佳答案

Opencv minMaxLoc 可以在滑动窗口的上下文中使用。如果最大值的位置在一条边上则忽略最大值,否则记录为最大值。您可以使用类似下面的函数(注意:此代码更像是伪代码,尚未经过测试)

/**
* Assumes a 1 channel histogram
*/
vector<int> findMaxima(Mat histogram, int windowsize, int histbins){
   vector<int> maximas;
   int lastmaxima;
   for(int i = 0; i < histbins - windowsize; i++){
       //Just some Local variables, only maxloc and maxval are used.
       int maxval,minval;
       Point* maxloc, maxloc;
       //Crop the windows
       Rect window(i,0,windowsize,1);
       //Get the maxima
       minMaxLoc(histogram(window), minval,maxval,maxloc,minloc);
       //Check if its not on the side
       if(maxloc.x != 0&&maxloc.x != windowsize-1){
            //Translate from cropped window into real position
            int originalposition = maxloc.x+i;
            //Check that this is a new maxima and not already recorded
            if(lastmaxima != originalposition){
               maximas.push(originalposition);
               lastmaxima = originalposition;
            }
       }
   }
   return maximas;
}

当然这是一个非常简单的系统。您可能希望使用具有不同滑动窗口大小的多尺度方法。您可能还需要根据您的数据应用高斯平滑。另一种方法可能是针对 3 或 4 之类的小窗口大小运行它(您需要最小值 3)。然后您可以使用其他东西进行非极大值抑制。

对于您建议的方法

Maybe there is a function that lets me specify a minimum peak/threshold and will tell me the bins of all those local maxima above that threshold.

您可以在使用上述函数找到最大值之前简单地执行一个阈值。

threshold(hist,res ...aditional parameters...);
vector<int> maximas = findMaximas(hist, ...other parameters...);

关于c++ - 确定直方图的所有局部最大值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674006/

相关文章:

C++ NetBeans std::array 不可用

opencv - VS2013 + Win7 中缺少 MSVCP140.dll

java - 如何使用java制作一个空Mat?

opencv - Tesseract&Opencv程序镜像问题显示一些错误

c++ - OpenCV drawContours 奇怪的行为

c++ - 三分法则是什么?

c++ - 如何在 Windows 中的特定显示器上打开一个窗口?

c++ - 模板类型推导失败

c++ - 如何在 C++ 中声明抽象类的 vector 列表?

matlab - 使用 OpenCV 和 python 从图像中提取对象(指纹和签名)