Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. #include <cv.h>
  2. #include <highgui.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <iostream>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. static double dis=0;//For calculating the distance
  12.  
  13.  
  14. IplImage *image = 0;
  15.  
  16. double
  17. compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
  18. {
  19. double total_cost = 0;
  20. assert( length % 4 == 0 );
  21. for( int i = 0; i < length; i += 4 )
  22. {
  23. double t0 = d1[i] - d2[i];
  24. double t1 = d1[i+1] - d2[i+1];
  25. double t2 = d1[i+2] - d2[i+2];
  26. double t3 = d1[i+3] - d2[i+3];
  27. total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
  28.  
  29. if( total_cost > best )
  30. break;
  31. }
  32. return total_cost;
  33. }
  34.  
  35.  
  36. int
  37. naiveNearestNeighbor( const float* vec, int laplacian,
  38. const CvSeq* model_keypoints,
  39. const CvSeq* model_descriptors )
  40. {
  41. int length = (int)(model_descriptors->elem_size/sizeof(float));
  42. int i, neighbor = -1;
  43. double d, dist1 = 1e6, dist2 = 1e6;
  44. CvSeqReader reader, kreader;
  45. cvStartReadSeq( model_keypoints, &kreader, 0 );
  46. cvStartReadSeq( model_descriptors, &reader, 0 );
  47.  
  48. for( i = 0; i < model_descriptors->total; i++ )
  49. {
  50. const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
  51. const float* mvec = (const float*)reader.ptr;
  52. CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
  53. CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
  54. if( laplacian != kp->laplacian )
  55. continue;
  56. d = compareSURFDescriptors( vec, mvec, dist2, length );
  57.  
  58. if( d < dist1 )
  59. {
  60. dist2 = dist1;
  61. dist1 = d;
  62. neighbor = i;
  63. }
  64. else if ( d < dist2 )
  65. dist2 = d;
  66. }
  67. dis=dis+dist1;
  68.  
  69. /*We are finding the distance from every descriptor of probe image to every descriptor of the galley image. Finally in the findpairs function, we divide this distance with the total no. of descriptors to get the average of all the distances
  70. */
  71. if ( dist1 < 0.6*dist2 )
  72. return neighbor;
  73. return -1;
  74. }
  75.  
  76. void
  77. findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
  78. const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector<int>& ptpairs )
  79. {
  80. int i;
  81. CvSeqReader reader, kreader;
  82. cvStartReadSeq( objectKeypoints, &kreader );
  83. cvStartReadSeq( objectDescriptors, &reader );
  84. ptpairs.clear();
  85.  
  86. for( i = 0; i < objectDescriptors->total; i++ )
  87. {
  88. const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
  89. const float* descriptor = (const float*)reader.ptr;
  90. CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
  91. CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
  92. int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors);
  93. //For every descriptor, we are trying to find it's nearest neighbour in the probe image
  94. if( nearest_neighbor >= 0 )
  95. {
  96. ptpairs.push_back(i);
  97. ptpairs.push_back(nearest_neighbor);
  98. }
  99. }
  100.  
  101. printf("n%lfn",(dis/objectDescriptors->total));////Here's where I am outputting the distance between the images
  102. /*Dileep: If you are using this for recognition, write this distance to a file along with the name of the image you are matching against. After doing this for several images, you can then sort them in ascending order to find the best possible match - the one with the smallest distance. Here, I am outputting the distance to stdout
  103. */
  104. }
  105.  
  106. int main(int argc, char** argv)
  107. {
  108. const char* object_filename = argc == 3 ? argv[1] : "box.png";
  109. const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";
  110. //Dileep:When you are excuting the object file, please write Command:./objectfile probe_image Gallery_image
  111. /*Dileep:
  112. Probe_image - This is the image for which you need to find the match
  113. Gallery_image - This is one of the set of images, you use for matching
  114.  
  115. You keep the same probe image same, repeatedly changing the gallery image and outputting the distance in the format
  116. <Gallery_name distance> into a file
  117. Finally you can sort the distances in ascending order. And the one with the shortest distance - You can output it's name as the best possible match
  118.  
  119. It may become tedious to continually write the same command multiple times, changing the gallery file name. Try to use shell script with a for loop
  120. */
  121. CvMemStorage* storage = cvCreateMemStorage(0);
  122.  
  123.  
  124.  
  125.  
  126.  
  127. IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
  128. IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
  129. if( !object || !image )
  130. {
  131. fprintf( stderr, "Can not load %s and/or %sn"
  132. "Usage: find_obj [<object_filename> <scene_filename>]n",
  133. object_filename, scene_filename );
  134. exit(-1);
  135. }
  136.  
  137. CvSeq *objectKeypoints = 0, *objectDescriptors = 0;
  138. CvSeq *imageKeypoints = 0, *imageDescriptors = 0;
  139. int i;
  140. CvSURFParams params = cvSURFParams(500, 1);
  141.  
  142. double tt = (double)cvGetTickCount();
  143. cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params );
  144. printf("Object Descriptors: %dn", objectDescriptors->total);
  145. cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
  146. printf("Image Descriptors: %dn", imageDescriptors->total);
  147. tt = (double)cvGetTickCount() - tt;
  148. printf( "Extraction time = %gmsn", tt/(cvGetTickFrequency()*1000.));
  149.  
  150.  
  151.  
  152.  
  153. vector<int> ptpairs;
  154.  
  155. findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
  156.  
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement