1.calcHist
计算一组数组的直方图。
C++:void calcHist
(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )
C++:void calcHist
(const Mat* images, int nimages, const int* channels, InputArray mask, SparseMat& hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )
C:void cvCalcHist
(IplImage** image, CvHistogram* hist, int accumulate=0, const CvArr* mask=NULL )
Parameters: |
|
函数calcHist计算一个或多个数组的直方图。 用于增加直方图bin的元组的元素取自同一位置的相应输入数组。 下面的示例显示了如何为彩色图像计算2D色相饱和度直方图。
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat src, hsv;
if( argc != 2 || !(src=imread(argv[1], 1)).data )
return -1;
cvtColor(src, hsv, CV_BGR2HSV);
// Quantize the hue to 30 levels
// and the saturation to 32 levels
int hbins = 30, sbins = 32;
int histSize[] = {hbins, sbins};
// hue varies from 0 to 179, see cvtColor
float hranges[] = { 0, 180 };
// saturation varies from 0 (black-gray-white) to
// 255 (pure spectrum color)
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist;
// we compute the histogram from the 0-th and 1-st channels
int channels[] = {0, 1};
calcHist( &hsv, 1, channels, Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );
double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
int scale = 10;
Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = hist.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( histImg, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}
namedWindow( "Source", 1 );
imshow( "Source", src );
namedWindow( "H-S Histogram", 1 );
imshow( "H-S Histogram", histImg );
waitKey();
}
Note
- An example for creating histograms of an image can be found at opencv_source_code/samples/cpp/demhist.cpp
2.calcBackProject
计算直方图的反投影。
C++:void calcBackProject
(const Mat* images, int nimages, const int* channels, InputArray hist, OutputArray backProject, const float** ranges, double scale=1, bool uniform=true )
C++:void calcBackProject
(const Mat* images, int nimages, const int* channels, const SparseMat& hist, OutputArray backProject, const float** ranges, double scale=1, bool uniform=true )
C:void cvCalcBackProject
(IplImage** image, CvArr* backProject, const CvHistogram* hist)
Parameters: |
|
函数calcBackProject计算直方图的后向项目。也就是说,类似于calcHist,该函数在每个位置(x,y)收集输入图像中所选通道的值,并找到对应的直方图bin。但是该函数不是增加它,而是读取bin值,按比例缩放它的值,然后存储在backProject(x,y)中。在统计方面,该函数根据直方图表示的经验概率分布计算每个元素值的概率。例如,查看如何在场景中查找和跟踪色彩鲜艳的对象:
跟踪之前,请向相机展示物体,以使其几乎覆盖整个画面。计算色调直方图。直方图可能具有很强的最大值,对应于对象中的主要颜色。
跟踪时,请使用该预先计算的直方图计算每个输入视频帧的色相平面的反投影。限制背投阈值以抑制较弱的色彩。抑制色彩饱和度不足且像素太暗或太亮的像素也可能是有意义的。
在结果图片中找到连接的组件,然后选择最大的组件。
This is an approximate algorithm of the CamShift() color object tracker.
See also
3.compareHist
比较两个直方图。
C++:double compareHist
(InputArray H1, InputArray H2, int method)
C++:double compareHist
(const SparseMat& H1, const SparseMat& H2, int method)
C:double cvCompareHist
(const CvHistogram* hist1, const CvHistogram* hist2, int method)
Parameters: |
|
compareHist函数使用指定的方法比较两个密集或两个稀疏直方图:
- Correlation (
method=CV_COMP_CORREL
)
where
andis a total number of histogram bins.
- Chi-Square (
method=CV_COMP_CHISQR
)
- Intersection (
method=CV_COMP_INTERSECT
)
- Bhattacharyya distance (
method=CV_COMP_BHATTACHARYYA
ormethod=CV_COMP_HELLINGER
). In fact, OpenCV computes Hellinger distance, which is related to Bhattacharyya coefficient.
The function returns
.
虽然此功能可以很好地处理1、2、3维密集直方图,但它可能不适用于高维稀疏直方图。 在这种直方图中,由于混叠和采样问题,非零直方图块的坐标可能会略有偏移。 若要比较此类直方图或加权点的更一般的稀疏配置,请考虑使用EMD()函数。
4.EMD
计算两个加权点配置之间的“最小功”距离。
C++:float EMD
(InputArray signature1, InputArray signature2, int distType, InputArray cost=noArray(), float* lowerBound=0, OutputArray flow=noArray() )C:float cvCalcEMD2
(const CvArr* signature1, const CvArr* signature2, int distance_type, CvDistanceFunction distance_func=NULL, const CvArr* cost_matrix=NULL, CvArr* flow=NULL, float* lower_bound=NULL, void* userdata=NULL )
Parameters: |
where
|
该函数计算推土机距离和/或两个加权点配置之间的距离的下边界。 [RubnerSept98]中描述的应用之一是用于图像检索的多维直方图比较。 EMD是使用单形算法的某些修改解决的运输问题,因此,在最坏的情况下,复杂度是指数级的,但是平均而言,它要快得多。 在实数的情况下,下边界可以更快地计算(使用线性时间算法),并且可以用来大致确定两个签名是否足够远,以致它们不能与同一对象相关。
5.equalizeHist
均衡灰度图像的直方图。
C++:void equalizeHist
(InputArray src, OutputArray dst)C:void cvEqualizeHist
(const CvArr* src, CvArr* dst)
Parameters: |
|
该函数使用以下算法均衡输入图像的直方图:
- Calculate the histogram
- for
src
. - Normalize the histogram so that the sum of histogram bins is 255.
- Compute the integral of the histogram:
- Transform the image using
- as a look-up table:
该算法对亮度进行归一化并增加图像的对比度。
6.Extra Histogram Functions (C API)
本节的其余部分描述了在CvHistogram上运行的其他C函数。
7.CalcBackProjectPatch
通过使用直方图比较在图像中找到模板。
C:void cvCalcBackProjectPatch
(IplImage** images, CvArr* dst, CvSize patch_size, CvHistogram* hist, int method, double factor)
Parameters: |
|
该功能通过将源图像块的直方图与给定的直方图进行比较来计算反投影。 该函数与matchTemplate()类似,但是函数CalcBackProjectPatch比较直方图,而不是将栅格面片与其在搜索窗口内的所有可能位置进行比较。 请参见下面的算法图:
8.CalcProbDensity
将一个直方图除以另一个。
C:void cvCalcProbDensity
(const CvHistogram* hist1, const CvHistogram* hist2, CvHistogram* dst_hist, double scale=255 )
Parameters: |
|
该函数根据两个直方图计算目标概率密度:
9.ClearHist
清除直方图。
C:void cvClearHist
(CvHistogram* hist)
Parameters: | hist – Histogram. |
The function sets all of the histogram bins to 0 in case of a dense histogram and removes all histogram bins in case of a sparse array.
10.CopyHist
复制直方图。
C:void cvCopyHist
(const CvHistogram* src, CvHistogram** dst)
Parameters: |
|
该函数复制直方图。 如果第二个直方图指针* dst为NULL,则创建一个与src大小相同的新直方图。 否则,两个直方图必须具有相同的类型和大小。 然后,该函数将源直方图的bin值复制到目标直方图,并设置与src中相同的bin值范围。
11.CreateHist
创建直方图。
C:CvHistogram* cvCreateHist
(int dims, int* sizes, int type, float** ranges=NULL, int uniform=1 )Python:cv.
CreateHist
(dims, type, ranges=None, uniform=1) → hist
Parameters: |
|
该函数创建指定大小的直方图,并返回指向创建的直方图的指针。 如果数组范围是0,则必须稍后通过函数SetHistBinRanges()指定直方图bin范围。 尽管CalcHist()和CalcBackProject()可以在不设置bin范围的情况下处理8位图像,但它们假定它们在0到255个bin中等间隔。
12.GetMinMaxHistValue
查找直方图最小和最大值。
C:void cvGetMinMaxHistValue
(const CvHistogram* hist, float* min_value, float* max_value, int* min_idx=NULL, int* max_idx=NULL )Python:cv.
GetMinMaxHistValue
(hist)-> (min_value, max_value, min_idx, max_idx)
Parameters: |
|
该函数查找最小和最大直方图块及其位置。 所有输出参数都是可选的。 在具有相同值的几个极值中,返回具有最小索引(按词典顺序)的极值。 如果有几个最大值或最小值,则以字典顺序(极值位置)中最早的形式返回。
13.MakeHistHeaderForArray
从数组中制作直方图。
C:CvHistogram* cvMakeHistHeaderForArray
(int dims, int* sizes, CvHistogram* hist, float* data, float** ranges=NULL, int uniform=1 )
Parameters: |
|
该函数初始化直方图,其标题和箱由用户分配。 之后不需要调用ReleaseHist()。 这样只能初始化密集的直方图。 该函数返回hist。
14.NormalizeHist
标准化直方图。
C:void cvNormalizeHist
(CvHistogram* hist, double factor)
Parameters: |
|
该函数通过缩放直方图bin来对其进行归一化,以使bin的总和等于因子。
15.ReleaseHist
释放掉直方图。
C:void cvReleaseHist
(CvHistogram** hist)
Parameters: |
|
该函数释放直方图(标题和数据)。 直方图的指针由该功能清除。 如果* hist指针已经为NULL,则该函数不执行任何操作。
16.SetHistBinRanges
设置直方图块的边界。
C:void cvSetHistBinRanges
(CvHistogram* hist, float** ranges, int uniform=1 )
Parameters: |
|
这是用于在直方图中设置bin范围的独立功能。 有关参数范围和统一性的更详细说明,请参见也可以初始化范围的CalcHist()函数。 在计算直方图或计算直方图的反向投影之前,必须设置直方图bin的范围。
17.ThreshHist
阈值直方图。
C:void cvThreshHist
(CvHistogram* hist, double threshold)¶
Parameters: |
|
该功能清除低于指定阈值的直方图区。