I am using the normalise function in the code below. My understanding was that normalising the histogram would result in the bins summing to one? But when I add them all up I keep getting a result higher then one. I dont know if I am doing something wrong or have misunderstood what the function does?

//read in image
Mat img = imread("image.jpg",1);
vector<Mat> planes;
split(img, planes);

//calculate hist
MatND hist;
int nbins = 256; // hold 256 levels
int hsize[] = { nbins }; // one dimension
float range[] = { 0, 255 };
const float *ranges[] = { range };
int chnls[] = {0};
calcHist(&planes[0], 1, chnls, Mat(), hist,1,hsize,ranges);

//normalise
normalize(hist,hist,1);

float tot = 0;
for( int n = 0;n < nbins; n++ )
    {
        float binVal = hist.at<float>(n);
        tot+=binVal;
    }
cout<<tot;
有帮助吗?

解决方案

Normalized array doesn't sum to 1, but square root of sum of squares of components equals 1, F.e. in vector:

It is normalized, when: sqrt(x^2 + y^2 + z^2) = 1

*This applies for vectors

in OpenCV - histogram - normalize is described here OpenCV histogram normalize, it should be clear (after reading the specs) that it doesn't have to sum to 1

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top