Opencv学习笔记三-直方图点运算(直方图基本点算子、直方图正规化、直方图均衡化)

三:直方图均衡化

直方图均衡化是一个非线性处理过程,其目的是通过一种合适人类视觉分析的方法来增强图像的亮度。它对图像进行改变,使得图像具有更平坦的直方图,所有亮度及等概率出现。其数学原理请参照: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);
	}
}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值