# Help:I am puzzled by cvCalcHist function,can you help me?Thank you

Hello, I am using cvCalcHist function. The defination is : void cvCalcHist( IplImage* image, CvHistogram hist,int accumulate=0, const CvArr* mask=NULL ); However, I don't know how to use the parameter mask, Can somebody give me an sample source code? Thanks very much. Best wishes to you.

edit retag close merge delete

Do you want to use mask? mask is used to find histogram of a specific region of image. If you want to find histogram of full image, just ignore it.

( 2013-04-23 01:39:03 -0600 )edit

Sort by ยป oldest newest most voted

Here is some sample C++ code extracted from a current throwaway project using openCV 3.4.1. Hope this helps.

 // input floating point image fdata of size nrows x ncols
cv::Mat fdata = cv::Mat::zeros(nrows,ncols,CV_32FC1);
// load data into fdata here
// set up to find maximum and minimum values
double minv, maxv;
cv::minMaxIdx(fdata,&minv,&maxv);
// mask is from thresholding all data above 31 counts
// mask8 values are 0 or 255
// size of histogram to create
int hist_sz = 10;
// create "tree" of pointers for binning histogram, contains max and min values
float rngs[2];
rngs[0] = static_cast<float>(minv);
rngs[1] = static_cast<float>(maxv);
const float* prng = { &rngs[0] };
const float** ranges = { &prng };
// print out values to see if tree is correct so that histogram function will not assert an error
std::cout << "ranges[0]=" << ranges[0] << " ranges[0][0]=" << ranges[0][0] <<
" ranges[0][1]=" << ranges[0][1] << std::endl;
// compute histogram on first channel
int chan = 0;
cv::MatND hist;
// histogram now only contains data counted from pixels with mask8 > 0
// note that histogram is of type float and is a cv::Mat vector with 10 rows and 1 column

more

Official site

GitHub

Wiki

Documentation