Openc学习笔记:关于反投影直方图

直方图是图像内容的一个重要特性。如果一副图像的区域中显示的是一种独特的纹理或是一个独特的物体,那么这个区域的直方图可以看作是一个概念函数,它给出的是某个像素属于该纹理或物体的概率。基于上述想法,我们可以利用反向投影直方图来检测特定的图像内容。

而所谓的反向投影就是一种记录给定图像中的像素点如何适应直方图模型像素分布的方式。简单的讲, 所谓反向投影就是首先计算某一特征的直方图模型,然后使用模型去寻找图像中存在的该特征。说到这里,可能大家还是有点儿糊涂,其实具体来说,图像的反向投影图是用输入图像的某一位置上像素值(多维或灰度)对应在直方图的一个bin上的值来代替该像素值(bin上的值越大就代表着这个像素值出现的概率越大),所以得到的反向投影图是单通的。用统计学术语,输出图像象素点的值是观测数组在某个分布(直方图)下的概率。

反向投影图 - 十三月de天空 - 十三月de天空
 
其中b(xi)表示在位置xi上像素对应的直方图第b(xi)bin,直方图共mbin,qu表示第ubin的值
在OpenCV里面,有一个专门的函数calcBackProject来进行反投影直方图计算。
void calcBackProject(const Mat* arrays, int narrays, const int* channels, const SparseMat& hist, Mat& backProject, const float** ranges, double scale=1, bool uniform=true)
该函数的官方解析如下:

Calculates the back projection of a histogram.

参数:

 
  • 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
  • narrays – The number of source arrays
  • channels – The list of channels that are used to compute the back projection. The number of channels must match the histogram dimensionality. The first array channels are numerated from 0 toarrays[0].channels()-1 , the second array channels are counted from arrays[0].channels() toarrays[0].channels() + arrays[1].channels()-1 etc.
  • hist – The input histogram, a dense or sparse
  • backProject – Destination back projection aray; will be a single-channel array of the same size and the same depth as arrays[0]
  • ranges – The array of arrays of the histogram bin boundaries in each dimension. See calcHist()
  • scale – The optional scale factor for the output back projection
  • uniform – Indicates whether the histogram is uniform or not, see above

The functions calcBackProject calculate the back project of the histogram. That is, similarly to calcHist , at each location (x, y)the function collects the values from the selected channels in the input images and finds the corresponding histogram bin. But instead of incrementing it, the function reads the bin value, scales it by scale and stores in backProject(x,y) . In terms of statistics, the function computes probability of each element value in respect with the empirical probability distribution represented by the histogram. Here is how, for example, you can find and track a bright-colored object in a scene:

  1. Before the tracking, show the object to the camera such that covers almost the whole frame. Calculate a hue histogram. The histogram will likely have a strong maximums, corresponding to the dominant colors in the object.
  2. During the tracking, calculate back projection of a hue plane of each input video frame using that pre-computed histogram. Threshold the back projection to suppress weak colors. It may also have sense to suppress pixels with non sufficient color saturation and too dark or too bright pixels.
  3. Find connected components in the resulting picture and choose, for example, the largest component.

That is the approximate algorithm of CAMShift() color object tracker.

你可能感兴趣的:(Openc学习笔记:关于反投影直方图)