图像增强直方图均衡化处理C语言实现.doc_第1页
图像增强直方图均衡化处理C语言实现.doc_第2页
图像增强直方图均衡化处理C语言实现.doc_第3页
图像增强直方图均衡化处理C语言实现.doc_第4页
图像增强直方图均衡化处理C语言实现.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

图像增强 直方图均衡化处理C语言实现#ifndef BMP_H_3_INCLUDED#define BMP_H_3_INCLUDED typedef unsigned short WORD;typedef unsigned long DWORD;typedef long LONG;typedef unsigned char BYTE;typedef struct tagBITMAPFILEHEADER/bmfh WORD bfType;DWORD bfSize;WORD bfReserved1;WORD bfReserved2;DWORD bfOffBits;BITMAPFILEHEADER;typedef struct tagBITMAPINFOHEADER/bmih DWORD biSize;LONG biWidth;LONG biHeight;WORD biPlanes;WORD biBitCount;DWORD biCompression;DWORD biSizeImage;LONG biXPelsPerMeter;LONG biYPelsPerMeter;DWORD biClrUsed;DWORD biClrImportant;BITMAPINFOHEADER;typedef struct tagRGBQUAD/rgbq BYTE rgbBlue;BYTE rgbGreen;BYTE rgbRed;BYTE rgbReserved;RGBQUAD;typedef struct tagBITMAPINFOBITMAPINFOHEADER bmiHeader;RGBQUAD bmiColors1;BITMAPINFO;#endif/BMP_H_3_INCLUDED#include stdio.h#include stdlib.h#include string.h#include malloc.h#include ctype.h#include process.h#includeBMP_3.hBITMAPFILEHEADER bmfh;BITMAPINFOHEADER bmih;BYTE*imgData;int N;void readData();void HistTrans();int ReadFileHeader(BITMAPFILEHEADER*);int ReadInfoHeader(BITMAPINFOHEADER*);int CreatePalette(RGBQUAD);int ReadPixelData(BYTE*);LONG GetLineBytes(int,int);int SaveAsImage(char*);int main()char saveasfilepath256;int i;DWORD dwLineBytes;readData();dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);N=(int)dwLineBytes*bmih.biHeight;HistTrans();printf(Save as another path(ex.d:/poon.bmp)n);scanf(%s,saveasfilepath);i=SaveAsImage(saveasfilepath);if(i=-1)printf(Error:failed to save the image.n);return 0;void readData()int i,k,h;DWORD dwLineBytes;i=ReadFileHeader(&bmfh);if(i=0)printf(Read file header successful!n);k=ReadInfoHeader(&bmih);if(k=0)printf(Read info header successful!n);dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);imgData=(BYTE*)malloc(dwLineBytes*bmih.biHeight*sizeof(BYTE);h=ReadPixelData(imgData);if(h=0)printf(Read pixel data successful!n);if(i=0&k=0&h=0)printf(Read datas successful!n);void HistTrans()int i,j,k;int count256;double SHisPixels256;int*Pixels;FILE*fp;Pixels=(int*)malloc(256*N*sizeof(int);for(i=0;i 256;i+)counti=0;SHisPixelsi=0.0;for(i=0;i 256*N;i+)Pixelsi=0;for(i=0;i N;i+)countimgDatai+;for(i=0;i 256;i+)k=0;for(j=0;j N;j+)if(imgDataj=i)Pixelsi*256+k=j;k+;SHisPixels0=(double)count0/N);for(i=1;i 256;i+)SHisPixelsi=SHisPixelsi-1+(double)counti/N);for(i=0;i 256;i+)for(j=0;j counti;j+)imgDataPixelsi*256+j=int(SHisPixelsi*255+0.5);int ReadFileHeader(BITMAPFILEHEADER*bmfh)FILE*dataFile;dataFile=fopen(shuaige.bmp,r);if(fread(&bmfh-bfType,sizeof(WORD),1,dataFile)!=1)printf(Can not read bfType in the file header.n);fclose(dataFile);return-1;if(fread(&bmfh-bfSize,sizeof(DWORD),1,dataFile)!=1)printf(Can not read bfSize in the file header.n);fclose(dataFile);return-1;if(fread(&bmfh-bfReserved1,sizeof(WORD),1,dataFile)!=1)printf(Can not read bfReserved1 in the file header.n);fclose(dataFile);return-1;if(fread(&bmfh-bfReserved2,sizeof(WORD),1,dataFile)!=1)printf(Can not read bfReserved2 in the file header.n);fclose(dataFile);return-1;if(fread(&bmfh-bfOffBits,sizeof(DWORD),1,dataFile)!=1)printf(Can not read bfOffBits in the file header.n);fclose(dataFile);return-1;fclose(dataFile);return 0;int ReadInfoHeader(BITMAPINFOHEADER*bmih)FILE*dataFile;dataFile=fopen(shuaige.bmp,r);fseek(dataFile,14,SEEK_SET);if(fread(&bmih-biSize,sizeof(DWORD),1,dataFile)!=1)printf(Can not read biSize in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biWidth,sizeof(LONG),1,dataFile)!=1)printf(Can not read biWidth in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biHeight,sizeof(LONG),1,dataFile)!=1)printf(Can not read biHeight in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biPlanes,sizeof(WORD),1,dataFile)!=1)printf(Can not read biPlanes in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biBitCount,sizeof(WORD),1,dataFile)!=1)printf(Can not read biBitCount in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biCompression,sizeof(DWORD),1,dataFile)!=1)printf(Can not read biCompression in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biSizeImage,sizeof(DWORD),1,dataFile)!=1)printf(Can not read biSizeImage in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biXPelsPerMeter,sizeof(LONG),1,dataFile)!=1)printf(Can not read biXPelsPerMeter in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biYPelsPerMeter,sizeof(LONG),1,dataFile)!=1)printf(Can not read biYPelsPerMeter in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biClrUsed,sizeof(DWORD),1,dataFile)!=1)printf(Can not read biClrUsed in the info header.n);fclose(dataFile);return-1;if(fread(&bmih-biClrImportant,sizeof(DWORD),1,dataFile)!=1)printf(Can not read biClrImportant in the info header.n);fclose(dataFile);return-1;fclose(dataFile);return 0;int CreatePalette(RGBQUAD pal)int i;if(sizeof(pal)/sizeof(RGBQUAD)!=256)printf(The size of the palette must be 256.n);return-1;for(i=0;i 256;i+)pali.rgbBlue=i;pali.rgbGreen=i;pali.rgbRed=i;pali.rgbReserved=0;return 0;int ReadPixelData(BYTE*imgData)BYTE*data;FILE*dataFile;DWORD dwLineBytes;dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);data=(BYTE*)malloc(dwLineBytes*bmih.biHeight*sizeof(BYTE);dataFile=fopen(shuaige.bmp,rb);fseek(dataFile,bmfh.bfOffBits,SEEK_SET);if(fread(data,dwLineBytes*bmih.biHeight*sizeof(BYTE),1,dataFile)!=1)printf(Can not read the pixel data.n);free(data);fclose(dataFile);return-1;memcpy(imgData,data,dwLineBytes*bmih.biHeight*sizeof(BYTE);free(data);fclose(dataFile);return 0;LONG GetLineBytes(int imgWidth,int bitCount)return(imgWidth*bitCount+31)/32*4;int SaveAsImage(char*filepath)FILE*dataFile;RGBQUAD pal256;int i;DWORD dwLineBytes;dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);dataFile=fopen(filepath,wb);if(!dataFile)printf(Error:Can not open the file:%sn,filepath);return-1;for(i=0;i 256;i+)pali.rgbReserved=0;pali.rgbBlue=i;pali.rgbGreen=i;pali.rgbRed=i;if(fwrite(&bmfh.bfType,sizeof(WORD),1,dataFile)!=1)printf(Can not write bfType in the file header.n);fclose(dataFile);return-1;if(fwrite(&bmfh.bfSize,sizeof(DWORD),1,dataFile)!=1)printf(Can not write bfSize in the file header.n);fclose(dataFile);return-1;if(fwrite(&bmfh.bfReserved1,sizeof(WORD),1,dataFile)!=1)printf(Can not write bfReserved1 in the file header.n);fclose(dataFile);return-1;if(fwrite(&bmfh.bfReserved2,sizeof(WORD),1,dataFile)!=1)printf(Can not write bfReserved2 in the file header.n);fclose(dataFile);return-1;if(fwrite(&bmfh.bfOffBits,sizeof(DWORD),1,dataFile)!=1)printf(Can not write bfOffBits in the file header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biSize,sizeof(DWORD),1,dataFile)!=1)printf(Can not write biSize in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biWidth,sizeof(LONG),1,dataFile)!=1)printf(Can not write biWidth in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biHeight,sizeof(LONG),1,dataFile)!=1)printf(Can not write biHeight in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biPlanes,sizeof(WORD),1,dataFile)!=1)printf(Can not write biPlanes in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biBitCount,sizeof(WORD),1,dataFile)!=1)printf(Can not write biBitCount in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biCompression,sizeof(DWORD),1,dataFile)!=1)printf(Can not write biCompression in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biSizeImage,sizeof(DWORD),1,dataFile)!=1)printf(Can not write biSizeImage in the info header.n);fclose(dataFile);return-1;if(fwrite(&bmih.biXPelsPerMeter,sizeof(LONG),1,dataFile)!=1)printf(Can not write biXPelsPerMeter in the i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论