Calculates histogram of a set of arrays
Parameters: |
|
---|
The functions calcHist calculate the histogram of one or more arrays. The elements of a tuple that is used to increment a histogram bin are taken at the same location from the corresponding input arrays. The sample below shows how to compute 2D Hue-Saturation histogram for a color imag
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat src;
if( argc != 2 || !(src=imread(argv[1], 1)).data )
return -1;
Mat hsv;
cvtColor(src, hsv, CV_BGR2HSV);
// let's 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/maxValue);
cvRectangle( 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();
}
Calculates the back projection of a histogram.
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. Here is how, for example, you can find and track a bright-colored object in a scene:
That is the approximate algorithm of CAMShift() color object tracker.
See also: calcHist()
Compares two histograms
Parameters: |
|
---|
The functions compareHist compare two dense or two sparse histograms using the specified method:
Correlation (method=CV_COMP_CORREL)
where
and is the total number of histogram bins.
Chi-Square (method=CV_COMP_CHISQR)
Intersection (method=CV_COMP_INTERSECT)
Bhattacharyya distance (method=CV_COMP_BHATTACHARYYA)
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, where, 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 calcEMD() function.
Equalizes the histogram of a grayscale image.
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.