Skip to content

Instantly share code, notes, and snippets.

@uugan
Created April 23, 2013 15:12
Show Gist options
  • Select an option

  • Save uugan/5444403 to your computer and use it in GitHub Desktop.

Select an option

Save uugan/5444403 to your computer and use it in GitHub Desktop.
/* Histogram exponential equalization when alpha = 0.011 */
float exponential_histogram(unsigned char* inbuf, unsigned char* outbuf, int height, int width){
double g_min=255;
double g_max=0;
double* histogram = new double[256];
/* histogram array */
double* cdf = new double[256]; /*Pf(f) values [cumulative distribution function]*/
double* out = new double[height*width];
int start = GetTickCount();
for(int i=0;i<256;i++)
histogram[i] = 0.0;
/*Counting histogram values 0..255 */
double d = 1.0/double(height*width);
for(int i=0;i<height;i++){
for(int j=0;j<width;j++)
{
if(inbuf[i*width+j]<g_min) g_min = inbuf[i*width+j];
if(inbuf[i*width+j]>g_max) g_max = inbuf[i*width+j];
histogram[inbuf[i*width+j]]+=d;
}
}
cdf[0]=histogram[0];
for(int i=1;i<256;i++)
cdf[i]=(cdf[i-1]+histogram[i]); /*Pf(f) cumsuming*/
double max=-999,min=999,alpha=0.011;
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
out[i*width+j] = g_min - (1.0/alpha)*log(1.1 - cdf[inbuf[i*width+j]]);
if(out[i*width+j] > max) max=out[i*width+j];
if(out[i*width+j] < min) min=out[i*width+j];
}
}
/* double array to unsigned char */
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
outbuf[i*width+j]=(unsigned char)(out[i*width+j]-min)/(max - min) * 255;
}
}
return (float)(GetTickCount() - start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment