code

Here we can have sample code using mutual information concept.

/*

Compare.c

Compares the similarity of two images using information theory and

produces joint histogram and gives measure of likelyhood .

Input:

two color image in ASCII format quantized using 8 bit in .pnm file.

Output:

the grey scale output image file

joint histogram file joint_histogram.int

Software needed:

GIMP

ImageMagick

created 2009

by a.k.nandan

*/

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#define max 256

FILE *fpt3,*fpt4,*fpt7;

int *f_hist,*r_hist,**image,**f_image,**f1_image,**r_image;

double *f_prob,*r_prob,**h_fr,*p_f,*p_r,*sum,deviation;

double **p_fr;

float I,H;

void operation()

{

int i,j,k,a,a1,a2,a3,m,n,b,f,r,SUM,row,col,y;//

float d,x,z;

char ch1[10],ch2[10];

printf("entr 1st rgb image file name\n");

scanf("%s",&ch1);

printf("entr 2st rgb image file name\n");

scanf("%s",&ch2);

fpt3=fopen(ch1,"r");

fpt4=fopen(ch2,"r");

for(i=0;i<34;i++)

{

fscanf(fpt3,"%c\n",&a); // reading P2#CREATOR:GIMPPNMFilterVersion1.1

fscanf(fpt4,"%c\n",&a1);

printf("%c",a);

}

fscanf(fpt3,"%d\n",&k);//reading row

fscanf(fpt4,"%d\n",&k1);

col=k;

printf("\n rows :%d",k);

fscanf(fpt3,"%d\n",&k); //reading col

fscanf(fpt4,"%d\n",&k1);

row=k;

printf("\n cols :%d",k);

fscanf(fpt3,"%d\n",&k); // reading 255

fscanf(fpt4,"%d\n",&k1);

SUM=row*col;

f_hist=(int*)calloc(max,sizeof(int));

r_hist=(int*)calloc(max,sizeof(int));

f_prob=(double*)calloc(max,sizeof(double));

r_prob=(double*)calloc(max,sizeof(double));

p_f=(double*)calloc(max,sizeof(double));

p_r=(double*)calloc(max,sizeof(double));

f_image=(int**)calloc(col,sizeof(int));

for(i=0;i<col;i++)

f_image[i]=(int *)calloc(row,sizeof(int));

r_image=(int**)calloc(col,sizeof(int));

for(i=0;i<col;i++)

r_image[i]=(int *)calloc(row,sizeof(int));

h_fr=(double **)calloc(max,sizeof(double));

for(i=0;i<max;i++)

h_fr[i]=(double *)calloc(max,sizeof(double));

p_fr=( double **)calloc(max,sizeof(double));

for(i=0;i<max;i++)

p_fr[i]=( double *)calloc(max,sizeof( double));

for(i=0;i<col;i++)

for(j=0;j<row;j++)

{

fscanf(fpt3,"%d\n",&a1);

fscanf(fpt3,"%d\n",&a2);

fscanf(fpt3,"%d\n",&a3);

r_image[i][j]=(a1+a2+a3)/3;

r_hist[a]++;

}

for(i=0;i<col;i++)

for(j=0;j<row;j++)

{

fscanf(fpt4,"%d\n",&a1);

fscanf(fpt4,"%d\n",&a2);

fscanf(fpt4,"%d\n",&a3);

f_image[i][j]=(a1+a2+a3)/3;

f_hist[a]++;

}

/* calculate the joint histogram*/

for(i=0;i<col;i++)

for(j=0;j<row;j++)

{

f=f_image[i][j];

r=r_image[i][j];

h_fr[f][r]++;

//printf("%d %d...",f,r);

}

fpt7=fopen("joint_histogram.int","w");

/* calculate the joint probability*/

for(i=0;i<256;i++)

for(j=0;j<256;j++)

{

p_fr[i][j]=h_fr[i][j]/(double)SUM;

x=h_fr[i][j];

z=0.0;

if(x>1.0)

z=255;

y=z;

fprintf(fpt7,"%d\n",y);

}

fclose(fpt7);

/*calculate the marginial probability of test & reference image*/

for(j=0;j<256;j++)

for(i=0;i<256;i++)

{

p_f[i]=p_f[i]+p_fr[i][j];

p_r[j]=p_r[j]+p_fr[i][j];

}

/*calculate the mutual information*/

for(i=0;i<256;i++)

for(j=0;j<256;j++)

{

if(!((p_r[i]==0) && (p_f[j]==0)))

if(!(p_fr[j][i]==0))

{

I+=p_fr[j][i]*log((p_fr[j][i])/(p_r[i]*p_f[j]));

H+=(-p_fr[j][i]*log(p_fr[j][i]));

}

}

printf("\nI=%f at %d H=%f at %d",I,angle,H,angle);

deviation=(I-H)/(I+H)*100;

if(deviation>0)

printf("\ndeviation= %f percentage",deviation);

else

if(deviation<0)

printf("\ndeviation= %f percentage\n",deviation*(-1));

else

printf("\ndeviation= 0 percentage");

I=0;

H=0;

fclose(fpt3);

fclose(fpt4);

}

int main()

{

int i=0,j=0,angle=0,s;

float k;

printf("\nsrarting........");

operation();

printf("\nView joint histogram file.......joint_histogram.int\n");

return 0;

}