Convert the grayscale image obtained from the previous article to a two value graph, read the pixel data, lower than a certain value of 0, otherwise set to 255, in order to get a better effect different pictures should use different values
1 /*2 June 2, 2015 11:16:223 Grayscale Graph converted to two value graph4 Blog:http://www.cnblogs.com/wd1001/5 */6#include <stdio.h>7#include <malloc.h>8#include <stdlib.h>9 /*Ten Bitmap Header Structure One */ A #pragmaPack (1) -typedefstructTagbitmapfileheader - { theUnsignedCharbftype[2];//file Format -UnsignedLongBfsize;//File Size -Unsigned ShortbfReserved1;//reserved -Unsigned ShortBfReserved2; +UnsignedLongBfoffbits;//the offset of the DIB data in the file - }fileheader; + #pragmaPack () A /* at Bitmap data information structure - */ - #pragmaPack (1) -typedefstructTagbitmapinfoheader - { -UnsignedLongBisize;//the size of the structure in LongBiwidth;//File Width - LongBiheight;//File Height toUnsigned Shortbiplanes;//Number of planes +Unsigned ShortbiBitCount;//number of color bits -UnsignedLongBicompression;//Compression Type theUnsignedLongbiSizeImage;//DIB data area size * LongBixpixpermeter; $ LongBiypixpermeter;Panax NotoginsengUnsignedLongbiclrused;//how many color index tables -UnsignedLongBiclrimporant;//How many important colors? the }fileinfo; + #pragmaPack () A /* the Palette Structure + */ - #pragmaPack (1) $typedefstructTagrgbquad $ { -UnsignedCharRgbblue;//Blue Component Brightness -UnsignedCharRgbgreen;//Green Component Brightness theUnsignedCharrgbred;//Red Component Brightness -UnsignedCharrgbreserved;Wuyi }rgbq; the #pragmaPack () - Wu intMain () - { About inti,j; $UnsignedCharimgdata[ +]; -FILE * fpgray,*Fpbin; -Fileheader *fh; -FileInfo *fi; ARGBQ *Fq; + the if((Fpgray=fopen ("g:/vc6.0/work/22.bmp","RB"))==NULL) - { $printf"failed to open file"); theExit0); the } the the if((Fpbin=fopen ("g:/vc6.0/work/33.bmp","WB"))==NULL) - { inprintf"failed to create file"); theExit0); the } About //reading grayscale graph Data theFh= (Fileheader *)malloc(sizeof(Fileheader)); theFi= (FileInfo *)malloc(sizeof(FileInfo)); thefq= (RGBQ *)malloc( the*sizeof(RGBQ)); +Fread (FH,sizeof(Fileheader),1, Fpgray); -Fread (FI,sizeof(FileInfo),1, Fpgray); theFread (FQ,sizeof(RGBQ), the, Fpgray);Bayi //writes header information to theFwrite (FH,sizeof(Fileheader),1, fpbin); theFwrite (FI,sizeof(FileInfo),1, fpbin); -Fwrite (FQ,sizeof(RGBQ), the, fpbin); - //Gray value below threshold is set to 0 the for(i=0;i< (fi->biheight); i++) the { the for(j=0;(j< (fi->biwidth+3)/4*4); J + +) the { -Fread (&imgdata[j],1,1, Fpgray); the if(imgdata[j]>142) theimgdata[j]=255; the Else94imgdata[j]=0; the } theFwrite (Imgdata,1, j,fpbin); the }98 Free(FH); About Free(FI); - Free(FQ);101 fclose (fpbin);102 fclose (fpgray);103printf"success\n");104 return 0; the}
Results:
Test four tomorrow, and write the rest after the exams.
C-language implementation of gray-scale image conversion to two-value graph