是否有一个 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/