在OpenCV C ++中找到Mat()的所有峰

时间:2015-03-04 01:27:59

标签: c++ opencv histogram ocr

我想找到我图像的所有最大值(非零像素数)。我需要它来区分我的图片:

enter image description here

所以,我已经问过question,如何将所有图像投影到其中一个轴上,现在我需要在这个单行图像上找到所有最大值。 这是我的代码部分:

void segment_plate (Mat input_image) {
    double minVal; 
    double maxVal; 
    Point minLoc; 
    Point maxLoc;
    Mat work_image = input_image;
    Mat output;
    //binarize image
    adaptiveThreshold(work_image,work_image, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 10);  
    //project it to one axis
    reduce(work_image,output,0,CV_REDUCE_SUM,CV_32S);

    //find minimum and maximum falue for ALL image
    minMaxLoc(output, &minVal,&maxVal,&minLoc,&maxLoc);
    cout << "min val : " << minVal << endl;
    cout << "max val: " << maxVal << endl;

如您所见,我可以找到所有图片的最大值和最小值,但我需要找到局部最大值。谢谢你的帮助!

修改 好吧,所以我犯了一个错误,我需要找到这个向量的峰值。我已使用此代码查找第一个峰值:

int findPeakUtil(Mat arr, int low, int high, int n) {
// Find index of middle element
    int mid = low + (high - low)/2;  /* (low + high)/2 */

// Compare middle element with its neighbours (if neighbours exist)
    if ((mid == 0 || arr.at<int>(0,mid-1) <= arr.at<int>(0,mid)) &&
        (mid == n-1 || arr.at<int>(0,mid+1) <= arr.at<int>(0,mid)))
    return mid;

// If middle element is not peak and its left neighbor is greater than it
// then left half must have a peak element
    else if (mid > 0 && arr.at<int>(0,mid-1) > arr.at<int>(0,mid))
    return findPeakUtil(arr, low, (mid - 1), n);

// If middle element is not peak and its right neighbor is greater than it
// then right half must have a peak element
    else return findPeakUtil(arr, (mid + 1), high, n);
}

// A wrapper over recursive function findPeakUtil()
int findPeak(Mat arr, int n) {
   return findPeakUtil(arr, 0, n-1, n);
}

所以现在我的代码看起来像:

void segment_plate (Mat input_image) {
    Mat work_image = input_image;
    Mat output;
    //binarize image
    adaptiveThreshold(work_image,work_image, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 10);  
    //project it to one axis
    reduce(work_image,output,0,CV_REDUCE_SUM,CV_32S);
    int n = output.cols;
    printf("Index of a peak point is %d", findPeak(output, n));

但我怎样才能找到另一座山峰呢?峰值发现算法取自here

1 个答案:

答案 0 :(得分:1)

我可以考虑找到峰值的一种方法是找到一阶导数,然后找到它的负数。

例如,

a = [ 1, 2, 3, 4, 4, 5, 6, 3, 4]

在此示例中,位置6的峰值为6,拉斯位置的峰值为4。 所以,如果你扩展向量(结尾为0)并应用一阶导数(a [i] - a [i-1]),你将得到

a_deriv = [1,1,1,0,1,1,-3,1,-4]

其中负数位于峰的位置。在这种情况下,-3位于位置6,-4位于位置8,即峰位于的位置。

这是一种方法......但不是唯一的方法。

请注意,此方法仅计算高原中的最后一个数字作为峰值(当两个数字共享峰值时,您可以找到一个平台,因为它们具有相同的值并且是连续的)

希望这有助于你