PageRenderTime 192ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Contract_meeting/8_ObjectTrackingSURF/sift.cpp

https://github.com/yianni/cappocacciaactivevision
C++ | 163 lines | 91 code | 9 blank | 63 comment | 9 complexity | 39681a7ed56e8e393b6f5184192ceb11 MD5 | raw file
  1. // sift.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <cxcore.h>
  5. #include <cv.h>
  6. #include <highgui.h>
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. /*IplImage *image = cvLoadImage("blue.jpg", CV_LOAD_IMAGE_COLOR);
  10. if(image)
  11. {
  12. cvNamedWindow("Image");
  13. cvShowImage("image",image);
  14. cvSaveImage("save.jpg",image);
  15. cvWaitKey();
  16. cvDestroyWindow("Image");
  17. int nl= image->height; // number of lines
  18. int nc= image->width * image->nChannels; // total number of element per line
  19. int step= image->widthStep; // effective width
  20. // get the pointer to the image buffer
  21. unsigned char *data= reinterpret_cast<unsigned char *>(image->imageData);
  22. int ct=0;double xmean=0.0;double ymean=0.0;
  23. for (int i=1; i<nl; i++) {
  24. for (int j=0; j<nc; j+= image->nChannels) {
  25. // process each pixel ---------------------
  26. //data[j]= data[j]/div * div + div/2;
  27. //data[j+1]= data[j+1]/div * div + div/2;
  28. //data[j+2]= data[j+2]/div * div + div/2;
  29. if((int)data[j]>1.2*(int)data[j+1]&&(int)data[j]>1.2*(int)data[j+2])
  30. {
  31. printf("%d %d %d \n",(int)data[j],(int)data[j+1],(int)data[j+2]);
  32. //getchar();
  33. ct++;
  34. xmean += (double)j/3.0;
  35. ymean += (double)i;
  36. data[j]=data[j+1]=data[j+2]=0;
  37. }
  38. // end of pixel processing ----------------
  39. } // end of line
  40. data+= step; // next line
  41. }
  42. if (ct>0) {
  43. xmean /= (double)ct;
  44. ymean /= (double)ct;
  45. }
  46. printf("current value of ct %d \n",ct);
  47. if (ct>(image->width/20)*(image->height/20))
  48. {
  49. printf("Best guess at blue target: %f %f \n", xmean, ymean); //%g %g\n",xMean, yMean);
  50. }
  51. cvSaveImage("save.pgm",image);
  52. getchar();
  53. cvReleaseImage(&image);
  54. }*/
  55. system("siftWin32.exe <blue1.pgm >tmp1.txt");
  56. IplImage *image1 = cvLoadImage("blue1.pgm");
  57. FILE* fp1=fopen("tmp1.txt","rb");
  58. float *arr1x; float *arr1y; int *arr2;float scale,ori;int temp1, temp2;
  59. //while(fp1!=NULL)
  60. {
  61. fscanf(fp1,"%d %d \n",&temp1, &temp2);
  62. printf("%d %d \n",temp1,temp2);
  63. //getchar();
  64. arr1x=(float*)malloc(sizeof(float)*temp1);
  65. arr1y=(float*)malloc(sizeof(float)*temp1);
  66. arr2=(int*)malloc(sizeof(int)*temp2*temp1);
  67. for(int i2=1;i2<=temp1;i2++)
  68. {
  69. printf("temp1 %d \n",i2);
  70. fscanf(fp1,"%f %f %f %f \n", &arr1x[i2-1], &arr1y[i2-1], &scale,&ori);
  71. printf("%f %f %f %f \n", arr1x[i2-1], arr1y[i2-1], scale,ori);
  72. //getchar();
  73. for(int i3=1;i3<=temp2;i3++)
  74. {
  75. fscanf(fp1,"%d ", &arr2[(i2-1)*temp2+(i3-1)]);
  76. printf("%d ", arr2[(i2-1)*temp2+(i3-1)]);
  77. }fscanf(fp1,"\n");printf("\n");
  78. cvCircle(image1,cvPoint((int)arr1x[i2-1],(int)arr1y[i2-1]),3,cvScalar(0,255,255,255),1);
  79. //getchar();
  80. }
  81. }
  82. cvSaveImage("save_first.jpg",image1);
  83. cvReleaseImage(&image1);
  84. printf("first one finished \n");
  85. fclose(fp1);
  86. system("siftWin32.exe <blue2.pgm >tmp2.txt");
  87. FILE* fp1new=fopen("tmp2.txt","rb");
  88. float *arr1xnew; float *arr1ynew; int *arr2new;float scalenew,orinew;int temp1new, temp2new;
  89. //while(fp1new!=NULL)
  90. {
  91. fscanf(fp1new,"%d %d \n",&temp1new, &temp2new);
  92. printf("%d %d \n",temp1new,temp2new);
  93. //getchar();
  94. arr1xnew=(float*)malloc(sizeof(float)*temp1new);
  95. arr1ynew=(float*)malloc(sizeof(float)*temp1new);
  96. arr2new=(int*)malloc(sizeof(int)*temp2new*temp1new);
  97. for(int i2new=1;i2new<=temp1new;i2new++)
  98. {
  99. fscanf(fp1new,"%f %f %f %f \n", &arr1xnew[i2new-1], &arr1ynew[i2new-1], &scalenew,&orinew);
  100. printf("%f %f %f %f \n", arr1xnew[i2new-1], arr1ynew[i2new-1], scalenew,orinew);
  101. //getchar();
  102. for(int i3new=1;i3new<=temp2new;i3new++)
  103. {
  104. fscanf(fp1new,"%d ", &arr2new[(i2new-1)*temp2new+(i3new-1)]);
  105. printf("%d ", arr2new[(i2new-1)*temp2new+(i3new-1)]);
  106. }fscanf(fp1new,"\n");printf("\n");
  107. //getchar();
  108. }
  109. }
  110. //matching
  111. IplImage *imagematch = cvLoadImage("blue2.pgm");
  112. float *bestmatchx = (float*)malloc(sizeof(float)*temp1);
  113. float *bestmatchy = (float*)malloc(sizeof(float)*temp1);
  114. for (int i4=1;i4<=temp1;i4++)
  115. {
  116. float bestval = 1000000.0;
  117. for(int i5=1;i5<=temp1new;i5++)
  118. {
  119. double value=0.0;
  120. for(int i6=1;i6<=temp2new;i6++)
  121. {
  122. double temp = arr2[(i4-1)*temp2+(i6-1)]-arr2new[(i5-1)*temp2new+(i6-1)];
  123. value+= temp*temp;
  124. }
  125. value = sqrt(value)/temp2new;
  126. if (value < bestval)
  127. {
  128. bestval=value;
  129. bestmatchx[i4-1]=arr1xnew[i5-1];
  130. bestmatchy[i4-1]=arr1ynew[i5-1];
  131. }
  132. }
  133. cvCircle(imagematch,cvPoint((int)bestmatchx[i4-1],(int)bestmatchy[i4-1]),3,cvScalar(0,255,255,255),1);
  134. }
  135. double xmean = 0.0; double ymean = 0.0;
  136. for(int i7=1;i7<=temp1;i7++)
  137. {
  138. xmean += bestmatchx[i7-1];
  139. ymean += bestmatchy[i7-1];
  140. }
  141. xmean = xmean/temp1;
  142. ymean = ymean/temp1;
  143. printf("%lf %lf",xmean,ymean);
  144. getchar();
  145. cvSaveImage("savematch.jpg",imagematch);
  146. cvReleaseImage(&imagematch);
  147. return 0;
  148. }