ASIFT+OpenCV图像特征匹配实战


OpenCV包含头文件:

  1. #include "cv.h"   
  2. #include "highgui.h"   
  3. #include "cxcore.h"  
核心代码如下:
  1. if (!m_pImage1||!m_pImage2)  
  2.  {  
  3.   AfxMessageBox("please,select 2 images!");  
  4.    return;   
  5.  }  
  6.   
  7.  UpdateData(TRUE);  
  8.     
  9.  CvSize sz1 = cvSize(m_pImage1->width,m_pImage1->height);  
  10.  CvSize sz2 = cvSize(m_pImage2->width,m_pImage2->height);  
  11.   
  12.  CvScalar s;  
  13.   
  14.  IplImage *gimg1 = cvCreateImage(sz1,IPL_DEPTH_8U,1);  
  15.  cvCvtColor(m_pImage1,gimg1,CV_BGR2GRAY);    
  16.   
  17.  IplImage *gimg2 = cvCreateImage(sz2,IPL_DEPTH_8U,1);  
  18.  cvCvtColor(m_pImage2,gimg2,CV_BGR2GRAY);   
  19.   
  20.  size_t w1, h1;  
  21.   
  22.  w1 = gimg1->width;  
  23.  h1 = gimg1->height;  
  24.   
  25.  float * iarr1 = new float[w1*h1];  
  26.   
  27.  for(int i=0;i<h1;i++)  
  28.  {  
  29.   for(int j=0;j<w1;j++)     
  30.   {     
  31.    s=cvGet2D(gimg1,i,j);      
  32.    iarr1[i*w1+j] = s.val[0];  
  33.   }  
  34.  }  
  35.   
  36.  vector<float> ipixels1(iarr1, iarr1 + w1 * h1);  
  37.   
  38.  delete [] iarr1;   
  39.    
  40.  size_t w2, h2;  
  41.   
  42.  w2 = gimg2->width;  
  43.  h2 = gimg2->height;  
  44.   
  45.  float * iarr2 = new float[w2*h2];  
  46.   
  47.  for(int i=0;i<h2;i++)  
  48.  {  
  49.   for(int j=0;j<w2;j++)     
  50.   {     
  51.    s=cvGet2D(gimg2,i,j);      
  52.    iarr2[i*w2+j] = s.val[0];  
  53.   }  
  54.  }  
  55.   
  56.  vector<float> ipixels2(iarr2, iarr2 + w2 * h2);  
  57.   
  58.  delete [] iarr2;  
  59.   
  60.  float wS = IM_X;  
  61.  float hS = IM_Y;  
  62.   
  63.  float zoom1=0, zoom2=0;   
  64.  int wS1=0, hS1=0, wS2=0, hS2=0;  
  65.  vector<float> ipixels1_zoom, ipixels2_zoom;   
  66.   
  67.  if (!m_bOrininal)  
  68.  {  
  69.   if (m_lWidth==0 || m_lHeight == 0)  
  70.    return;  
  71.   
  72.   wS = m_lWidth;  
  73.   hS = m_lHeight;  
  74.   
  75.   float InitSigma_aa = 1.6;  
  76.   
  77.   float fproj_p, fproj_bg;  
  78.   char fproj_i;  
  79.   float *fproj_x4, *fproj_y4;  
  80.   int fproj_o;  
  81.   
  82.   fproj_o = 3;  
  83.   fproj_p = 0;  
  84.   fproj_i = 0;  
  85.   fproj_bg = 0;  
  86.   fproj_x4 = 0;  
  87.   fproj_y4 = 0;  
  88.   
  89.   float areaS = wS * hS;  
  90.   
  91.   // Resize image 1    
  92.   float area1 = w1 * h1;  
  93.   zoom1 = sqrt(area1/areaS);  
  94.   
  95.   wS1 = (int) (w1 / zoom1);  
  96.   hS1 = (int) (h1 / zoom1);  
  97.   
  98.   int fproj_sx = wS1;  
  99.   int fproj_sy = hS1;       
  100.   
  101.   float fproj_x1 = 0;  
  102.   float fproj_y1 = 0;  
  103.   float fproj_x2 = wS1;  
  104.   float fproj_y2 = 0;  
  105.   float fproj_x3 = 0;        
  106.   float fproj_y3 = hS1;  
  107.   
  108.   /* Anti-aliasing filtering along vertical direction */  
  109.   if ( zoom1 > 1 )  
  110.   {  
  111.    float sigma_aa = InitSigma_aa * zoom1 / 2;  
  112.    GaussianBlur1D(ipixels1,w1,h1,sigma_aa,1);  
  113.    GaussianBlur1D(ipixels1,w1,h1,sigma_aa,0);  
  114.   }  
  115.   
  116.   // simulate a tilt: subsample the image along the vertical axis by a factor of t.   
  117.   ipixels1_zoom.resize(wS1*hS1);  
  118.   fproj (ipixels1, ipixels1_zoom, w1, h1, &fproj_sx, &fproj_sy, &fproj_bg, &fproj_o, &fproj_p,   
  119.    &fproj_i , fproj_x1 , fproj_y1 , fproj_x2 , fproj_y2 , fproj_x3 , fproj_y3, fproj_x4, fproj_y4);   
  120.   
  121.   
  122.   // Resize image 2    
  123.   float area2 = w2 * h2;  
  124.   zoom2 = sqrt(area2/areaS);  
  125.   
  126.   wS2 = (int) (w2 / zoom2);  
  127.   hS2 = (int) (h2 / zoom2);  
  128.   
  129.   fproj_sx = wS2;  
  130.   fproj_sy = hS2;       
  131.   
  132.   fproj_x2 = wS2;  
  133.   fproj_y3 = hS2;  
  134.   
  135.   /* Anti-aliasing filtering along vertical direction */  
  136.   if ( zoom1 > 1 )  
  137.   {  
  138.    float sigma_aa = InitSigma_aa * zoom2 / 2;  
  139.    GaussianBlur1D(ipixels2,w2,h2,sigma_aa,1);  
  140.    GaussianBlur1D(ipixels2,w2,h2,sigma_aa,0);  
  141.   }  
  142.   
  143.   // simulate a tilt: subsample the image along the vertical axis by a factor of t.   
  144.   ipixels2_zoom.resize(wS2*hS2);  
  145.   fproj (ipixels2, ipixels2_zoom, w2, h2, &fproj_sx, &fproj_sy, &fproj_bg, &fproj_o, &fproj_p,   
  146.    &fproj_i , fproj_x1 , fproj_y1 , fproj_x2 , fproj_y2 , fproj_x3 , fproj_y3, fproj_x4, fproj_y4);  
  147.  }  
  148.  else   
  149.  {  
  150.   ipixels1_zoom.resize(w1*h1);   
  151.   ipixels1_zoom = ipixels1;  
  152.   wS1 = w1;  
  153.   hS1 = h1;  
  154.   zoom1 = 1;  
  155.   
  156.   ipixels2_zoom.resize(w2*h2);   
  157.   ipixels2_zoom = ipixels2;  
  158.   wS2 = w2;  
  159.   hS2 = h2;  
  160.   zoom2 = 1;  
  161.  }  
  162.   
  163.  int num_of_tilts1 = m_lTilts1;  
  164.  int num_of_tilts2 = m_lTilts2;  
  165.   
  166.  int verb = 0;  
  167.  // Define the SIFT parameters   
  168.  siftPar siftparameters;   
  169.  default_sift_parameters(siftparameters);  
  170.   
  171.  vector< vector< keypointslist > > keys1;    
  172.  vector< vector< keypointslist > > keys2;   
  173.   
  174.  int num_keys1=0, num_keys2=0;  
  175.     
  176.  SetWindowText("Computing keypoints on the two images...");  
  177.   
  178.  CString str1,str2;  
  179.   
  180.  time_t tstart, tend1,tend2;   
  181.  tstart = time(0);  
  182.  DWORD dstart = GetTickCount();  
  183.   
  184.  num_keys1 = compute_asift_keypoints(ipixels1_zoom, wS1, hS1, num_of_tilts1, verb, keys1, siftparameters);  
  185.   
  186.  tend1 = time(0);  
  187.   
  188.  m_lKeyNum1 = num_keys1;  
  189.  UpdateData(FALSE);  
  190.    
  191.  str1.Format("Img1 Keypoints computation accomplished in %f s",difftime(tend1, tstart));  
  192.  SetWindowText(str1);  
  193.   
  194.  num_keys2 = compute_asift_keypoints(ipixels2_zoom, wS2, hS2, num_of_tilts2, verb, keys2, siftparameters);  
  195.   
  196.  tend2 = time(0);  
  197.   
  198.  m_lKeyNum2 = num_keys2;  
  199.  UpdateData(FALSE);  
  200.   
  201.  str2.Format("Img2 Keypoints computation accomplished in %f s ,Matching the keypoints...",difftime(tend2, tstart));  
  202.  SetWindowText(str2);    
  203.   
  204.  //// Match ASIFT keypoints   
  205.  int num_matchings;  
  206.  matchingslist matchings;    
  207.   
  208.  tstart = time(0);  
  209.  num_matchings = compute_asift_matches(num_of_tilts1, num_of_tilts2, wS1, hS1, wS2,   
  210.   hS2, verb, keys1, keys2, matchings, siftparameters);  
  211.  tend1 = time(0);  
  212.  DWORD dSpan = GetTickCount() - dstart;  
  213.   
  214.  cout << "Keypoints matching accomplished in " << difftime(tend1, tstart) << " seconds." << endl;  
  215.  str2.Format("Keypoints matching accomplished in %f s",difftime(tend1, tstart));  
  216.  SetWindowText(str2);  
  217.   
  218.  m_lMatches = num_matchings;  
  219.  UpdateData(FALSE);  
  220.   
  221.  str1.Format("Total time used:%d ms",dSpan);  
  222.    
  223.  AfxMessageBox(str1);  
  224.   
  225.  cvRelease((void**)&gimg1);  
  226.  cvRelease((void**)&gimg2);  

运行界面:

ASIFT+OpenCV图像特征匹配实战

相关内容