Calculates a histogram of a set of arrays.
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 )¶
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 )¶
cv2.
calcHist
(images, channels, mask, histSize, ranges[, hist[, accumulate]]) → hist¶
void cvCalcHist
(IplImage** image, CvHistogram* hist, int accumulate=0, const CvArr* mask=NULL )¶
cv.
CalcHist
(image, hist, accumulate=0, mask=None) → None¶Parameters: |
|
---|
The functions calcHist
calculate the histogram of one or more
arrays. The elements of a tuple used to increment
a histogram bin are taken from the corresponding
input arrays at the same location. The sample below shows how to compute a 2D Hue-Saturation histogram for a color image.
#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
Calculates the back projection of a histogram.
void calcBackProject
(const Mat* images, int nimages, const int* channels, InputArray hist, OutputArray backProject, const float** ranges, double scale=1, bool uniform=true )¶
void calcBackProject
(const Mat* images, int nimages, const int* channels, const SparseMat& hist, OutputArray backProject, const float** ranges, double scale=1, bool uniform=true )¶
cv2.
calcBackProject
(images, channels, hist, ranges, scale[, dst]) → dst¶
void cvCalcBackProject
(IplImage** image, CvArr* backProject, const CvHistogram* hist)¶
cv.
CalcBackProject
(image, back_project, hist) → None¶Parameters: |
|
---|
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. See how, for example, you can find and track a bright-colored object in a scene:
This is an approximate algorithm of the
CamShift()
color object tracker.
See also
Compares two histograms.
double compareHist
(InputArray H1, InputArray H2, int method)¶
double compareHist
(const SparseMat& H1, const SparseMat& H2, int method)¶
cv2.
compareHist
(H1, H2, method) → retval¶
double cvCompareHist
(const CvHistogram* hist1, const CvHistogram* hist2, int method)¶
cv.
CompareHist
(hist1, hist2, method) → float¶Parameters: |
|
---|
The functions compareHist
compare two dense or two sparse histograms using the specified method:
Correlation (method=CV_COMP_CORREL
)
where
and is a total number of histogram bins.
Chi-Square (method=CV_COMP_CHISQR
)
Intersection (method=CV_COMP_INTERSECT
)
Bhattacharyya distance (method=CV_COMP_BHATTACHARYYA
or method=CV_COMP_HELLINGER
). In fact, OpenCV computes Hellinger distance, which is related to Bhattacharyya coefficient.
The function returns .
While the function works well with 1-, 2-, 3-dimensional dense histograms, it may not be suitable for high-dimensional sparse histograms. In such histograms, because of aliasing and sampling problems, the coordinates of non-zero histogram bins can slightly shift. To compare such histograms or more general sparse configurations of weighted points, consider using the
EMD()
function.
Computes the “minimal work” distance between two weighted point configurations.
float EMD
(InputArray signature1, InputArray signature2, int distType, InputArray cost=noArray(), float* lowerBound=0, OutputArray flow=noArray() )¶
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 )¶
cv.
CalcEMD2
(signature1, signature2, distance_type, distance_func=None, cost_matrix=None, flow=None, lower_bound=None, userdata=None) → float¶Parameters: |
|
---|
The function computes the earth mover distance and/or a lower boundary of the distance between the two weighted point configurations. One of the applications described in [RubnerSept98] is multi-dimensional histogram comparison for image retrieval. EMD is a transportation problem that is solved using some modification of a simplex algorithm, thus the complexity is exponential in the worst case, though, on average it is much faster. In the case of a real metric the lower boundary can be calculated even faster (using linear-time algorithm) and it can be used to determine roughly whether the two signatures are far enough so that they cannot relate to the same object.
Equalizes the histogram of a grayscale image.
void equalizeHist
(InputArray src, OutputArray dst)¶
cv2.
equalizeHist
(src[, dst]) → dst¶
void cvEqualizeHist
(const CvArr* src, CvArr* dst)¶Parameters: |
|
---|
The function equalizes the histogram of the input image using the following algorithm:
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:
The algorithm normalizes the brightness and increases the contrast of the image.
The rest of the section describes additional C functions operating on CvHistogram
.
Locates a template within an image by using a histogram comparison.
void cvCalcBackProjectPatch
(IplImage** images, CvArr* dst, CvSize patch_size, CvHistogram* hist, int method, double factor)¶
cv.
CalcBackProjectPatch
(images, dst, patch_size, hist, method, factor) → None¶Parameters: |
|
---|
The function calculates the back projection by comparing histograms of the source image patches with the given histogram. The function is similar to matchTemplate()
, but instead of comparing the raster patch with all its possible positions within the search window, the function CalcBackProjectPatch
compares histograms. See the algorithm diagram below:
Divides one histogram by another.
void cvCalcProbDensity
(const CvHistogram* hist1, const CvHistogram* hist2, CvHistogram* dst_hist, double scale=255 )¶
cv.
CalcProbDensity
(hist1, hist2, dst_hist, scale=255) → None¶Parameters: |
|
---|
The function calculates the object probability density from two histograms as:
Clears the histogram.
void cvClearHist
(CvHistogram* hist)¶
cv.
ClearHist
(hist) → None¶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.
Copies a histogram.
void cvCopyHist
(const CvHistogram* src, CvHistogram** dst)¶Parameters: |
|
---|
The function makes a copy of the histogram. If the second histogram pointer *dst
is NULL, a new histogram of the same size as src
is created. Otherwise, both histograms must have equal types and sizes. Then the function copies the bin values of the source histogram to the destination histogram and sets the same bin value ranges as in src
.
Creates a histogram.
CvHistogram* cvCreateHist
(int dims, int* sizes, int type, float** ranges=NULL, int uniform=1 )¶
cv.
CreateHist
(dims, type, ranges=None, uniform=1) → hist¶Parameters: |
|
---|
The function creates a histogram of the specified size and returns a pointer to the created histogram. If the array ranges
is 0, the histogram bin ranges must be specified later via the function SetHistBinRanges()
. Though CalcHist()
and CalcBackProject()
may process 8-bit images without setting bin ranges, they assume they are equally spaced in 0 to 255 bins.
Finds the minimum and maximum histogram bins.
void cvGetMinMaxHistValue
(const CvHistogram* hist, float* min_value, float* max_value, int* min_idx=NULL, int* max_idx=NULL )¶
cv.
GetMinMaxHistValue
(hist)-> (min_value, max_value, min_idx, max_idx)¶Parameters: |
|
---|
The function finds the minimum and maximum histogram bins and their positions. All of output arguments are optional. Among several extremas with the same value the ones with the minimum index (in the lexicographical order) are returned. In case of several maximums or minimums, the earliest in the lexicographical order (extrema locations) is returned.
Makes a histogram out of an array.
CvHistogram* cvMakeHistHeaderForArray
(int dims, int* sizes, CvHistogram* hist, float* data, float** ranges=NULL, int uniform=1 )¶Parameters: |
|
---|
The function initializes the histogram, whose header and bins are allocated by the user. ReleaseHist()
does not need to be called afterwards. Only dense histograms can be initialized this way. The function returns hist
.
Normalizes the histogram.
void cvNormalizeHist
(CvHistogram* hist, double factor)¶
cv.
NormalizeHist
(hist, factor) → None¶Parameters: |
|
---|
The function normalizes the histogram bins by scaling them so that the sum of the bins becomes equal to factor
.
Releases the histogram.
void cvReleaseHist
(CvHistogram** hist)¶Parameters: |
|
---|
The function releases the histogram (header and the data). The pointer to the histogram is cleared by the function. If *hist
pointer is already NULL
, the function does nothing.
Sets the bounds of the histogram bins.
void cvSetHistBinRanges
(CvHistogram* hist, float** ranges, int uniform=1 )¶Parameters: |
|
---|
This is a standalone function for setting bin ranges in the histogram. For a more detailed description of the parameters ranges
and uniform
, see the CalcHist()
function that can initialize the ranges as well. Ranges for the histogram bins must be set before the histogram is calculated or the backproject of the histogram is calculated.
Thresholds the histogram.
void cvThreshHist
(CvHistogram* hist, double threshold)¶
cv.
ThreshHist
(hist, threshold) → None¶Parameters: |
|
---|
The function clears histogram bins that are below the specified threshold.
[RubnerSept98] |
|