Normalize 3D histogram - Sum to 1

asked 2014-02-02 09:44:41 -0600

CalumMurray gravatar image

updated 2014-02-02 09:56:44 -0600

I've built a 3D Histogram from H-S-V samples from an (CV_8UC3) image.

I need to normalize this histogram so that all the values sum to 1.0 (preferrably in a float representation), since it will be used as a probability mass function (pmf) for a lookup table.

I've tried various permutations of built-in OpenCV functions, but none seem to give the desired result.

int histSize[] = {hBins, sBins, vBins};
float hRange[] = {0.0f, (float)H_RANGE};
float sRange[] = {0.0f, (float)S_RANGE};
float vRange[] = {0.0f, (float)V_RANGE};
const float* ranges[] = {hRange, sRange, vRange};
const int channels[]  = {0, 1, 2}; // we compute the 3D histogram on all 2 channels (H-S-V)

    cv::calcHist(&newBGSamples, 1, channels, cv::Mat(), currentBGColourHist, 3, histSize, ranges, true, false);
//  currentBGColourHist /= cv::sum(bgHistoricalColourHist)(0);
    cv::normalize(currentBGColourHist, currentBGColourHist, 1.0, 1.0, cv::NORM_L1, CV_32FC3);
//  cv::normalize(currentBGColourHist, currentBGColourHist, 1.0, 0, cv::NORM_L2, -1, cv::Mat());
//  cv::norm(currentBGColourHist, )
//  cv::divide((double)1.0/cv::sum(bgHistoricalColourHist)(0), currentBGColourHist, currentBGColourHist, CV_32FC3);

The commented lines show my rough ideas for the normalisation.

edit retag flag offensive close merge delete