歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ASIFT+OpenCV圖像特征匹配實戰

ASIFT+OpenCV圖像特征匹配實戰

日期:2017/3/1 10:09:26   编辑:Linux編程

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

運行界面:

Copyright © Linux教程網 All Rights Reserved