Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save agaikwad123/cc08f952b14abc84777a9b5288958ebb to your computer and use it in GitHub Desktop.
Save agaikwad123/cc08f952b14abc84777a9b5288958ebb to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<iostream.h>
#include<stdlib.h>
#include<graphics.h>
void plot_axis(int type);
void plot_histo(void);
void plot_profile(void);
unsigned char input[128][128];
void main()
{
int i,j,k;
char name[20];
FILE *fp;
clrscr();
cout<<"\n program to plot histogram of a given images:";
cout<<"\n Enter the name of raw file:";
cin>>name;
//p//c:\rice.tif";
fp=fopen(name,"rb");//c:\\rice.tif","rb");
if(fp==NULL)
{
printf("\n unabel to open the file");
fclose(fp);
getch();
exit(1);
}
i=0;
for(i=0;i<128;i++)
{
for(j=0;j<128;j++)
{
fscanf(fp,"%c",&input[i][j]);
}
}
plot_axis(0);
plot_histo();
getch();
// next:
closegraph();
plot_axis(1);
plot_profile();
getch();
//goto next;
}
void plot_axis(int type)
{
int i,k;
float j;
int gdriver=DETECT,gmode,errorcode;
float x,y,real,imaginary,denominator,w;
initgraph(&gdriver, &gmode, "E:\\TC\\BGI ");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("graphics error: %s\n",grapherrormsg(errorcode));
printf("press any key to halt:");
getch();
exit(1);
}
j=150;
setcolor(7);
line(50,70,50,400);
line(50,400,600,400);
for(i=1;i<6;i++);
{
line(j,395,j,405);
j+=100;
}
if(type==0)
{
outtextxy(150,405,"50");
outtextxy(250,405,"100");
outtextxy(350,405,"150");
outtextxy(450,405,"200");
outtextxy(550,405,"250");
outtextxy(400,10,"HISTOGRAM PLOT");
outtextxy(400,20,"==================");
outtextxy(450,425,"Gray level");
outtextxy(5,100,"freq");
outtextxy(20,370,"150");
}
j=400;
for(i=0;i<11;i++)
{
line(50,j,55,j);
j-=30;
}
if(type==1)
{
outtextxy(150,405,"25");
outtextxy(250,405,"50");
outtextxy(350,405,"75");
outtextxy(450,405,"100");
outtextxy(550,405,"125");
outtextxy(5,50,"Gray level");
outtextxy(20,370,"40");
outtextxy(400,10,"PROFILE PLOT");
outtextxy(400,20,"==================");
outtextxy(450,425,"Pixel number");
}
}
void plot_histo(void)
{
int counter[256];
int i,j;
float x,y,mean,temp,sd,var;
for(i=0;i<256;i++)
counter[i]=0;
mean=0;
sd=0;
for(i=0;i<128;i++)
{
for(j=0;j<128;j++)
{
counter[int(input[i][j])]++;
mean+=int(input[i][j]);
}
}
setcolor(15);
for(i=0;i<256;i++)
{
temp=counter[i];
y=400-(30*temp/150);
x=50+(i*2);
line(x,400,x,y);
}
mean=mean/16384;
for(i=0;i<128;i++)
{
for(j=0;j<128;j++)
{
temp=int(input[i][j])-mean;
sd+=temp*temp;
}
}
sd=sd/16384;
sd=sqrt(sd);
var=sd*sd;
cout<<"\n Mean"<<mean;
cout<<"\n S.D."<<sd;
cout<<"\n VARIANCE"<<var;
}
//Function to plot profile of a given row
void plot_profile(void)
{
int i,x,y,row;
cout<<"\n enter the row number (200 to exit):";
cin>>row;
if(row!=200)
{
for(i=0;i<128;i++)
{
y=int(input[row][i]);
y=400-(y*30/40);
x=50+(i*4);
setcolor(15);
line(x,400,x,y);
}//for
}//if
if(row==200)
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment