当前位置:首页 » 编程语言 » c语言图像svd
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言图像svd

发布时间: 2023-01-12 05:15:49

1. c语言的问题 关于“图”黑白图像 最后一句的s[j]哪来的,-'0'是什么意思

这个s是字符串,你看到倒数3行那个scanf("%s",s);了吧,%s就是字符串,s[j]就是当成数组处理的。-‘0’是减去字符0的ASCLL码值,为48

2. 纯C语言实现图像处理

这个不难吧?虽然我不用c,可是说下怎么做,首先打开一个BMP文件,然后按照bmp格式解析到一个数组中,接着进行二值化(这个有很多方法),最后将数组中的数据写入一个新的BMP文件就可以了。

3. 如何用C语言对图像加入高斯噪声和椒盐噪声

自己产生[x,y] 2维随机数,用分布函数为权,取用点的个数就可以了。如考虑中心对称,用极座标[r,sita] 也许更方便。

2维椒盐噪声,即产生[x,y,1] 和 [x,y,0], 1 是白点,0是黑点。

计算机的伪随机数,就是白噪声,均匀分布。

4. svd分解 c语言实现

/*
本程序在linux g++下编译通过
bool svd(vector<vector<double> > A, int K, vector<vector<double> > &U, vector<double> &S, vector<vector<double> > &V);

A: 输入待分解矩阵
K: 输入,取前K大奇异值及奇异向量
U[0],U[1],...,U[K-1]: 前K大奇异值对应的左奇异向量
S[0],S[1],...,S[K-1]: 前K大奇异值 S[0]>=S[1]>=...>=S[K-1]
V[0],V[1],...,V[K-1]: 前K大奇异值对应的右奇异向量
*/
#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <vector>
using namespace std;
const int MAX_ITER=100000;
const double eps=0.0000001;
double get_norm(double *x, int n){
double r=0;
for(int i=0;i<n;i++)
r+=x[i]*x[i];
return sqrt(r);
}
double normalize(double *x, int n){
double r=get_norm(x,n);
if(r<eps)
return 0;
for(int i=0;i<n;i++)
x[i]/=r;
return r;
}

inline double proct(double*a, double *b,int n){
double r=0;
for(int i=0;i<n;i++)
r+=a[i]*b[i];
return r;
}

void orth(double *a, double *b, int n){//|a|=1
double r=proct(a,b,n);
for(int i=0;i<n;i++)
b[i]-=r*a[i];

}

bool svd(vector<vector<double> > A, int K, vector<vector<double> > &U, vector<double> &S, vector<vector<double> > &V){
int M=A.size();
int N=A[0].size();
U.clear();
V.clear();
S.clear();
S.resize(K,0);
U.resize(K);
for(int i=0;i<K;i++)
U[i].resize(M,0);
V.resize(K);
for(int i=0;i<K;i++)
V[i].resize(N,0);


srand(time(0));
double *left_vector=new double[M];
double *next_left_vector=new double[M];
double *right_vector=new double[N];
double *next_right_vector=new double[N];
int col=0;
for(int col=0;col<K;col++){
double diff=1;
double r=-1;
while(1){
for(int i=0;i<M;i++)
left_vector[i]= (float)rand() / RAND_MAX;
if(normalize(left_vector, M)>eps)
break;
}

for(int iter=0;diff>=eps && iter<MAX_ITER;iter++){
memset(next_left_vector,0,sizeof(double)*M);
memset(next_right_vector,0,sizeof(double)*N);
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
next_right_vector[j]+=left_vector[i]*A[i][j];

r=normalize(next_right_vector,N);
if(r<eps) break;
for(int i=0;i<col;i++)
orth(&V[i][0],next_right_vector,N);
normalize(next_right_vector,N);

for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
next_left_vector[i]+=next_right_vector[j]*A[i][j];
r=normalize(next_left_vector,M);
if(r<eps) break;
for(int i=0;i<col;i++)
orth(&U[i][0],next_left_vector,M);
normalize(next_left_vector,M);
diff=0;
for(int i=0;i<M;i++){
double d=next_left_vector[i]-left_vector[i];
diff+=d*d;
}

memcpy(left_vector,next_left_vector,sizeof(double)*M);
memcpy(right_vector,next_right_vector,sizeof(double)*N);
}
if(r>=eps){
S[col]=r;
memcpy((char *)&U[col][0],left_vector,sizeof(double)*M);
memcpy((char *)&V[col][0],right_vector,sizeof(double)*N);
}else{
cout<<r<<endl;
break;
}
}
delete [] next_left_vector;
delete [] next_right_vector;
delete [] left_vector;
delete [] right_vector;

return true;
}

void print(vector<vector<double> > &A){

}
int main(){
int m=10;
int n=8;
int k=5;
//分解一个10*8的矩阵A,求其前5个奇异值和奇异向量
srand(time(0));
vector<vector<double> > A;
A.resize(m);

for(int i=0;i<m;i++){
A[i].resize(n);
for(int j=0;j<n;j++)
A[i][j]=(float)rand()/RAND_MAX-0.5;
}



cout<<"A="<<endl;
for(int i=0;i<A.size();i++){
for(int j=0;j<A[i].size();j++){
cout<<setw(12)<<A[i][j]<<' ';
}
cout<<endl;
}
cout<<endl;

vector<vector<double> > U;
vector<double> S;
vector<vector<double> > V;
svd(A,k,U,S,V);
cout<<"U="<<endl;
for(int i=0;i<U[0].size();i++){
for(int j=0;j<U.size();j++){
cout<<setw(12)<<U[j][i]<<' ';
}
cout<<endl;
}
cout<<endl;
cout<<"S="<<endl;
for(int i=0;i<S.size();i++){
cout<<setw(7)<<S[i]<<' ';
}
cout<<endl;
cout<<"V="<<endl;
for(int i=0;i<V[0].size();i++){
for(int j=0;j<V.size();j++){
cout<<setw(12)<<V[j][i]<<' ';
}
cout<<endl;
}
return 0;
}


5. 如何用C语言实现对图像的二值化

/*************************************************************************
* 该函数用于对图像进行阈值分割运算
* 参数:
* LPSTR lpDIBBits - 指向源DIB图像指针
* LONG lWidth - 源图像宽度(象素数)
* LONG lHeight - 源图像高度(象素数)
************************************************************************/

BOOL ImageChangeProc::ThresholdDIB(LPSTR lpDIBBits,LONG lWidth, LONG lHeight)
{

// 指向源图像的指针
LPSTR lpSrc;

// 指向缓存图像的指针
LPSTR lpDst;

// 指向缓存DIB图像的指针
LPSTR lpNewDIBBits;
HLOCAL hNewDIBBits;

//循环变量
long i;
long j;

unsigned char pixel;
long lHistogram[256];

//阈值,最大灰度值与最小灰度值,两个区域的平均灰度值
unsigned char Threshold,NewThreshold,MaxGrayValue,MinGrayValue,Temp1GrayValue,Temp2GrayValue;

//用于计算区域灰度平均值的中间变量
long lP1,lP2,lS1,lS2;

//迭代次数
int IterationTimes;

LONG lLineBytes;
hNewDIBBits = LocalAlloc(LHND, lWidth * lHeight);

if (hNewDIBBits == NULL)
{
// 分配内存失败
return FALSE;
}

// 锁定内存
lpNewDIBBits = (char * )LocalLock(hNewDIBBits);

// 初始化新分配的内存
lpDst = (char *)lpNewDIBBits;
memset(lpDst, (BYTE)255, lWidth * lHeight);

lLineBytes = WIDTHBYTES(lWidth * 8);

for (i = 0; i < 256;i++)
{
lHistogram[i]=0;
}

//获得直方图
MaxGrayValue = 0;
MinGrayValue = 255;
for (i = 0;i < lWidth ;i++)
{
for(j = 0;j < lHeight ;j++)
{
lpSrc = (char *)lpDIBBits + lLineBytes * j + i;

pixel = (unsigned char)*lpSrc;

lHistogram[pixel]++;
//修改最大,最小灰度值
if(MinGrayValue > pixel)
{
MinGrayValue = pixel;
}
if(MaxGrayValue < pixel)
{
MaxGrayValue = pixel;
}
}
}

//迭代求最佳阈值
NewThreshold = (MinGrayValue + MaxGrayValue)/2;
Threshold = 0;

for(IterationTimes = 0; Threshold != NewThreshold && IterationTimes < 1000;IterationTimes ++)
{
Threshold = NewThreshold;
lP1 =0;
lP2 =0;
lS1 = 0;
lS2 = 0;
//求两个区域的灰度平均值
for (i = MinGrayValue;i <=Threshold;i++)
{
lP1 += lHistogram[i]*i;
lS1 += lHistogram[i];
}

for (i = Threshold+1;i<MaxGrayValue;i++)
{
lP2 += lHistogram[i]*i;
lS2 += lHistogram[i];
}
if(lS1==0||lS2==0)
{
// 释放内存
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
return FALSE;
}
Temp1GrayValue = (unsigned char)(lP1 / lS1);
Temp2GrayValue = (unsigned char)(lP2 / lS2);
NewThreshold = (Temp1GrayValue + Temp2GrayValue)/2;
}

//根据阈值将图像二值化
for (i = 0;i < lWidth ;i++)
{
for(j = 0;j < lHeight ;j++)
{
lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
lpDst = (char *)lpNewDIBBits + lLineBytes * j + i;
pixel = (unsigned char)*lpSrc;

if(pixel <= Threshold)
{
*lpDst = (unsigned char)0;
}
else
{
*lpDst = (unsigned char)255;
}
}
}

// 复制图像
memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);

// 释放内存
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);

// 返回
return TRUE;
}

参考:http://topic.csdn.net/t/20030909/13/2240079.html

6. 关于C语言处理图像的问题

#include <Bitmap.h>//引用处理图片的头文件

FILE *hFile = NULL;//文件指针
int LoadBitmapFile ( char *fileName, BITMAP_IMAGE_PTR bitmap )
{
int hImageFile = 0; //文件读取成功的标识
unsigned char *workingBuffer = 0; //保存读取的图片
hImageFile = _open ( fileName, _O_RDONLY ); //以只读方式打开图片文件,成功返回1,失败返回-1

if ( hImageFile==-1 )//如果打开失败,则输出警告
{
WriteLogFile ( "LoadBitmapFile function error! \nCan not open the bitmap.\n" );
return 0;

}

这只是最基本的打开图片文件,跟打开普通文件没什么区别,后面要对图片进行处理的话没有图形学的知识会很困难,如果想进一步研究,建议学习一下计算机图形学

7. 怎样用C语言实现数字图像处理的局部直方图均衡算法

1、ctrl+alt+l,自动色阶。 2、手动调色阶,用黑场和白场吸取色样。 3、曲线调色。 4、图像调整,匹配颜色,勾选中和选项。 方法很多的,学会灵活运用哟

8. C语言打开图像文件后读取像素

C语言打开图像文件后运用以下代码就可以读取像素,具体如下:
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改变图像亮度

typedef struct BMP
{
//14字节
unsigned short bfType; //文件标识 2字节 必须为BM
unsigned int bfSize; //文件大小 4字节
unsigned short bfReserved1; //保留,每字节以"00"填写 2字节
unsigned short bfReserved2; //同上 2字节
unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节

//40字节
unsigned int biSize; //表示本结构的大小 4字节
int biWidth; //位图的宽度 4字节
int biHeight; //位图的高度 4字节
unsigned short biPlanes; //永远为1 , 2字节
unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节
unsigned int biCompression; //压缩说明 4字节
unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节
int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节
int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节
unsigned int biClrUsed; //位图使用的颜色索引数 4字节
unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节

} BMP;

int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif

//image_rw.c文件

#include<stdio.h>
#include<stdlib.h>
#include"image.h"

void image_info(FILE *file)
{

int times=3; //输入文件名次数。
char bmp_name[10]; //文件名

printf("\nplease enter a file name for reading:");
do
{
if (times<3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);

if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}

//读取图像信息

fseek(file,0L,0); //读取图像文件类型
fread(&bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);

printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);

line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);

fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);
}

//保存图像
void image_save(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
//int i; //记录数据区个数

printf("\nplease enter a file name for writeing:");
do
{
if (times<3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);

if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}

//写文件头
printf("\n%s",bmp_name);
fseek(file,0L,0); //图像文件类型
fwrite(&(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);

fseek(file,2L,0); //图像文件大小
fwrite(&(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);

fseek(file,6L,0); //图像文件保留字1
fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);

fseek(file,8L,0); //图像文件保留字2
fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);

fseek(file,10L,0);//数据区的偏移量
fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);

fseek(file,14L,0);//文件头结构大小
fwrite(&(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);

fseek(file,18L,0);//图像的宽度
fwrite(&(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);

fseek(file,22L,0);//图像的高度
fwrite(&(bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);

fseek(file,24L,0);//图像的面数
fwrite(&(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);

fseek(file,28L,0);//图像一个像素的字节数
fwrite(&(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);

fseek(file,30L,0);//图像压缩信息
fwrite(&(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);

fseek(file,34L,0);//图像数据区的大小
fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);

fseek(file,38L,0);//水平分辨率
fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

fseek(file,42L,0);//垂直分辨率
fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

fseek(file,46L,0);//颜色索引数
fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);

fseek(file,50L,0);//重要颜色索引数
fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);

fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);
}

//pixProcess.c文件

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"

//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}

//二值化

void image_binarization()
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if ((*(imagedata+i*line_byte+j))<128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}

void image_opposite() //反相
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}

void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",&rgb);
if (rgb=='b')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}

}

void image_bright()//改变图像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",&level);
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if (level>=0)
{

if ((imagedata[i*line_byte+j]+level)>255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))<0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}

}
}
}

//void image_create() //创建一幅24位BMP图像文件。
//{

//main.c文件

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"image.h"

BMP bmp;

int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已经分配了动态内存,但是没有释放

printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");

printf("\nchoose your options:");
fflush(stdin);
scanf("%d",&choose);
switch(choose)
{

case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");

}

printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",&gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);

return 0;
}

9. 请问C语言读取图像转换为二维数组程序有什么问题,怎么修改

FileName=fileDlg.GetFileName ();FILE *fp=fopen(FileName,"rb");//二进制读方式打开指定的图像文件fread(&FileHead, sizeof(BITMAPFILEHEADER), 1,fp); //读取文件头,文件指针自动后移fread(&InfoHead, sizeof(BITMAPINFOHEADER), 1,fp);//读取信息头,文件指针自动后移//获取图像宽、高、每像素所占位数等信息bmpWidth = InfoHead.biWidth;bmpHeight = InfoHead.biHeight;//下面完成图像数据向内存数组的存储ImageData=new unsigned char*[bmpHeight];if(InfoHead.biBitCount==24){for (int i=0;i<bmpHeight;i++){ImageData[i]=new unsigned char[(bmpWidth*3+3)/4*4];}for (int k=0;k<bmpHeight;k++ ){for(int j=0;j<(bmpWidth*3+3)/4*4;j++){fread(&ImageData[k][j],1,1,fp);//上面完成动态二维数组的申请,这里实际读取图像数据}}fclose(fp);//关闭文件

10. 用C语言编写灰度图像直方图变换增强

参考一下这个程序:

#define IMAGEWIDTH 80
#define IMAGEHEIGHT 80

void Histogram(unsigned char *pImage,int nWidth,int nHeight,float fHisto[256]);
void Enhance(unsigned char *pImage,unsigned char *pImage1,int nWidth,int nHeight,float fHisto[256],float fHisto1[256]);

unsigned char dbImage[IMAGEWIDTH*IMAGEHEIGHT];
unsigned char dbTargetImage[IMAGEWIDTH*IMAGEHEIGHT];
float fHistogram[256],fHistogram1[256];
unsigned char lut[256];

/* 直方图统计程序 */
int main()
{
... //准备一幅灰度图片到dbImage
Histogram(dbImage,IMAGEWIDTH,IMAGEHEIGHT,fHistogram); //做直方图统计
Enhance(dbImage,dbTargetImage,IMAGEWIDTH,IMAGEHEIGHT,fHistogram,fHistogram1); //直方图增强输出到dbTargetImage
...//完成后显示

}

void Histogram(unsigned char *pImage,int nWidth,int nHeight,float fHisto[256])
{
int i,j;
unsigned int uWork;
unsigned char *pWork;

for ( i=0;i<256;i++ ) fHisto[i]=0.0f;
pWork=pImage;
for ( i=0;i<nHeight;i++ )
{
for ( j=0;j<nWidth;j++,pWork++ )
{
uWork=(unsigned int)(*pWork);
fHisto[uWork]++;
}
}
uWork=nWidth*nHeight;
for ( i=0;i<256;i++ )
{
fHisto[i]/=uWork;
fHisto[i]*=100;
}
}

void Enhance(unsigned char *pImage,unsigned char *pImage1,int nWidth,int nHeight,float fHisto[256],float fHisto1[256])
{
int i,j;
unsigned int uWork;
unsigned char *pWork,*pWork1;

for ( i=0;i<256;i++ )
fHisto1[i]=fHisto[i]/100;
for ( i=1;i<256;i++ )
fHisto1[i]+=fHisto1[i-1];
for ( i=0;i<256;i++ )
lut[i]=fHisto1[i]*256;
for ( i=0;i<256;i++ )
if ( lut[i]>=256 )
lut[i]=255;
pWork=pImage; pWork1=pImage1;
for ( i=0;i<nHeight;i++ )
for ( j=0;j<nWidth;j++,pWork++,pWork1++ )
(*pWork1)=lut[(*pWork)];
}