Ask Your Question

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

asked 2013-04-22 08:39:55 -0600

btdan gravatar image

updated 2019-12-09 08:04:32 -0600

Akhil Patel gravatar image

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 flag offensive 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.

Abid Rahman K gravatar imageAbid Rahman K ( 2013-04-23 01:39:03 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-09-06 14:16:24 -0600

MikeTronix gravatar image

updated 2018-09-06 14:16:51 -0600

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;
    // mask is from thresholding all data above 31 counts
    cv::Mat mask8 = cv::Mat::zeros(nrows,ncols,CV_8UC1);
    // 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
edit flag offensive delete link more

Question Tools


Asked: 2013-04-22 08:39:55 -0600

Seen: 338 times

Last updated: Sep 06 '18