/* Image Retrieval Engine By WENXIA ZHENG
retrive.c build the retrieval engine. For each query image,first apply the Histogram() function to obtain the color histogram of this query image,then for each image in the database ,find the similarity value between the database image and query image.finally,output the five database images that correspond to the top five similarity values.
*/
#include"/opt/local/include/tiffio.h"
#include
#include
#include
#include
#include
#include
#include
#include
#define min(x,y) (x)<(y)? (x):(y)
#define abs(x) (x)< 0 ? (-x):(x)
void list_queryfile(char *filename ); /*list query images file name */
void Histogram(int n , int m, int l, TIFF *tif);/*calculate the histogram and save as temp.dat */
void PutHisgmTofile(float *H,float *V,float *C, int n, int m, int l );
double Cal_simility(float *QHVC, float *HVC,double a,double b,double r);/*Return the similarity value */
int index_Max(double list[],int size); /*Return the position of max value in an arrey */
main()
{
FILE *file,*Qfile,*index_hfile,*img_hisgrmfile,*fileindex;
char w;
char queryname[40], hfname[50];
char pathname1[41],pathname2[41],fullname[80];
char hfilename[200],imagename[200];
char command[100];
double simility[200];
char select,flag;
int len,n,m,l;
int i,j ,k;
float a,b ,r;
float *qhisgram,*QueryH,*QueryV,*QueryC;
float query_histogram[300],image_histogram[300];
int index=0;
int top[5];/* save the index of five top simility value*/
n=100;
m=50;
l=50;
TIFF* tif ;
system("ls /u0/users/A/zzhang/cs580/imagedb/query1/*.tif > Query1.txt");/*Query1.txt save all the Query1 file name wirh path*/
system("ls /u0/users/A/zzhang/cs580/imagedb/query2/*.tif > Query2.txt");/*Query2.txt save all the Query2 file name with path*/
system("ls /u0/users/A/zzhang/cs580/imagedb/db/*.tif > fileindex.txt");/*fileindex.txt save all the image database file name with path */
system("ls *.tif.txt > hfileindex.txt" );/* hfileindex.txt save all the histogram file name in my location*/
pathname1="/u0/users/A/zzhang/cs580/imagedb/query1/"; /*query1 images file path */
pathname2="/u0/users/A/zzhang/cs580/imagedb/query2/";/* query1 images file path */
printf("Please select Query 1 or 2: "); /* user imterface*/
scanf("%c",&select);
switch(select)
{
case '1' : /*select query2 */
list_queryfile("Query1.txt");
printf("please input query image:");
scanf("%s",queryname);
strcpy(fullname,pathname1);
strcat(fullname, queryname);
break;
case '2' : /*select query2*/
list_queryfile("Query2.txt");
printf("please input query image:");
scanf("%s",queryname);
strcpy(fullname,pathname2);
strcat(fullname, queryname);
break;
dafault :
printf("you should press 1 or 2");
exit(0);
}
sprintf(command,"xv %s & \n",fullname);/*display query image*/
system(command);
printf("please select a, b,r (0~1)\n"); /* get coefficient value from keyboard*/
printf("alpha=? ");
scanf("%f", &a);
printf("bata=? ");
scanf("%f", &b);
printf("gama=?");
scanf("%f",&r);
tif=TIFFOpen( fullname, "r"); /* Open query image file*/
Histogram(n ,m , l , tif); /* Calculate the histogram of query image*/
TIFFClose(tif);
/*Read data from Query histogram temp file to form array*/
Qfile=fopen("temp.txt","r");
i=0;
while( fscanf(Qfile,"%f",&query_histogram[i] )!=EOF)
{
i++;
}
fclose(Qfile);
/* open file that indexing file name of histogram*/
index_hfile=fopen("hfileindex.txt","r");
while(fscanf(index_hfile,"%s", hfilename )!=EOF) /* Get a histogram file name a time*/
{
img_hisgrmfile=fopen(hfilename,"r"); /* open histogram file */
i=0; /*Read the histogram data from the histogram file to an array */
while( fscanf(img_hisgrmfile,"%f",&image_histogram[i] )!=EOF)
{ i++; }
fclose(img_hisgrmfile);
/*function Cal_simility(), input query histogram array ,image histogram and coefficient ,
output the similarity value between the query image and database image*/
simility[index]=Cal_simility(query_histogram, image_histogram, a,b, r);
index++ ;
}
fclose(index_hfile);
/* get index of five top simility value in simility[] */
for(i=0;i<5;i++)
{
top[i]=index_Max(simility , 160);
k=top[i];
simility[k]=-1.00;
}
/* display retrieval images*/
for(i=0;i<5;i++)
{
fileindex=fopen("fileindex.txt","r");
index=0;
while(fscanf(index_hfile,"%s", imagename )!=EOF)
{
k=top[i];
if (index==k)
{
printf("top %d image:%s\n",i+1,imagename);
sprintf(command,"xv %s & \n",imagename);
system(command);
sleep(1);
}
index++;
}
fclose(fileindex);
}
}
/*============================================================================
Display file content (Query1.txt or Query2.txt)___list the query file name
without path
==============================================================================*/
void list_queryfile(char *filename )
{ FILE *file;
char *queryname,*name;
int len;
queryname=new char[80];
name=new char[20];
file=fopen(filename,"r");
while(fscanf(file, "%s", queryname)!=EOF)
{
len=strlen(queryname );
do
{
len--;
}while(queryname[len]!='/');
strcpy(name, &queryname[len+1]);
printf("%s\n",name);
}
fclose(file);
}
/*=======================================================================
Calculate similarity value from two image histogram
=========================================================================*/
double Cal_simility(float QHVC[], float HVC[],double a,double b,double r)
{
double disQh,disQv,disQc,disH,disV,disC;
double dotprodH,dotprodV,dotprodC;
double simility;
simility=0.0;
disQh=0.0;
disQv=0.0;
disQc=0.0;
disH=0.0;
disV=0.0;
disC=0.0;
dotprodH=0.0;
dotprodV=0.0;
dotprodC=0.0;
for (int i=0;i<100;i++) /*H*/
{
dotprodH=dotprodH+ (double)QHVC[i]*HVC[i];
disQh=disQh+ pow((double)QHVC[i],2.0);
disH=disH+pow((double)HVC[i],2.0);
}
disQh=sqrt(disQh);
disH=sqrt(disH);
simility=simility+ a* (dotprodH/(disQh*disH));
for (int i=100;i<150;i++)
{
dotprodV=dotprodV+(double) QHVC[i]*HVC[i];
disQv=disQv+ pow((double)QHVC[i],2.0);
disV=disV+pow((double)HVC[i],2.0);
}
disQv=sqrt(disQv);
disV=sqrt(disV);
simility=simility+ b * (dotprodV/(disQv*disV) );
for (int i=150;i<200;i++)
{
dotprodC=dotprodC+ (double)QHVC[i]*HVC[i];
disQc=disQc+ pow((double)QHVC[i],2.0);
disC=disC+pow((double)HVC[i],2.0);
}
disQc=sqrt(disQc);
disC=sqrt(disC);
simility=simility+r * ( dotprodC/(disQc*disC) );
return (simility);
}
/*=====================================================================
Histogram() calculate image histogram and PutHisgmTofile() form
temp.txt file.
=======================================================================*/
/*The function put the histogram data to a file */
void PutHisgmTofile(float *H,float *V,float *C, int n, int m, int l )
{
FILE *temp;
int j;
temp=fopen("temp.txt","w");
for ( j=0;jg)
{
h= -abs( acos(0.5*((r-g)+(r-b))/sqrt( pow((r-g),2.0)+(r-b)*(g-b)) ) -stndH );
}
else
{
h= abs( acos(0.5*((r-g)+(r-b))/sqrt ( pow((r-g),2.0)+(r-b)*(g-b)) ) -stndH );
}
}
for (j=0;j<(n-1);j++) /*Decide the h,v,c value belong to which bucket ,then aggregtate
the pixels number on that bucket*/
{
if( ( -M_PI+j*buckstepH) <= h&& h< (-M_PI+ (j+1)*buckstepH ) )
pixnumberH[j]++ ;
}
if( (-M_PI+(n-1)*buckstepH) <= h&& h<= (-M_PI+ (n)*buckstepH ) )
pixnumberH[(n-1)]++ ;
for ( j=0;j<(m-1);j++)
{
if(j*buckstepV <= v&& v< (j+1)*buckstepV )
pixnumberV[j]++ ;
}
if((m-1)*buckstepV <= v&& v<= (m)*buckstepV )
pixnumberV[(m-1)]++ ;
for ( j=0;j<(l-1);j++)
{
if(j*buckstepC <= c&& c< (j+1)*buckstepC )
pixnumberC[j]++ ;
}
if((l-1)*buckstepC <= c&& c<=(l)*buckstepC )
pixnumberC[(l-1)]++ ;
}
/*Normalizate the histogram represented by total pixel number of every bucket
to probility of every gray level */
for (j=0;j (to report bad content: archivehelp @ gmail)