Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 cv::Mat mask8 = cv::Mat::zeros(nrows,ncols,CV_8UC1); // mask8 values are 0 or 255 cv::threshold(fdata,mask8,31,255,cv::THRESH_BINARY); // 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; cv::calcHist(&fdata,1,&chan,mask8,hist,1,&hist_sz,ranges,true,false); // 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

Here is some sample C++ code extracted from a current throwaway project using openCV 3.4.1. Hope this helps. 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
    cv::Mat mask8 = cv::Mat::zeros(nrows,ncols,CV_8UC1);
    // mask8 values are 0 or 255
    cv::threshold(fdata,mask8,31,255,cv::THRESH_BINARY);
    // 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;
    cv::calcHist(&fdata,1,&chan,mask8,hist,1,&hist_sz,ranges,true,false);
    // 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

column