第四章
主要介绍的是Counting the Pixels with Histograms 用直方图来计算像素,可谓是图像识别的一个基础,有必要总结一下。
首先:直方图的概念就不用再赘述了!
简单介绍一下OpenCV是如何计算直方图的:
OpenCV提供了一个简单的计算数组集(通常是图像或分割后的通道)的直方图函数 calcHist
cv::calcHist(&image,//inputImg 输入图像,是个Mat* 类型
1, // histogram from 1 image only 所用到的图像数量,因为输入是个指针。
channels, // the channel used 所用到的通道数,对于灰度图像1就可以了
cv::Mat(), // no mask is used 一个遮罩
hist, // the resulting histogram 输出直方图矩阵
1, // it is a 1D histogram 直方图维数,一般一维就可以了
histSize, // number of bins 直方图尺寸,对于8位灰度图每通道的大小应该是256
ranges // pixel value range 每通道的直方图范围一般[0,255]
);
特别要说明的是这里很多参数都是指针所以特别要注意!可能是由于一些函数原来是用C风格代码写的,这也就随之继承到C++里了。
见官方说明
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 )
计算直方图,各参数含义可在参考文档中查到
Parameters:
接下来这一章就讲到一些增强对比度的方法,个人觉得也比较简单,就不赘述了。
主要介绍的是Counting the Pixels with Histograms 用直方图来计算像素,可谓是图像识别的一个基础,有必要总结一下。
首先:直方图的概念就不用再赘述了!
简单介绍一下OpenCV是如何计算直方图的:
OpenCV提供了一个简单的计算数组集(通常是图像或分割后的通道)的直方图函数 calcHist
cv::calcHist(&image,//inputImg 输入图像,是个Mat* 类型
1, // histogram from 1 image only 所用到的图像数量,因为输入是个指针。
channels, // the channel used 所用到的通道数,对于灰度图像1就可以了
cv::Mat(), // no mask is used 一个遮罩
hist, // the resulting histogram 输出直方图矩阵
1, // it is a 1D histogram 直方图维数,一般一维就可以了
histSize, // number of bins 直方图尺寸,对于8位灰度图每通道的大小应该是256
ranges // pixel value range 每通道的直方图范围一般[0,255]
);
特别要说明的是这里很多参数都是指针所以特别要注意!可能是由于一些函数原来是用C风格代码写的,这也就随之继承到C++里了。
见官方说明
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 )
计算直方图,各参数含义可在参考文档中查到
Parameters:
- 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 – Number of source arrays.
- channels – List of the dims channels used to compute the histogram. The first array channels are numerated from 0 toarrays[0].channels()-1 , the second array channels 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.
- hist – Output histogram, which is a dense or sparse dims -dimensional array.
- dims – Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS (equal to 32 in the current OpenCV version).
- histSize – Array of histogram sizes in each dimension.
- 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 is enough 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 ofranges[i] is an array of 2 elements. When the histogram is not uniform ( uniform=false ), then each of ranges[i] containshistSize[i]+1 elements: . The array elements, that are not between and , are not counted in the histogram.
- uniform – Flag indicatinfg whether the histogram is uniform or not (see above).
- 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.
接下来这一章就讲到一些增强对比度的方法,个人觉得也比较简单,就不赘述了。
然后就提到了如何用直方图去查找目标!书上的例子只不过是灰度图。
其实思路也比较简单,就是给查找目标的直方图,然后通过这个Backprojecting方法就OK。
这里要注意:
首先是计算目标区域的直方图
Histogram1D h;
cv::MatND hist= h.getHistogram(imageROI);
然后还必须归一化(前篇日志有提到)以下是原文:
By normalizing this histogram, we obtain a function that gives the probability of a pixel of a given intensity value to belong to the defined area:
cv::normalize(histogram,histogram,1.0);
虽然不知道为什么要归一。可能是算法要求吧!
然后就是有关这个Backprojecting的解释,书上说这个用法和calcHist很类似。只不过,<Backprojecting a histogram consists in replacing each pixel value in an input image by its corresponding probability value read in the normalized histogram.>
稍微翻译一下:
反向投射一个直方图组成了一副把每一个像素替换为改像素符合归一化的直方图的概率。
这句话确实有点复杂。咱先不管他继续看
<The result is the following probability map, with probabilities from bright (low probability) to dark (high probability) of belonging to the reference area:>
第二句就很好懂了,投射结果,亮的地方代表符合查找结果可能性小的地方,黑的地方就可能性大了。
cv::calcBackProject(&image,
1, // one image
channels, // the channels used
histogram, // the histogram we are backprojecting
result, // the resulting back projection image
ranges, // the range of values, for each dimension
255.0 // a scaling factor
);
至于那个255.0 比例因子 可能比较难理解,后来查了下资料也就明白了。因为back projecting 计算的是概率,因此每个像素是[0,1]的浮点数,所以乘以255.0
把它变为8位的图片!
至于书上解释道为什么灰度查找效果不好的原因是,灰度图像的其他部分可能也会存在和目标相似的亮度,为了避免这个问题可以使用彩色的图像!
其实思路也比较简单,就是给查找目标的直方图,然后通过这个Backprojecting方法就OK。
这里要注意:
首先是计算目标区域的直方图
Histogram1D h;
cv::MatND hist= h.getHistogram(imageROI);
然后还必须归一化(前篇日志有提到)以下是原文:
By normalizing this histogram, we obtain a function that gives the probability of a pixel of a given intensity value to belong to the defined area:
cv::normalize(histogram,histogram,1.0);
虽然不知道为什么要归一。可能是算法要求吧!
然后就是有关这个Backprojecting的解释,书上说这个用法和calcHist很类似。只不过,<Backprojecting a histogram consists in replacing each pixel value in an input image by its corresponding probability value read in the normalized histogram.>
稍微翻译一下:
反向投射一个直方图组成了一副把每一个像素替换为改像素符合归一化的直方图的概率。
这句话确实有点复杂。咱先不管他继续看
<The result is the following probability map, with probabilities from bright (low probability) to dark (high probability) of belonging to the reference area:>
第二句就很好懂了,投射结果,亮的地方代表符合查找结果可能性小的地方,黑的地方就可能性大了。
cv::calcBackProject(&image,
1, // one image
channels, // the channels used
histogram, // the histogram we are backprojecting
result, // the resulting back projection image
ranges, // the range of values, for each dimension
255.0 // a scaling factor
);
至于那个255.0 比例因子 可能比较难理解,后来查了下资料也就明白了。因为back projecting 计算的是概率,因此每个像素是[0,1]的浮点数,所以乘以255.0
把它变为8位的图片!
至于书上解释道为什么灰度查找效果不好的原因是,灰度图像的其他部分可能也会存在和目标相似的亮度,为了避免这个问题可以使用彩色的图像!
随后就是
meanshift去搜索目标了,以下是meanshift的相关介绍。
http://www.cnblogs.com/liqizhou/archive/2012/05/12/2497220.html
未完待续
meanshift去搜索目标了,以下是meanshift的相关介绍。
http://www.cnblogs.com/liqizhou/archive/2012/05/12/2497220.html
未完待续