PageRenderTime 656ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenCV-2.4.2/samples/c/find_obj.cpp

#
C++ | 322 lines | 273 code | 39 blank | 10 comment | 28 complexity | e135074a0b9ab9e30f0b6c4ae4e90735 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * A Demo to OpenCV Implementation of SURF
  3. * Further Information Refer to "SURF: Speed-Up Robust Feature"
  4. * Author: Liu Liu
  5. * liuliu.1987+opencv@gmail.com
  6. */
  7. #include "opencv2/objdetect/objdetect.hpp"
  8. #include "opencv2/features2d/features2d.hpp"
  9. #include "opencv2/highgui/highgui.hpp"
  10. #include "opencv2/calib3d/calib3d.hpp"
  11. #include "opencv2/nonfree/nonfree.hpp"
  12. #include "opencv2/imgproc/imgproc_c.h"
  13. #include "opencv2/legacy/legacy.hpp"
  14. #include "opencv2/legacy/compat.hpp"
  15. #include <iostream>
  16. #include <vector>
  17. #include <stdio.h>
  18. using namespace std;
  19. static void help()
  20. {
  21. printf(
  22. "This program demonstrated the use of the SURF Detector and Descriptor using\n"
  23. "either FLANN (fast approx nearst neighbor classification) or brute force matching\n"
  24. "on planar objects.\n"
  25. "Usage:\n"
  26. "./find_obj <object_filename> <scene_filename>, default is box.png and box_in_scene.png\n\n");
  27. return;
  28. }
  29. // define whether to use approximate nearest-neighbor search
  30. #define USE_FLANN
  31. #ifdef USE_FLANN
  32. static void
  33. flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
  34. const CvSeq*, const CvSeq* imageDescriptors, vector<int>& ptpairs )
  35. {
  36. int length = (int)(objectDescriptors->elem_size/sizeof(float));
  37. cv::Mat m_object(objectDescriptors->total, length, CV_32F);
  38. cv::Mat m_image(imageDescriptors->total, length, CV_32F);
  39. // copy descriptors
  40. CvSeqReader obj_reader;
  41. float* obj_ptr = m_object.ptr<float>(0);
  42. cvStartReadSeq( objectDescriptors, &obj_reader );
  43. for(int i = 0; i < objectDescriptors->total; i++ )
  44. {
  45. const float* descriptor = (const float*)obj_reader.ptr;
  46. CV_NEXT_SEQ_ELEM( obj_reader.seq->elem_size, obj_reader );
  47. memcpy(obj_ptr, descriptor, length*sizeof(float));
  48. obj_ptr += length;
  49. }
  50. CvSeqReader img_reader;
  51. float* img_ptr = m_image.ptr<float>(0);
  52. cvStartReadSeq( imageDescriptors, &img_reader );
  53. for(int i = 0; i < imageDescriptors->total; i++ )
  54. {
  55. const float* descriptor = (const float*)img_reader.ptr;
  56. CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader );
  57. memcpy(img_ptr, descriptor, length*sizeof(float));
  58. img_ptr += length;
  59. }
  60. // find nearest neighbors using FLANN
  61. cv::Mat m_indices(objectDescriptors->total, 2, CV_32S);
  62. cv::Mat m_dists(objectDescriptors->total, 2, CV_32F);
  63. cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4)); // using 4 randomized kdtrees
  64. flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64) ); // maximum number of leafs checked
  65. int* indices_ptr = m_indices.ptr<int>(0);
  66. float* dists_ptr = m_dists.ptr<float>(0);
  67. for (int i=0;i<m_indices.rows;++i) {
  68. if (dists_ptr[2*i]<0.6*dists_ptr[2*i+1]) {
  69. ptpairs.push_back(i);
  70. ptpairs.push_back(indices_ptr[2*i]);
  71. }
  72. }
  73. }
  74. #else
  75. static double
  76. compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
  77. {
  78. double total_cost = 0;
  79. assert( length % 4 == 0 );
  80. for( int i = 0; i < length; i += 4 )
  81. {
  82. double t0 = d1[i ] - d2[i ];
  83. double t1 = d1[i+1] - d2[i+1];
  84. double t2 = d1[i+2] - d2[i+2];
  85. double t3 = d1[i+3] - d2[i+3];
  86. total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
  87. if( total_cost > best )
  88. break;
  89. }
  90. return total_cost;
  91. }
  92. static int
  93. naiveNearestNeighbor( const float* vec, int laplacian,
  94. const CvSeq* model_keypoints,
  95. const CvSeq* model_descriptors )
  96. {
  97. int length = (int)(model_descriptors->elem_size/sizeof(float));
  98. int i, neighbor = -1;
  99. double d, dist1 = 1e6, dist2 = 1e6;
  100. CvSeqReader reader, kreader;
  101. cvStartReadSeq( model_keypoints, &kreader, 0 );
  102. cvStartReadSeq( model_descriptors, &reader, 0 );
  103. for( i = 0; i < model_descriptors->total; i++ )
  104. {
  105. const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
  106. const float* mvec = (const float*)reader.ptr;
  107. CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
  108. CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
  109. if( laplacian != kp->laplacian )
  110. continue;
  111. d = compareSURFDescriptors( vec, mvec, dist2, length );
  112. if( d < dist1 )
  113. {
  114. dist2 = dist1;
  115. dist1 = d;
  116. neighbor = i;
  117. }
  118. else if ( d < dist2 )
  119. dist2 = d;
  120. }
  121. if ( dist1 < 0.6*dist2 )
  122. return neighbor;
  123. return -1;
  124. }
  125. static void
  126. findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
  127. const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector<int>& ptpairs )
  128. {
  129. int i;
  130. CvSeqReader reader, kreader;
  131. cvStartReadSeq( objectKeypoints, &kreader );
  132. cvStartReadSeq( objectDescriptors, &reader );
  133. ptpairs.clear();
  134. for( i = 0; i < objectDescriptors->total; i++ )
  135. {
  136. const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
  137. const float* descriptor = (const float*)reader.ptr;
  138. CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
  139. CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
  140. int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors );
  141. if( nearest_neighbor >= 0 )
  142. {
  143. ptpairs.push_back(i);
  144. ptpairs.push_back(nearest_neighbor);
  145. }
  146. }
  147. }
  148. #endif
  149. /* a rough implementation for object location */
  150. static int
  151. locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
  152. const CvSeq* imageKeypoints, const CvSeq* imageDescriptors,
  153. const CvPoint src_corners[4], CvPoint dst_corners[4] )
  154. {
  155. double h[9];
  156. CvMat _h = cvMat(3, 3, CV_64F, h);
  157. vector<int> ptpairs;
  158. vector<CvPoint2D32f> pt1, pt2;
  159. CvMat _pt1, _pt2;
  160. int i, n;
  161. #ifdef USE_FLANN
  162. flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
  163. #else
  164. findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
  165. #endif
  166. n = (int)(ptpairs.size()/2);
  167. if( n < 4 )
  168. return 0;
  169. pt1.resize(n);
  170. pt2.resize(n);
  171. for( i = 0; i < n; i++ )
  172. {
  173. pt1[i] = ((CvSURFPoint*)cvGetSeqElem(objectKeypoints,ptpairs[i*2]))->pt;
  174. pt2[i] = ((CvSURFPoint*)cvGetSeqElem(imageKeypoints,ptpairs[i*2+1]))->pt;
  175. }
  176. _pt1 = cvMat(1, n, CV_32FC2, &pt1[0] );
  177. _pt2 = cvMat(1, n, CV_32FC2, &pt2[0] );
  178. if( !cvFindHomography( &_pt1, &_pt2, &_h, CV_RANSAC, 5 ))
  179. return 0;
  180. for( i = 0; i < 4; i++ )
  181. {
  182. double x = src_corners[i].x, y = src_corners[i].y;
  183. double Z = 1./(h[6]*x + h[7]*y + h[8]);
  184. double X = (h[0]*x + h[1]*y + h[2])*Z;
  185. double Y = (h[3]*x + h[4]*y + h[5])*Z;
  186. dst_corners[i] = cvPoint(cvRound(X), cvRound(Y));
  187. }
  188. return 1;
  189. }
  190. int main(int argc, char** argv)
  191. {
  192. const char* object_filename = argc == 3 ? argv[1] : "box.png";
  193. const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";
  194. cv::initModule_nonfree();
  195. help();
  196. IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
  197. IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
  198. if( !object || !image )
  199. {
  200. fprintf( stderr, "Can not load %s and/or %s\n",
  201. object_filename, scene_filename );
  202. exit(-1);
  203. }
  204. CvMemStorage* storage = cvCreateMemStorage(0);
  205. cvNamedWindow("Object", 1);
  206. cvNamedWindow("Object Correspond", 1);
  207. static CvScalar colors[] =
  208. {
  209. {{0,0,255}},
  210. {{0,128,255}},
  211. {{0,255,255}},
  212. {{0,255,0}},
  213. {{255,128,0}},
  214. {{255,255,0}},
  215. {{255,0,0}},
  216. {{255,0,255}},
  217. {{255,255,255}}
  218. };
  219. IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
  220. cvCvtColor( object, object_color, CV_GRAY2BGR );
  221. CvSeq* objectKeypoints = 0, *objectDescriptors = 0;
  222. CvSeq* imageKeypoints = 0, *imageDescriptors = 0;
  223. int i;
  224. CvSURFParams params = cvSURFParams(500, 1);
  225. double tt = (double)cvGetTickCount();
  226. cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params );
  227. printf("Object Descriptors: %d\n", objectDescriptors->total);
  228. cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
  229. printf("Image Descriptors: %d\n", imageDescriptors->total);
  230. tt = (double)cvGetTickCount() - tt;
  231. printf( "Extraction time = %gms\n", tt/(cvGetTickFrequency()*1000.));
  232. CvPoint src_corners[4] = {{0,0}, {object->width,0}, {object->width, object->height}, {0, object->height}};
  233. CvPoint dst_corners[4];
  234. IplImage* correspond = cvCreateImage( cvSize(image->width, object->height+image->height), 8, 1 );
  235. cvSetImageROI( correspond, cvRect( 0, 0, object->width, object->height ) );
  236. cvCopy( object, correspond );
  237. cvSetImageROI( correspond, cvRect( 0, object->height, correspond->width, correspond->height ) );
  238. cvCopy( image, correspond );
  239. cvResetImageROI( correspond );
  240. #ifdef USE_FLANN
  241. printf("Using approximate nearest neighbor search\n");
  242. #endif
  243. if( locatePlanarObject( objectKeypoints, objectDescriptors, imageKeypoints,
  244. imageDescriptors, src_corners, dst_corners ))
  245. {
  246. for( i = 0; i < 4; i++ )
  247. {
  248. CvPoint r1 = dst_corners[i%4];
  249. CvPoint r2 = dst_corners[(i+1)%4];
  250. cvLine( correspond, cvPoint(r1.x, r1.y+object->height ),
  251. cvPoint(r2.x, r2.y+object->height ), colors[8] );
  252. }
  253. }
  254. vector<int> ptpairs;
  255. #ifdef USE_FLANN
  256. flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
  257. #else
  258. findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
  259. #endif
  260. for( i = 0; i < (int)ptpairs.size(); i += 2 )
  261. {
  262. CvSURFPoint* r1 = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, ptpairs[i] );
  263. CvSURFPoint* r2 = (CvSURFPoint*)cvGetSeqElem( imageKeypoints, ptpairs[i+1] );
  264. cvLine( correspond, cvPointFrom32f(r1->pt),
  265. cvPoint(cvRound(r2->pt.x), cvRound(r2->pt.y+object->height)), colors[8] );
  266. }
  267. cvShowImage( "Object Correspond", correspond );
  268. for( i = 0; i < objectKeypoints->total; i++ )
  269. {
  270. CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, i );
  271. CvPoint center;
  272. int radius;
  273. center.x = cvRound(r->pt.x);
  274. center.y = cvRound(r->pt.y);
  275. radius = cvRound(r->size*1.2/9.*2);
  276. cvCircle( object_color, center, radius, colors[0], 1, 8, 0 );
  277. }
  278. cvShowImage( "Object", object_color );
  279. cvWaitKey(0);
  280. cvDestroyWindow("Object");
  281. cvDestroyWindow("Object Correspond");
  282. return 0;
  283. }