OPENCV中直方图的标准化

时间:2015-11-26 19:54:19

标签: histogram normalization opencv3.0

我对OpenCV项目中的规范化过程有疑问。我应该使用的函数叫做cvNormalizeHist,但是我不知道如何在代码中直接使用它。

C: void cvNormalizeHist(CvHistogram* hist, double factor)

Parameters: 
    hist – Pointer to the histogram.
    factor – Normalization factor.

The function normalizes the histogram bins by scaling them so that the sum of the bins becomes equal to factor.

我想做的是:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat src, dst;

    /// Load image
    src = imread(argv[1], 1);

    if (!src.data)
    {
        return -1;
    }

    /// Separate the image in 3 places ( B, G and R )
    vector<Mat> bgr_planes;
    split(src, bgr_planes);

    /// Establish the number of bins
    int histSize = 256;

    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 };
    const float* histRange = { range };

    bool uniform = true; 
    bool accumulate = false;

    Mat b_hist, g_hist, r_hist;

    /// Compute the histograms:
    calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);

    cvNormalizeHist(b_hist, 5); // (1)

    CvHistogram histogram(); // (2)
    ...

    waitKey(0);

    return 0;
}

我正在尝试使用这样的函数,但错误提示我应该使用CvHistogram而不是Mat对象。我不知道如何在这些对象之间进行转换。

任何人都可以告诉我如何做到这一点或任何建议?

可能有用的更多信息有: Windows 10,Visual Studio 2013,OpenCV 3.0

此致

1 个答案:

答案 0 :(得分:2)

您应该避免使用过时的C函数。

您可以使用normalizealpha等于您的因子,NORM_L1

例如:

double factor = 25;
normalize(b_hist, b_hist, factor, 0, NORM_L1);

cout << sum(b_hist)[0];

你看到这些箱子的总和等于因子。