最近要计算图像的直方图,发现calcHist中的参数不是那么的一目了然,到网上搜了一下,资源还是比较多。

这个哥们做的不错,为了方便在此转载过来。

函数说明如下:

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, boolaccumulate=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, boolaccumulate=false)

函数参数:

arrays – Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.
        源输入(图像)数组,必须是相同深度的CV_8U或者CV_32F(即uchar或者float),相同大小,每一个可以是任意通道的;
     narrays – Number of source arrays.

 源输入数组中的元素个数;

channels – List of the dims channels used to compute the histogram. The first array channels are enumerated from 0 to arrays[0].channels()-1 ,   the second arraychannels are counted from arrays[0].channels() to arrays[0].channels() + arrays[1].channels()-1, and so on.
   

mask – Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as arrays[i].  The non-zero mask elements mark the array elements counted in the histogram.

可选的掩膜,如果该矩阵不是空的,则必须是8位的并且与arrays[i]的大小相等,掩膜的非零值标记需要在直方图中统计的数组元素;
   hist – Output histogram, which is a dense or sparse dims -dimensional array.

输出直方图,是一个稠密或者稀疏的dims维的数组;
       dims – Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS (equal to 32 in the current OpenCV version).

直方图的维数,必须为正,并且不大于CV_MAX_DIMS(当前的OpenCV版本中为32,即最大可以统计32维的直方图);(统计的变量个数)
       histSize – Array of histogram sizes in each dimension.

用于指出直方图数组每一维的大小的数组,即指出每一维的bin的个数的数组;(每维分成多少份)

ranges – Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform ( uniform =true),   then for each dimension i it isenough to specify the lower (inclusive) boundary of the 0-th histogram bin and the upper(exclusive)  boundary for the last histogram bin histSize[i]-1. That is,in case of a uniform histogram each of ranges[i] is an array of 2 elements.   When the histogram is not uniform ( uniform=false ), then each of ranges[i]contains histSize[i]+1 elements:.  The array elements, that are not between  and  , are not counted in the histogram.
    - 用于指出直方图每一维的每个bin(份)的上下界范围数组的数组,当直方图是均匀的(uniform =true)时,对每一维i指定直方图的第0个bin的下界(包含即[)L0和最 后一个即第histSize[i]-1个bin的上界(不包含的即))U_histSize[i]-1,也就是说对均匀直方图来说,每一个ranges[i]都是一个两个元素的数组【指出该维的上下界】。当直方图不是均匀的时,每一个ranges[i]数组都包含histSize[i]+1个元素:L0,U0=L1,U1=L1,...,U_histSize[i]-2 = L_histSize[i]-1,U_histSize[i]-1.不在L0到U_histSize[i]-1之间的数组元素将不会统计进直方图中;

uniform – Flag indicates that whether the histogram is uniform or not (see above).

直方图是否均匀的标志;【指定直方图每个bin统计的是否是相同数量的灰度级】

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.
 


PS:对通道的单独说明:

首先dims是最终的直方图维数,narrays指出了arrays数组中图像的个数,其中每一幅图像都可以是任意通道的【只要最终dims不超过32即可】。

如果channels参数为0,则narrays和dims必须相等,否则弹出assert,此时计算直方图的时候取数组中每幅图像的第0通道。

当channels不是0的时候,用于计算直方图的图像是arrays中由channels指定的通道的图像,channels与arrays中的图像的对应关系,如channels的参数说明的,将arrays中的图像从第0幅开始按照通道摊开排列起来,然后channels中的指定的用于计算直方图的就是这些摊开的通道;假设有arrays中只有一幅三通道的图像image,那么narrays应该为1,如果是想计算3维直方图【最大也只能是3维的】,想将image的通道2作为第一维,通道0作为第二维,通道1作为第三维,则可以将channels设置为channesl={2,0,1};这样calcHist函数计算时就按照这个顺序来统计直方图。
可以看出channels不为0时narrays可以和dims不相等,只要保证arrays中至少有channels指定的通道就可以。