SIFT.cpp
上传用户:yli86818
上传日期:2014-07-15
资源大小:273k
文件大小:2k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. // SIFT.cpp: implementation of the CSIFT class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "TestSIFT.h"
  6. #include "SIFT.h"
  7. #include "highgui.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CSIFT::CSIFT()
  17. : SCALE_LEVEL(2),
  18.   OCTAVE_SIGMA(1.6),
  19.   PRESMOOTH_SIGMA(1.5),
  20.   EDGE_RATIO(10.0), // Lowe use 10.0
  21.   RELOCATION_MAX(4),
  22.   DOG_THRESH(0.0075), 
  23.   DVALUE_LOW_THRESH(0.008), //
  24.   SCALE_ADJ_THRESH(0.50),
  25.   OCTAVE_COUNT(1)
  26. {
  27. }
  28. CSIFT::~CSIFT()
  29. {
  30. }
  31. void CSIFT::detect(IplImage *img,std::vector <CKeyPoint*>* keypoints)
  32. {
  33. IplImage *smoothedImg = cvCreateImage (cvGetSize(img),IPL_DEPTH_8U,1);
  34. cvSmooth (img,smoothedImg,CV_GAUSSIAN,0,0,PRESMOOTH_SIGMA);
  35. for (int i = 0; i < OCTAVE_COUNT; i++)
  36. {
  37. CScaleSpace *scaleSpace = new CScaleSpace(SCALE_LEVEL,exp(i*log(2)));
  38. scaleSpace->buildDoG(smoothedImg,OCTAVE_SIGMA,1.0);
  39. std::vector <CScalePoint> peaks;
  40. std::vector <CScalePoint> filtered;
  41. std::vector <CKeyPoint*> octaveKeypoints;
  42. scaleSpace->findPeak (EDGE_RATIO,DOG_THRESH,&peaks);
  43. scaleSpace->filterAndLocalizePeaks(peaks,&filtered,DVALUE_LOW_THRESH,SCALE_ADJ_THRESH,RELOCATION_MAX);
  44. //TRACE ("peaks = %d,filtered = %dn",peaks.size(),filtered.size());
  45. //TRACE ("old x = %d,new x = %fn",filtered[0].m_x,filtered[0].m_fineX);
  46. scaleSpace->GenMagnitudeAndDirectionMaps();
  47. scaleSpace->GenerateKeypoints (filtered, &octaveKeypoints, SCALE_LEVEL, OCTAVE_SIGMA);
  48. scaleSpace->ClearMagnitudeAndDirectionMaps();
  49. if (i > 0)
  50. TRACE ("keypoints found in octave %d= %dn",i,octaveKeypoints.size());
  51. for (int j = 0; j < octaveKeypoints.size(); j++)
  52. keypoints->push_back(octaveKeypoints[j]);
  53. CvSize size = cvGetSize(smoothedImg);
  54. size.width /= 2;
  55. size.height /= 2;
  56. cvReleaseImage (&smoothedImg);
  57. if (i < OCTAVE_COUNT-1)
  58. {
  59. smoothedImg = cvCreateImage (size,IPL_DEPTH_32F,1);
  60. cvResize(scaleSpace->getDoG(SCALE_LEVEL+1),smoothedImg,CV_INTER_NN);
  61. }
  62. delete scaleSpace;
  63. }
  64. }