-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistogram-equalization.cpp
executable file
·54 lines (43 loc) · 1.23 KB
/
histogram-equalization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hist-equ.h"
void histogram(int * hist_out, unsigned char * img_in, int img_size, int nbr_bin){
int i;
for ( i = 0; i < nbr_bin; i ++){
hist_out[i] = 0;
}
for ( i = 0; i < img_size; i ++){
hist_out[img_in[i]] ++;
}
}
void histogram_equalization(unsigned char * img_out, unsigned char * img_in,
int * hist_in, int img_size, int nbr_bin){
int *lut = (int *)malloc(sizeof(int)*nbr_bin);
int i, cdf, min, d;
/* Construct the LUT by calculating the CDF */
cdf = 0;
min = 0;
i = 0;
while(min == 0){
min = hist_in[i++];
}
d = img_size - min;
for(i = 0; i < nbr_bin; i ++){
cdf += hist_in[i];
//lut[i] = (cdf - min)*(nbr_bin - 1)/d;
lut[i] = (int)(((float)cdf - min)*255/d + 0.5);
if(lut[i] < 0){
lut[i] = 0;
}
}
/* Get the result image */
for(i = 0; i < img_size; i ++){
if(lut[img_in[i]] > 255){
img_out[i] = 255;
}
else{
img_out[i] = (unsigned char)lut[img_in[i]];
}
}
}