Skip to content

Instantly share code, notes, and snippets.

@ankurparihar
Created February 24, 2020 11:29
Show Gist options
  • Save ankurparihar/ea10782fae8193cac4449e0bb2e84523 to your computer and use it in GitHub Desktop.
Save ankurparihar/ea10782fae8193cac4449e0bb2e84523 to your computer and use it in GitHub Desktop.
The development of the algorithm for the luminance component [Paper:Novel Histogram Processing for Colour Image Enhancement]
#include <stdio.h>
float H(int); // Histogram data function
int value = 0; // color value [0-255] used in result
int result[255]; // store the ranges
int max_recursion = 8; // 8 recursion as mentioned in paper 2^8 = 256 intervals
/**
* Find intermediate cut C for which sum(H[start-C]) is equal to sum(H[C-end])
* for a given range
*
* start - start of range (included)
* end - end of range (excluded)
* alpha - user defined parameter
* depth - recursion depth (program concluded when maximum depth is reached)
*
*/
void findCutAndDivide(int start, int end, float alpha, int depth){
if(depth == max_recursion) {
// write color value to result array
// when we are at lowest level of recursion
value++; // update global value
for(int i=start; i<end; ++i){
result[i] = value;
}
return;
}
float left_count = 0.0f; // sum on left side from start
float total_count = 0.0f; // total sum of histogram values from start to end (excluded)
int beta, C;
// count total histogram sum
for(int i=start; i<end; ++i){
total_count += H(i);
}
// find a beta which divides histogram sum in two parts
for(int i=start; i<end; ++i){
left_count += H(i);
if(left_count >= total_count - left_count) {
beta = i;
break;
}
}
// Pivot point using formula given in paper
C = (int)(((float)(0 + 255) / 2) + alpha * (beta - ((float)(0 + 255) / 2)));
// Repeat same procudure recursively for left and right partitions
// This orderering ensures that data is processed from left to right
findCutAndDivide(start, C, alpha, depth + 1);
findCutAndDivide(C, end, alpha, depth + 1);
}
int main(){
float alpha = 0.0f; // 0 <= α <= 1, user defined parameter
// Initialize result
for(int i=0; i<256; ++i) result[i] = 0;
// Start procecure
findCutAndDivide(0, 256, alpha, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment