代码1 生成BMP位图文件程序
#include \"stdafx.h\" #include \"windows.h\" #include unsigned char pixel[200][200*3]; int main(int argc, char* argv[]) { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; bf.bfType=((WORD)('M'<<8)|'B'); bf.bfSize=200L*200*3+sizeof(bf)+sizeof(bi); bf.bfReserved1=0; bf.bfReserved2=0; bf.bfOffBits=sizeof(bf)+sizeof(bi); bi.biSize=sizeof(bi); bi.biWidth=200; bi.biHeight=200; bi.biPlanes=1; bi.biBitCount=24; bi.biCompression=0; bi.biSizeImage=200L*200; bi.biXPelsPerMeter=0; bi.biYPelsPerMeter=0; bi.biClrUsed=0; bi.biClrImportant=0; FILE *f=fopen(\"w.bmp\ if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i<200;i++) { for(int j=0;j<200;j++) { pixel[i][j*3+0]=0; pixel[i][j*3+1]=0; pixel[i][j*3+2]=255; } } for(i=0;i<200;i++) fwrite(pixel[i],200*3,1,f); fclose(f); return 0; } 代码2 在BMP位图文件中画出各种图像程序 #include \"windows.h\" #include unsigned char pixel[500][500*3]; int main(int argc, char* argv[]) { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; bf.bfType=((WORD)('M'<<8)|'B'); bf.bfSize=500L*500*3+sizeof(bf)+sizeof(bi); bf.bfReserved1=0; bf.bfReserved2=0; bf.bfOffBits=sizeof(bf)+sizeof(bi); bi.biSize=sizeof(bi); bi.biWidth=500; bi.biHeight=500; bi.biPlanes=1; bi.biBitCount=24; bi.biCompression=0; bi.biSizeImage=500L*500; bi.biXPelsPerMeter=0; bi.biYPelsPerMeter=0; bi.biClrUsed=0; bi.biClrImportant=0; FILE *f=fopen(\"ni.bmp\ if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i<500;i++) { for(int j=0;j<500;j++) { pixel[i][j*3+0]=0; pixel[i][j*3+1]=0; pixel[i][j*3+2]=0; } } for(int x=0;x<400;x++) } { pixel[50][x*3+0]=255; pixel[50][x*3+1]=0; pixel[50][x*3+2]=255; } for(int y=50;y<450;y++) { pixel[y][400*3+0]=255; pixel[y][400*3+1]=0; pixel[y][400*3+2]=0; } for(x=0;x<400;x++) { pixel[x+50][x*3+0]=0; pixel[x+50][x*3+1]=255; pixel[x+50][x*3+2]=0; } int r=50; for(int d=1;d<360;d++) { int x=r*cos(d*PI/180.0)+300; int y=r*sin(d*PI/180.0)+200; pixel[y][x*3+0]=0; pixel[y][x*3+1]=0; pixel[y][x*3+2]=255; } for(x=0;x<360;x++) { int y=100*sin(x*PI/180.0)+200; pixel[y][x*3+0]=0; pixel[y][x*3+1]=255; pixel[y][x*3+2]=255; } for(i = 0; i < 500; i++) fwrite(pixel[i],500*3,1,f); fclose(f); return 0; 代码3 对BMP位图文件进行读写程序 #include unsigned char pixel[300][400*3]; int main() { } BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; FILE *fp = NULL; if ((fp = fopen(\"33.bmp\ return 0; fread(&bf,sizeof(bf),1,fp); fread(&bi,sizeof(bi),1,fp); for (int i = 0; i< bi.biHeight; i++) fread(pixel[i],bi.biWidth*3,1,fp); if ((fp = fopen(\"66.bmp\ return 0; fwrite(&bf,sizeof(bf),1,fp); fwrite(&bi,sizeof(bi),1,fp); for ( i = 0; i < bi.biHeight; i++) fwrite(pixel[i],bi.biWidth*3,1,fp); return 0; 代码4 将彩色图像转变为灰度图像程序 #include \"stdafx.h\" #include \"windows.h\" #include unsigned char pixel[800][800*3]; int main(int argc, char* argv[]) { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; FILE *f=fopen(\"12.bmp\ if(f==NULL) return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y yy=(unsigned char)(0.299*r+0.587*g+0.114*b); pixel[y][x*3+2]=yy; pixel[y][x*3+1]=yy; pixel[y][x*3+0]=yy; } f=fopen(\"66.bmp\if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i #include \"windows.h\" #include unsigned char pixel[200][200*3]; float Y[200][200],Cb[200][200],Cr[200][200],X[200][200]; int main() { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; FILE *f=fopen(\"12.bmp\ if(f==NULL) return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y float yy; int i,j,a,c; float rr,rg,rb; for( y=0;y yy=(float)((Y[c][j]+Y[c][x]+Y[c][a]+Y[y][j]+Y[y][x]+Y[y][a]+Y[i][j]+Y[i][x]+Y[i][a])/9.0); X[y][x]=yy; rr=(X[y][x]+1.402*Cr[y][x]); rg=(X[y][x]-0.7141*Cr[y][x]-0.3441*Cb[y][x]); rb=(X[y][x]+1.772*Cb[y][x]); if(rr<0) rr=0; if(rg<0) rg=0; if(rb<0) rb=0; if(rr>255) rr=255; if(rg>255) rg=255; if(rb>255) rb=255; pixel[y][x*3+2]=rr; pixel[y][x*3+1]=rg; } pixel[y][x*3+0]=rb; } } f=fopen(\"66.bmp\if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for( i=0;i #include \"windows.h\" #include unsigned char pixel[400][400*3]; float Y[400][400],Cb[400][400],Cr[400][400],X[400][400]; int main() { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; FILE *f=fopen(\"12.bmp\ if(f==NULL) return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y float yy; int a,c; float rr,rg,rb; for( y=0;y f=fopen(\"66.bmp\if(f==NULL) } return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i #include \"windows.h\" #include unsigned char pixel[N][N*3]; float Y[N][N],Cb[N][N],Cr[N][N],T[N][N],G[N][N],D[N][N],H[N][N],g[N][N]; float value1,value2; float rr,rg,rb; BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; void DCT(void) { for(int m = 0; m { value2=(float)sqrt(2.0 / N); } T[m][n]+=(float)Y[i][j]*(value1*(float)cos(PI*(2*i+1)*(float)m/(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } } } } void IDCT(void) { for(int i= 0; i g[i][j]+=G[m][n]*(value1*(float)cos(PI*(2*i+1)*m/(float)(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } rr=(float)(g[i][j] + 1.402*Cr[i][j]); rg=(float)(g[i][j]-0.7141* Cr[i][j]-0.3441*Cb[i][j]); rb=(float)(g[i][j]+1.772*Cb[i][j]); if(rr<0) { rr=0; } else if (rr > 255) { rr=255; } if (rb<0) { rb=0; } else if (rb > 255) { rb=255; } if (rg < 0) { rg=0; } else if (rg>255) { rg=255; } pixel[i][j*3+2]=(unsigned char)rr; pixel[i][j*3+1]=(unsigned char)rg; pixel[i][j*3+0]=(unsigned char)rb; } } } void filter(void) { for(int m = 0; m < bi.biHeight; m++) for(int n = 0; n < bi.biWidth; n++) { D[m][n]=(float)pow(m*m+n*n,0.5); if(D[m][n]<=50) H[m][n] = 1; if(D[m][n]>50) H[m][n] = 0; G[m][n] =T[m][n]*H[m][n] ; } } int main() { } FILE *f=fopen(\"12.bmp\if(f==NULL) return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y f=fopen(\"66lidig.bmp\if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i #include \"windows.h\" #include unsigned char pixel[N][N*3]; float Y[N][N],Cb[N][N],Cr[N][N],T[N][N],G[N][N],D[N][N],H[N][N],g[N][N]; float value1,value2; float rr,rg,rb; BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; void DCT(void) { for(int m = 0; m T[m][n]+=(float)Y[i][j]*(value1*(float)cos(PI*(2*i+1)*(float)m/(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } } } } void IDCT(void) { for(int i= 0; i g[i][j]+=G[m][n]*(value1*(float)cos(PI*(2*i+1)*m/(float)(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } rr=(float)(g[i][j] + 1.402*Cr[i][j]); rg=(float)(g[i][j]-0.7141* Cr[i][j]-0.3441*Cb[i][j]); rb=(float)(g[i][j]+1.772*Cb[i][j]); if(rr<0) { rr=0; } else if (rr > 255) { rr=255; } if (rb<0) { rb=0; } else if (rb > 255) { rb=255; } if (rg < 0) { rg=0; } else if (rg>255) { rg=255; } pixel[i][j*3+2]=(unsigned char)rr; pixel[i][j*3+1]=(unsigned char)rg; pixel[i][j*3+0]=(unsigned char)rb; } } } void filter(void) { for(int m = 0; m < bi.biHeight; m++) for(int n = 0; n < bi.biWidth; n++) { D[m][n]=(float)pow(m*m+n*n,0.5); H[m][n]=(float)(1/(1+pow((D[m][n]/50),2.0))); G[m][n] =T[m][n]*H[m][n] ; } } int main() { FILE *f=fopen(\"12.bmp\ if(f==NULL) return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y Cr[y][x]=(float)(0.500*r-0.4187*g-0.0813*b); Cb[y][x]=(float)(-0.1687*r-0.3313*g+0.500*b); } } DCT(); filter(); IDCT(); f=fopen(\"66badig.bmp\if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i #include \"windows.h\" #include unsigned char pixel[N][N*3]; float Y[N][N],Cb[N][N],Cr[N][N],T[N][N],G[N][N],D[N][N],H[N][N],g[N][N]; float value1,value2; float rr,rg,rb; BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; void DCT(void) { for(int m = 0; m T[m][n]+=(float)Y[i][j]*(value1*(float)cos(PI*(2*i+1)*(float)m/(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } } } } void IDCT(void) { for(int i= 0; i { value2=(float)pow(2.0 / N, 0.5); } g[i][j]+=G[m][n]*(value1*(float)cos(PI*(2*i+1)*m/(float)(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } rr=(float)(g[i][j] + 1.402*Cr[i][j]); rg=(float)(g[i][j]-0.7141* Cr[i][j]-0.3441*Cb[i][j]); rb=(float)(g[i][j]+1.772*Cb[i][j]); if(rr<0) { rr=0; } else if (rr > 255) { rr=255; } if (rb<0) { rb=0; } else if (rb > 255) { rb=255; } if (rg < 0) { rg=0; } else if (rg>255) { rg=255; } pixel[i][j*3+2]=(unsigned char)rr; pixel[i][j*3+1]=(unsigned char)rg; pixel[i][j*3+0]=(unsigned char)rb; } } } void filter(void) { for(int m = 0; m < bi.biHeight; m++) for(int n = 0; n < bi.biWidth; n++) { D[m][n]=(float)pow(m*m+n*n,0.5); if(D[m][n]<=10) H[m][n] = 0; if(D[m][n]>10) H[m][n] = 1; G[m][n] =T[m][n]*H[m][n] ; } } int main() { FILE *f=fopen(\"12.bmp\ if(f==NULL) return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y f=fopen(\"66ligaod.bmp\ if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i 代码10 频域中巴特沃斯高通滤波程序 #include \"windows.h\" #include unsigned char pixel[N][N*3]; float Y[N][N],Cb[N][N],Cr[N][N],T[N][N],G[N][N],D[N][N],H[N][N],g[N][N]; float value1,value2; float rr,rg,rb; BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; void DCT(void) { for(int m = 0; m T[m][n]+=(float)Y[i][j]*(value1*(float)cos(PI*(2*i+1)*(float)m/(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } } } } void IDCT(void) { for(int i= 0; i g[i][j]+=G[m][n]*(value1*(float)cos(PI*(2*i+1)*m/(float)(2*N)))*(value2*(float)cos(PI*(2*j+1)*(float)n/(2*N))); } } rr=(float)(g[i][j] + 1.402*Cr[i][j]); rg=(float)(g[i][j]-0.7141* Cr[i][j]-0.3441*Cb[i][j]); rb=(float)(g[i][j]+1.772*Cb[i][j]); if(rr<0) { rr=0; } else if (rr > 255) { rr=255; } if (rb<0) { rb=0; } else if (rb > 255) { rb=255; } if (rg < 0) { rg=0; } else if (rg>255) { rg=255; } pixel[i][j*3+2]=(unsigned char)rr; pixel[i][j*3+1]=(unsigned char)rg; pixel[i][j*3+0]=(unsigned char)rb; } } } void filter(void) { for(int m = 0; m < bi.biHeight; m++) for(int n = 0; n < bi.biWidth; n++) { if((m==0)&&(n==0)) { H[m][n]=0.5; } D[m][n]=(float)pow(m*m+n*n,0.5); H[m][n]=(float)(1/(1+pow((10/D[m][n]),2.0))); G[m][n] =T[m][n]*H[m][n] ; } } int main() { FILE *f=fopen(\"12.bmp\ if(f==NULL) } return 0; fread(&bf,sizeof(bf),1,f); fread(&bi,sizeof(bi),1,f); for(int y=0;y for( y=0;y f=fopen(\"66bagaod.bmp\if(f==NULL) return 0; fwrite(&bf,sizeof(bf),1,f); fwrite(&bi,sizeof(bi),1,f); for(int i=0;i 因篇幅问题不能全部显示,请点此查看更多更全内容