OpenCV的calcHist

來源:互聯網
上載者:User

最近要計算映像的長條圖,發現calcHist中的參數不是那麼的一目瞭然,到網上搜了一下,資源還是比較多。

http://blog.csdn.net/ljbsdu/article/details/7412787這個哥們做的不錯,為了方便在此轉載過來。

函數說明如下:

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.
            用來計算長條圖的通道維數數組,第一個數組的通道由0到arrays[0].channels()-1列出,第二個數組的通道從arrays[0].channels()到arrays[0].channels()arrays[1].channels()-1以此類推;

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.
     -累加標誌;  [單幅映像不進行累計所以例子1中為false]


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};這樣calcHistFunction Compute時就按照這個順序來統計長條圖。
可以看出channels不為0時narrays可以和dims不相等,只要保證arrays中至少有channels指定的通道就可以。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.