三:直方图均衡化
直方图均衡化是一个非线性处理过程,其目的是通过一种合适人类视觉分析的方法来增强图像的亮度。它对图像进行改变,使得图像具有更平坦的直方图,所有亮度及等概率出现。其数学原理请参照:http://www.cnblogs.com/cfantaisie/archive/2011/06/05/2073406.html和附件:http://download.csdn.net/detail/fullyfulei/5303040。
直方图均衡化OpenCV实现:
http://blog.csdn.net/xiaowei_cqu/article/details/7606607
直方图均衡化C语言实现:(网上找的代码自己并未测试过,看过原理实现应该比较简单)
http://blog.sina.com.cn/s/blog_633b2a530101cyxh.html
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
unsigned char **get_matrix_space(int m,int n)
{
int i;
unsigned char **a;
a=(unsigned char **)calloc(m,sizeof(unsigned char *));
for(i=0;i<m;i++)a[i]=(unsigned char *)calloc(n,sizeof(unsigned char));
return a;
}
main(){
FILE *fs,*ft;
char c1,c2;
int width,height,L,i,j,max,min;
int n[256]={0};
double p[256]={0};
double c[256]={0};
unsigned char **s,**t,**get_matrix_space(int,int);
if((fs= fopen("source.pgm","rb")) ==NULL){
printf("can't open %s\n","source.pgm");
exit(1);
}
if((ft = fopen("destination.pgm","wb")) ==NULL){
printf("can't open %s\n","destination.pgm");
exit(1);
}
fscanf(fs,"%c%c\n%d%d\n%d\n",&c1,&c2,&width,&height,&L);
s=get_matrix_space(height,width);
t=get_matrix_space(height,width);
for(i=0;i<height;i++){
for(j=0;j<width;j++){
fread(&s[i][j],sizeof(unsigned char),1,fs);
}
}
for(i=0;i<height;i++){
for(j=0;j<width;j++){
n[s[i][j]]++;
}
}
for(i=0;i<256;i++){
p[i] = (double)n[i]/(height*width);
}
for(i=0;i<256;i++){
for(j=0;j<=i;j++){
c[i]+=p[j];
}
}
max=min=s[0][0];
for(i=0;i<height;i++){
for(j=0;j<width;j++){
if(max<s[i][j]){max=s[i][j];}else if(min>s[i][j]){min=s[i][j];}
}
}
printf("%d %d\n",max,min);
for(i=0;i<height;i++){
for(j=0;j<width;j++){
t[i][j]=c[s[i][j]]*(max-min)+min;
}
}
fprintf(ft,"%c%c\n%d %d\n%d\n",'P','5',width,height,L);
for(i=0;i<height;i++){
for(j=0;j<width;j++){
fwrite(&t[i][j],sizeof(unsigned char),1,ft);
}
}
}