1、 用 c 语言进行数字图像处理 其实,数字图像处理有几步呢?一共三步。第一步,读入图片。第二步,处理图片。第三步,保存图片 。 而第二步主要涉及的是处理图像的算法,所以,我在这里就不多说了。而第一步和第三步是为第二步做 位图文件结构的声明:BMP.h #ifndef BMP_H_INCLUDED #define BMP_H_INCLUDED typedef unsigned short WORD; typedef unsigned long DWORD; typedef long LONG; typedef unsigned char BYTE; typedef struct tagBITMA
2、PFILEHEADER / 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
3、 biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; BITMAPINFOHEADER; typedef struct tagRGBQUAD / rgbq BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved; RGBQUAD; typedef struct tagBITMAPINFO BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors1; BITMAPINFO; #endif / BMP_H_INCLUDED 主程序:main.c
4、 #include #include #include #include #include #include #include “BMP.h“ BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; BYTE *imgData; bool bReadBMFH=false; bool bReadBMIH=false; bool bReadPixel=false; /检查路径是否合法:文件能打开;以 bmp 为后缀名 int CheckFilePath(char *filepath); /读入位图的文件头 int ReadFileHeader(char *fil
5、epath,BITMAPFILEHEADER *bmfh); /打印位图的文件头 void PrintFileHeader(BITMAPFILEHEADER *bmfh); /读入位图的信息头 int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih); /打印位图的信息头 void PrintInfoHeader(BITMAPINFOHEADER *bmih); /创建 8 位位图的调色板 int CreatePalette(RGBQUAD pal); /读入位图的像素数据 int ReadPixelData(char *filepat
6、h,BYTE *imgData); /计算每行像素所占的字节数 LONG GetLineBytes(int imgWidth,int bitCount); /打印位图的像素数据 void PrintPixelData(BYTE *imgData,int width,int height,int bitCount); /打印菜单选项 void PrintMenu(); /另存为位图 int SaveAsImage(char *filepath); /显示位图 void ShowImage(char * filepath); /保存文件头 int SaveFileHeader(FILE* fp);
7、 /保存信息头 int SaveInfoHeader(FILE* fp); /保存调色板 int SaveColorPalette(FILE *fp); /保存像素数据 int SavePixelData(FILE* fp); int main() char filepath256; char saveasfilepath256; int i; int width; int height; int bitCount; DWORD dwLineBytes; int select; int q=0; system(“echo off“); system(“color 2“); printf(“-T
8、IMimage-n“); printf(“Input the path of the BMP file:n“); gets(filepath); i=CheckFilePath(filepath); if(i=-1) return -1; do PrintMenu(); scanf(“%u“, switch(select) case 0: printf(“Input the path of the BMP file:n“); scanf(“%s“,filepath); CheckFilePath(filepath); break; case 1: i=ReadFileHeader(filepa
9、th, if(i!=-1) printf(“Read the file header successfully.n“); bReadBMFH=true; break; else printf(“Read the file header failed.n“); bReadBMFH=false; q=1; break; case 2: i=ReadInfoHeader(filepath, if(i!=-1) printf(“Read the info header successfully.n“); bReadBMIH=true; break; else printf(“Read the info
10、 header failed.n“); bReadBMIH=false; q=1; break; case 3: if(!bReadBMIH) printf(“Please read the info header at first.n“); break; height=bmih.biHeight; width=bmih.biWidth; bitCount=bmih.biBitCount; dwLineBytes=GetLineBytes(width,bitCount); imgData=(BYTE*)malloc(dwLineBytes*height*sizeof(BYTE); if(!im
11、gData) printf(“Can not allocate memory for the image.n“); q=1; break; i=ReadPixelData(filepath,imgData); if(i=-1) printf(“Read the pixel data failed.n“); bReadPixel=false; q=1; break; else printf(“Read the pixel data successfully.n“); bReadPixel=true; break; case 4: if(bReadBMFH) PrintFileHeader( br
12、eak; else printf(“Please read the file header at first.n“); break; case 5: if(bReadBMIH) PrintInfoHeader( break; else printf(“Please read the info header at first.n“); break; case 6: if(bReadPixel) PrintPixelData(imgData,width,height,bitCount); break; else printf(“Please read the pixel data at first
13、.n“); break; case 7: ShowImage(filepath); break; case 8: printf(“Input the path(ex. d:/poon.bmp) you want to save:n“); scanf(“%s“,saveasfilepath); i=SaveAsImage(saveasfilepath); if(i=-1) printf(“Error: failed to save the image.n“); break; break; default: q=1; break; select=9527; while (q=0); return
14、0; int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh) FILE *fp; fp=fopen(filepath,“rb“); if(!fp) printf(“Can not open the file:%sn“,filepath); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(frea
15、d( fclose(fp); return -1; fclose(fp); return 0; int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih) FILE *fp; fp=fopen(filepath,“rb“); if(!fp) printf(“Can not open the file:%sn“,filepath); return -1; fseek(fp,14,SEEK_SET); if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fr
16、ead( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; fclose(f
17、p); 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;i256;i+) pali.rgbBlue=i; pali.rgbGreen=i; pali.rgbRed=i; pali.rgbReserved=0; return 0; int ReadPixelData(char *filepath,BYTE *imgData) BITMAPIN
18、FOHEADER bmih; BITMAPFILEHEADER bmfh; BYTE *data; FILE *fp; int n; int width; int height; int bitCount; DWORD dwLineBytes; n=ReadFileHeader(filepath, if(n=-1) printf(“Can not read the file header of the BMP file.n“); return -1; n=ReadInfoHeader(filepath, if(n=-1) printf(“Can not read the info header of the BMP file.n“); return -1; width=bmih.biWidth; height=bmih.biHeight;