My question is when we normalize the histogram , is there any build-in function for that , if not than obviously we can calculate the histogram of the image using the function calcHist()
, but the formula of normalizing histogram is Nk/N
so what calcHist
return us is N
in this formula , or we have to calculate N
on our own , and whats its role in entropy formula
Asked
Active
Viewed 9,005 times
0

Rocket
- 553
- 8
- 31
1 Answers
2
I am not sure I get your question. But here is a simple example of how to get the l1 normalised histogram of a grayscale image with OpenCV.
In case of an image N
is the number of pixels which can be computed simply by multiplying the width and the height of the image. Then it is simply a matter of dividing the histogram by N
.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char** argv)
{
Mat img = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
Mat hist;
int channels[] = {0};
int histSize[] = {32};
float range[] = { 0, 256 };
const float* ranges[] = { range };
calcHist( &img, 1, channels, Mat(), // do not use mask
hist, 1, histSize, ranges,
true, // the histogram is uniform
false );
Mat histNorm = hist / (img.rows * img.cols);
return 0;
}
To get the example I modified the one from the OpenCV documentation.
If you want to compute the entropy with this histogram, you can do this:
double entropy = 0.0;
for (int i=0; i<histNorm.rows; i++)
{
float binEntry = histNorm.at<float>(i,0);
if (binEntry != 0.0)
entropy -= binEntry * log2(binEntry);
}
-
I want to subtract the two frame with sufficient time gap ,so that entropy clearly represent the area where change occured , `if (binEntry == 0.0)` than it lead to infinity , what can do for this – Rocket Sep 01 '13 at 17:25
-
Just handle them as I did in my example: Ignore the empty bins. – sietschie Sep 01 '13 at 18:41
-
how to add the time gap ? any info about that ? – Rocket Sep 01 '13 at 18:42
-
You simply save on frame, wait a certain amount of time and save another frame. As simple as that. If you have problems with that, maybe you can open another question where you show, what you have got so far and at which point you have problems. – sietschie Sep 01 '13 at 20:02
-
This means your compiler does not support it. But you can simply emulate it by computing `log(value)/log(2.0)` instead. See also [this question](http://stackoverflow.com/questions/758001/log2-not-found-in-my-math-h) – sietschie Sep 02 '13 at 20:30
-
Don't you think your loop should be something like this : `for (int i=0; i<histNorm.rows; i++) { float binEntry = histNorm.at<float>(i,0);` because your loop focus is only on one row ? which is deciding about the whole image – Rocket Sep 03 '13 at 11:21
-
We are looping over a histogram, which is one dimensional. Not the two dimensional image. – sietschie Sep 03 '13 at 12:34
-
but my image is 2dimensional , isn't it affect ? – Rocket Sep 03 '13 at 12:54
-
Not if you make computations on the histogram and not on the image. – sietschie Sep 03 '13 at 19:45