Opencv.calcHist使用方法

来源:互联网 发布:台式电脑网络连接不上 编辑:程序博客网 时间:2024/04/29 23:36

  opencv提供了calcHist函数来计算图像直方图。

         其中C++的函数原型如下:void calcHist(const Mat* arrays, int narrays, const int* channels, InputArray mask, OutputArray
hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=
false );

          void calcHist(const Mat* arrays, int narrays, const int* channels, InputArray mask, SparseMat&
hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=
false );

         参数解释:

        arrays。输入的图像的指针,可以是多幅图像,所有的图像必须有同样的深度(CV_8U or CV_32F)。同时一副图像可以有多个channes。

        narrays。输入的图像的个数。

        channels。用来计算直方图的channes的数组。比如输入是2副图像,第一副图像有0,1,2共三个channel,第二幅图像只有0一个channel,

那么输入就一共有4个channes,如果int channels[3] = {3, 2, 0},那么就表示是使用第二副图像的第一个通道和第一副图像的第2和第0个通道来计

算直方图。

        mask。掩码。如果mask不为空,那么它必须是一个8位(CV_8U)的数组,并且它的大小的和arrays[i]的大小相同,值为1的点将用来计算

直方图。

        hist。计算出来的直方图

        dims。计算出来的直方图的维数。

        histSize。在每一维上直方图的个数。简单把直方图看作一个一个的竖条的话,就是每一维上竖条的个数。

        ranges。用来进行统计的范围。比如

         float rang1[] = {0, 20};

        float rang2[] = {30, 40};

        const float *rangs[] = {rang1, rang2};那么就是对0,20和30,40范围的值进行统计。

       uniform。每一个竖条的宽度是否相等。

       accumulate。Accumulation flag. If it is set, the histogram is not cleared in the beginning
when it is allocated. This feature enables you to compute a single histogram from several
sets of arrays, or to update the histogram in time.  是否累加。如果为true,在下次计算的时候不会首先清空hist。这个地方我是这样理解的,不知道有没有错,

请高手指点。


     一段示例代码:

    //生成一幅单通道图像。    cv::Mat image(imageHeight        , imageWidth, CV_8U, imageAddress);    cv::MatND histogram;    //256个,范围是0,255.    const int histSize = 256;    float range[] = {0, 255};    const float *ranges[] = {range};    const int channels = 0;    cv::calcHist(image, 1, &channels, cv::Mat(), histogram, 1, &histSize, &ranges[0], true, false);    int row = histogram.rows;    int col = histogram.cols;    float *h = (float*)histogram.data;    double hh[256];    if (h) {        for (int i = 0; i < 256; ++i) {            hh[i] = h[i];        }    }

转自: http://blog.csdn.net/sydnash/article/details/7451039

原创粉丝点击