Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- C program to implement Optimum Thresholding to binarize an image
- Sandipan Dey
- BCSE, JU, Kolkata
- 2003
- */
- //MUST USE MEDIUM MEMORY MODEL
- //USE 256 COLOR BITMAP FILE (WIDTH%4=0)
- // Input image files: RABBIT.BMP, BIRD.BMP
- #include <math.h>
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <graphics.h>
- #include <string.h>
- #define DEFAULT "output.bmp"
- #define MAX 256
- #define PI 3.14159
- #define VAL 1
- struct Point {
- int x,y;
- };
- struct SE {
- Point p[4];
- };
- double ln(double x) {
- return (x>0?log(x):0);
- }
- void main(int argc,char* argv[]) {
- FILE* fp=NULL,*fo=NULL;
- long int n[MAX]={0};
- double p[MAX]={0};
- int c,type;
- char INPUTFILE[100],OUTPUTFILE[100],s[100];
- long int imgoffset=0;
- double sum=0;
- strcpy(OUTPUTFILE,DEFAULT);
- clrscr();
- if(argc<=1) {
- printf("\nNo Input File!");
- getch();
- exit(0);
- }
- strcpy(INPUTFILE,argv[1]);
- if(argc>2 && strcmp(argv[2],"\0"))
- strcpy(OUTPUTFILE,argv[2]);
- if((fp=fopen(INPUTFILE,"rb"))==NULL) {
- printf("\n File Not Found!");
- fclose(fp);
- getch();
- exit(0);
- } // File Not Found
- fo=fopen(OUTPUTFILE,"wb");
- if(fgetc(fp)!='B' || fgetc(fp)!='M') { // Check Signature
- printf("\n Not a valid BMP File!!!");
- fclose(fp);
- fclose(fo);
- getch();
- exit(0);
- }
- fseek(fp,10,0); // Find Offset of Data
- imgoffset=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24); // Data Offset
- fseek(fp,28,0); // Find No. of Bits to Represent a Pixel of the Bitmap
- type=fgetc(fp);
- switch(type) {
- case 1:printf("\n A Monochrome (1-Bit) Bitmap!");break;
- case 4:printf("\n A 16-Color (4-Bit) Bitmap!");break;
- case 8:printf("\n A 256-Color (8-Bit) Bitmap");break;
- default:printf("\n Not a 1,4 Or 8-Bit Bitmap!");break;
- }
- if(type!=8){
- fclose(fp);
- printf("\nAn 8-bit Bitmap expected!!!");
- fclose(fo);
- exit(0);
- }
- long int w,h;
- fseek(fp,18,0);
- w=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
- h=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
- int threshold=0;
- double minH=1000000.01,q1,q2,m1,m2,v1,v2,H,sumsq=0;
- for(int i=0;i<MAX;++i){n[i]=p[i]=0;}
- int gd=VGA,gm=VGAHI,x=0,y=h;
- struct palettetype pal;
- initgraph(&gd,&gm,"");
- getpalette(&pal);
- for(i=0;i<16;i++)
- setrgbpalette(pal.colors[i],i*4,i*4,i*4);
- fp=fopen(INPUTFILE,"rb");
- fo=fopen(OUTPUTFILE,"wb");
- fseek(fp,0,0);
- while(!feof(fp)) {
- fputc(fgetc(fp),fo);
- }
- fseek(fp,imgoffset,0);
- while(!feof(fp)) {
- c=fgetc(fp);
- putpixel(x,y,c/16);
- x=(x+1)%w;
- if(!x){--y;}
- ++n[c%MAX];
- }
- getch();
- cleardevice();
- for(i=0;i<MAX;++i)sum+=n[i];
- for(i=0;i<MAX;++i)p[i]=n[i]/(sum*1.0);
- threshold=0;
- for(unsigned int t=1;t<MAX-1;++t) {
- q1=sum=0;
- for(i=0;i<t;++i){q1+=p[i];sum+=i*p[i];}
- m1=q1?sum/q1:0;sumsq=0;
- for(i=0;i<t;++i){sumsq+=(i-m1)*(i-m1)*p[i];}
- v1=q1?sumsq/q1:0;
- q2=sum=0;
- for(i=t;i<MAX;++i){q2+=p[i];sum+=i*p[i];}
- m2=q2?sum/q2:0;sumsq=0;
- for(i=t;i<MAX;++i){sumsq+=(i-m2)*(i-m2)*p[i];}
- v2=q2?sumsq/q2:02;
- //printf("%lf %lf %lf %lf\n", q1, q2, v1, v2);
- H=(1+ln(2*PI))/2-q1*ln(q1)-q2*ln(q2)+(q1*ln(v1)+q2*ln(v2))/2;
- if(H<minH){minH=H;threshold=t;}
- }
- int xstart=10,ystart=470;
- line(xstart,ystart,620,ystart);
- line(xstart,ystart-470,xstart,ystart);
- setcolor(14);
- for(i=0;i<MAX;++i) {
- line(xstart+2*i,ystart-5000*p[i],xstart+2*i,ystart);
- if(!(i%15)){
- setcolor(getmaxcolor());
- line(xstart+2*i,ystart-5,xstart+2*i,ystart+1);
- sprintf(s,"%d",i);
- outtextxy(xstart+2*i-7,ystart+2,s);
- setcolor(14);
- }
- }
- setcolor(4);
- outtextxy(xstart+2*threshold+7,ystart-200,"Threshold");
- line(xstart+2*threshold,ystart,xstart+2*threshold,ystart-470);
- setcolor(14);
- fflush(stdin);
- getch();
- cleardevice();
- sprintf(s,"Threshold=%d",threshold);
- outtextxy(520,421,s);
- outtextxy(520,445,"Sandipan Dey");
- outtextxy(520,455,"BCSE-IV");
- outtextxy(520,465,"Roll-99710");
- fseek(fp,imgoffset,0);
- fseek(fo,imgoffset,0);
- x=0;y=h;
- int c1;
- while(!feof(fp)) {
- c=fgetc(fp);
- c1=(c<threshold)?0:15;
- fputc(c1,fo);
- putpixel(x,y,c1);
- x=(x+1)%w;
- if(!x){--y;}
- }
- fclose(fo);
- fclose(fp);
- getch();
- closegraph();
- }
Add Comment
Please, Sign In to add comment