Accessing Histograms

There are several ways to access a histogram's data. The most straightforward method is to use OpenCV's accessor functions.

double cvQueryHistValue_1D(
  CvHistogram* hist,
  int          idx0
);
double cvQueryHistValue_2D(
  CvHistogram* hist,
  int          idx0,
  int          idx1
);
double cvQueryHistValue_3D(
  CvHistogram* hist,
  int          idx0,
  int          idx1,
  int          idx2
);
double cvQueryHistValue_nD(
  CvHistogram* hist,
  int*         idxN
);

Each of these functions returns a floating-point number for the value in the appropriate bin. Similarly, you can set (or get) histogram bin values with the functions that return a pointer to a bin (not to a bin's value):

float* cvGetHistValue_1D(
  CvHistogram* hist,
  int          idx0
);
float* cvGetHistValue_2D(
  CvHistogram* hist,
  int          idx0,
  int          idx1
);
float* cvGetHistValue_3D(
  CvHistogram* hist,
  int          idx0,
  int          idx1,
  int          idx2
);
float* cvGetHistValue_nD(
  CvHistogram* hist,
  int*         idxN
);

These functions look a lot like the cvGetReal*D and cvPtr*D families of functions, and in fact they are pretty much the same thing. Inside of these calls are essentially those same matrix accessors called with the matrix hist->bins passed on to them. Similarly, the functions for sparse histograms inherit the behavior of the corresponding sparse matrix functions. If you attempt to access a nonexistent bin using a GetHist*() function in a sparse histogram, then that bin is automatically created and its value set to 0. Note that QueryHist*() functions do not create missing bins.

This leads us to the more general ...

Get Learning OpenCV now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.