• ASIFT算法过程实现 --- 配置避坑指南


    常规的SIFT算法进行图像匹配的时候,只能进行两个摄像机夹角比较小的(最大是15°),拍摄的图像进行相机的图像匹配,但是针对于相机之间的夹角比较大的时候,上述的算法匹配就是会出现问题.为了解决上面的这个问题,使用了一种改进的算法匹配方式ASIFT算法进行匹配.具体这种算法的优点见博客论文:基于ASIFT算法特征匹配的研究算法特征匹配的研究-图像处理文档类资源-CSDN下载在进行相机图像匹配的时候,一般的情况下我们是使用的双目相机,但是二者的视角是几乎近似平行的,平时我们更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/m0_47489229/87108632配置过程出错:
    一开始我是看的这两篇博客,进行的配置ASIFT算法在WIN7 64位系统下利用VS2012生成_zz420521的博客-CSDN博客_demo_asift欢迎大家交流,转载时请注明原文出处:http://blog.csdn.net/zlr289652981/article/details/63684857传统的sift算法在视角变化不大的情况下,可以获得良好的匹配效果,但如果在视角变化偏大时,其匹配效果会急剧下降,甚至找不到匹配点。由Guoshen Yu和Jean-Michel Morel提出的ASIFT算法很好的解决了在大视角变化条件下的图像https://blog.csdn.net/zz420521/article/details/63686163搭建可随意更改路径的VS工程-以ASIFT算法为例_zz420521的博客-CSDN博客在前一篇的博文中,笔者介绍了如何利用Guoshen Yu和Jean-Michel Morel提出的ASIFT算法的windows下C++版本源码来构建工程,具体见链接:http://blog.csdn.net/zz420521/article/details/63686163但实际上,虽然工程是跑通了,但是里面还包含了许多我们不熟悉的或者不需要的东西,尤其是一些路径设置的问题,对工程里面的设置https://blog.csdn.net/zz420521/article/details/65437441?ops_request_misc=&request_id=&biz_id=102&utm_term=vs2017%E5%AE%9E%E7%8E%B0asift%E7%AE%97%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-65437441.nonecase&spm=1018.2226.3001.4187我是用的VS2017进行配置,已经配置好的是OpenCV3.6.4+contribe3.6.4,然后按照上面的步骤进行配置完成之后,发现有错误出现,当然我的需求是使用代码进行编制,在进行生成解决方案的时候会出现一个如下的错误:

    这里的问题貌似是需要进行提前配置Eigen和Boost库,但是我发现这个地方是直接使用一个exe文件进行操作的,并不是我所需要的.百度也没有发现想要解决的东西.

    后来查阅资料发现,就是代码形式的一般是相应的python形式的,但是在查询代码的过程之中,查到一个c++形式实现这个功能的代码,如下所示:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using namespace std;
    9. using namespace cv;
    10. static void help(char** argv)
    11. {
    12. cout
    13. << "This is a sample usage of AffineFeature detector/extractor.\n"
    14. << "And this is a C++ version of samples/python/asift.py\n"
    15. << "Usage: " << argv[0] << "\n"
    16. << " [ --feature= ] # Feature to use.\n"
    17. << " [ --flann ] # use Flann-based matcher instead of bruteforce.\n"
    18. << " [ --maxlines= ] # The maximum number of lines in visualizing the matching result.\n"
    19. << " [ --image1= ]\n"
    20. << " [ --image2= ] # Path to images to compare."
    21. << endl;
    22. }
    23. static double timer()
    24. {
    25. return getTickCount() / getTickFrequency();
    26. }
    27. int main(int argc, char** argv)
    28. {
    29. vector fileName;
    30. cv::CommandLineParser parser(argc, argv,
    31. "{help h ||}"
    32. "{feature|brisk|}"
    33. "{flann||}"
    34. "{maxlines|50|}"
    35. "{image1|aero1.jpg|}{image2|aero3.jpg|}");
    36. if (parser.has("help"))
    37. {
    38. help(argv);
    39. return 0;
    40. }
    41. string feature = parser.get("feature");
    42. bool useFlann = parser.has("flann");
    43. int maxlines = parser.get<int>("maxlines");
    44. fileName.push_back(samples::findFile(parser.get("image1")));
    45. fileName.push_back(samples::findFile(parser.get("image2")));
    46. if (!parser.check())
    47. {
    48. parser.printErrors();
    49. cout << "See --help (or missing '=' between argument name and value?)" << endl;
    50. return 1;
    51. }
    52. Mat img1 = imread(fileName[0], IMREAD_GRAYSCALE);
    53. Mat img2 = imread(fileName[1], IMREAD_GRAYSCALE);
    54. if (img1.empty())
    55. {
    56. cerr << "Image " << fileName[0] << " is empty or cannot be found" << endl;
    57. return 1;
    58. }
    59. if (img2.empty())
    60. {
    61. cerr << "Image " << fileName[1] << " is empty or cannot be found" << endl;
    62. return 1;
    63. }
    64. Ptr backend;
    65. Ptr matcher;
    66. if (feature == "sift")
    67. {
    68. backend = SIFT::create();
    69. if (useFlann)
    70. matcher = DescriptorMatcher::create("FlannBased");
    71. else
    72. matcher = DescriptorMatcher::create("BruteForce");
    73. }
    74. else if (feature == "orb")
    75. {
    76. backend = ORB::create();
    77. if (useFlann)
    78. matcher = makePtr(makePtr(6, 12, 1));
    79. else
    80. matcher = DescriptorMatcher::create("BruteForce-Hamming");
    81. }
    82. else if (feature == "brisk")
    83. {
    84. backend = BRISK::create();
    85. if (useFlann)
    86. matcher = makePtr(makePtr(6, 12, 1));
    87. else
    88. matcher = DescriptorMatcher::create("BruteForce-Hamming");
    89. }
    90. else
    91. {
    92. cerr << feature << " is not supported. See --help" << endl;
    93. return 1;
    94. }
    95. cout << "extracting with " << feature << "..." << endl;
    96. Ptr ext = AffineFeature::create(backend);
    97. vector kp1, kp2;
    98. Mat desc1, desc2;
    99. ext->detectAndCompute(img1, Mat(), kp1, desc1);
    100. ext->detectAndCompute(img2, Mat(), kp2, desc2);
    101. cout << "img1 - " << kp1.size() << " features, "
    102. << "img2 - " << kp2.size() << " features"
    103. << endl;
    104. cout << "matching with " << (useFlann ? "flann" : "bruteforce") << "..." << endl;
    105. double start = timer();
    106. // match and draw
    107. vector< vector > rawMatches;
    108. vector p1, p2;
    109. vector<float> distances;
    110. matcher->knnMatch(desc1, desc2, rawMatches, 2);
    111. // filter_matches
    112. for (size_t i = 0; i < rawMatches.size(); i++)
    113. {
    114. const vector& m = rawMatches[i];
    115. if (m.size() == 2 && m[0].distance < m[1].distance * 0.75)
    116. {
    117. p1.push_back(kp1[m[0].queryIdx].pt);
    118. p2.push_back(kp2[m[0].trainIdx].pt);
    119. distances.push_back(m[0].distance);
    120. }
    121. }
    122. vector status;
    123. vector< pair > pointPairs;
    124. Mat H = findHomography(p1, p2, status, RANSAC);
    125. int inliers = 0;
    126. for (size_t i = 0; i < status.size(); i++)
    127. {
    128. if (status[i])
    129. {
    130. pointPairs.push_back(make_pair(p1[i], p2[i]));
    131. distances[inliers] = distances[i];
    132. // CV_Assert(inliers <= (int)i);
    133. inliers++;
    134. }
    135. }
    136. distances.resize(inliers);
    137. cout << "execution time: " << fixed << setprecision(2) << (timer() - start) * 1000 << " ms" << endl;
    138. cout << inliers << " / " << status.size() << " inliers/matched" << endl;
    139. cout << "visualizing..." << endl;
    140. vector<int> indices(inliers);
    141. cv::sortIdx(distances, indices, SORT_EVERY_ROW + SORT_ASCENDING);
    142. // explore_match
    143. int h1 = img1.size().height;
    144. int w1 = img1.size().width;
    145. int h2 = img2.size().height;
    146. int w2 = img2.size().width;
    147. Mat vis = Mat::zeros(max(h1, h2), w1 + w2, CV_8U);
    148. img1.copyTo(Mat(vis, Rect(0, 0, w1, h1)));
    149. img2.copyTo(Mat(vis, Rect(w1, 0, w2, h2)));
    150. cvtColor(vis, vis, COLOR_GRAY2BGR);
    151. vector corners(4);
    152. corners[0] = Point2f(0, 0);
    153. corners[1] = Point2f((float)w1, 0);
    154. corners[2] = Point2f((float)w1, (float)h1);
    155. corners[3] = Point2f(0, (float)h1);
    156. vector icorners;
    157. perspectiveTransform(corners, corners, H);
    158. transform(corners, corners, Matx23f(1, 0, (float)w1, 0, 1, 0));
    159. Mat(corners).convertTo(icorners, CV_32S);
    160. polylines(vis, icorners, true, Scalar(255, 255, 255));
    161. for (int i = 0; i < min(inliers, maxlines); i++)
    162. {
    163. int idx = indices[i];
    164. const Point2f& pi1 = pointPairs[idx].first;
    165. const Point2f& pi2 = pointPairs[idx].second;
    166. circle(vis, pi1, 2, Scalar(0, 255, 0), -1);
    167. circle(vis, pi2 + Point2f((float)w1, 0), 2, Scalar(0, 255, 0), -1);
    168. line(vis, pi1, pi2 + Point2f((float)w1, 0), Scalar(0, 255, 0));
    169. }
    170. if (inliers > maxlines)
    171. cout << "only " << maxlines << " inliers are visualized" << endl;
    172. imshow("affine find_obj", vis);
    173. waitKey();
    174. cout << "done" << endl;
    175. return 0;
    176. }

     在执行的时候,发现代码报错:

    还是老样子,这里的报错原因是我的库是opencv3.6.4,但是支持这个地方的是opencv4.5.0版本以上,还必须要配置contribe,所以一切还得重新弄.
    我在使用配置vs2015+cmake3.24.0+opencv4.5.3+contribe4.5.3过程又出现了之前的老问题,解决方案失败,就是像下面的这个所示:

    这个过程就是我是连接的外网,发现还是不行,就是需要下载很多很多的配置文件,将这些配置文件需要一个一个下载下来才可以,感觉还是比较困难的.然后就接着找博客,我发现了这一篇博客.史上最全最详细讲解 VS2015+OpenCV4.5.1+OpenCV-4.5.1_Contribute+CUDA_元宇宙MetaAI的博客-CSDN博客视频正在录制请稍等VS2015+opencv4.1+opencv4.1_contributeCmake官网CMake Warning at cmake/OpenCVDownload.cmake:202 (message)如何去掉cmake编译OpenCV时的Cmake Warning:“OpenCVGenSetupVars.cmake:54https://blog.csdn.net/CSS360/article/details/117871722?ops_request_misc=&request_id=&biz_id=102&utm_term=vs2015%20%20opencv4.5.1&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-117871722.142%5Ev66%5Econtrol,201%5Ev3%5Eadd_ask,213%5Ev2%5Et3_esquery_v2&spm=1018.2226.3001.4187

    我的电脑已经是使用vs2017配置好别的环境了,所以只能是用vs2015进行配置新的,参考上面的操作步骤,里面的东西在他的百度网盘里面下载.cache文件添加进去,就可以了.
    可以个锤子,还是不行.....................................................................................................................

    前面的配置过程都是有问题的,就是我走过的坑.


    第一种配置

    发现要安装之前还要安装一个什么CUDA什么鬼,这具体是个什么,我现在也不知道,需要在网上进行发查找,那就接着安装...

    win10 vs2015 cmake3.18.0 opencv4.5.1 opencv_contrib4.5.1 编译攻略_dragon_perfect的博客-CSDN博客_cmake编译opencv4.5.1一、前期准备:需要下载的部分有四个:OpenCV, OpenCV_contrib, CMake,VisualStudio1. 下载OpenCV and OpenCV_contrib,要求是匹配的同版本,并解压缩存储到同一文件夹下;下载OpenCV链接:https://opencv.org/releases/下载OpenCV_contrib链接 :https://github.com/opencv/opencv_contrib/releases2. CMake的下载下载链接:ht...https://blog.csdn.net/zhulong1984/article/details/112728038https://www.baidu.com/link?url=STTSCR6UH-Zr7kAZXC8sNy3h9MSenc5VtvTHiSWMeQ30_MXa0-zrnioqai1bvwuNYjH2s4jpYwHufK5vrhn-LF0xMpJ8zL9pltVB_gINiJe&wd=&eqid=a6ef9f51000221a300000006637e2a66https://www.baidu.com/link?url=STTSCR6UH-Zr7kAZXC8sNy3h9MSenc5VtvTHiSWMeQ30_MXa0-zrnioqai1bvwuNYjH2s4jpYwHufK5vrhn-LF0xMpJ8zL9pltVB_gINiJe&wd=&eqid=a6ef9f51000221a300000006637e2a66我在看完上面两个博客的介绍之后,发现配置之中不存在什么CUDA的配置,也就是说我没有安装CUDA,那就接着CSDN,github看不懂,智力有限.那么接下来请看下面的这个博文,进行配置CUDA配置过程.
    win10+cuda11.0+vs2019安装教程_离墨猫的博客-CSDN博客_cuda vs转自:https://www.jianshu.com/p/1fd15d2408bf?utm_campaign=hugo第一步:检查显卡支持的cuda版本1.第一种方法:win+R打开cmd,输入nvidia-smi,我的显卡是nvidia 2070super,支持的cuda版本是11.0图1 cmd查看显卡支持的cuda版本2.第二种方法:搜索框输入nvidia,出现nvidia控制面板,打开帮助中的系统信息,选择组件,出现cuda版本信息。第二步:安装vs2019.https://blog.csdn.net/syz201558503103/article/details/114867877?ops_request_misc=&request_id=&biz_id=102&utm_term=vs%20cuda%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-114867877.pc_v2_rank_dl_default&spm=1018.2226.3001.4449但是我看了一些实验室配的电脑的显卡,只有一个核显,没有独显,所以就是没有什么NVDIA配置,就没有什么CUDA配置.

    到这个地方思路有断了,那么我可不可以不使用cuda的加速了.又开始了在vs2015上的配置过程,但是还是生成解决方案的时候,爆出了很多的错误.
    实在没有办法了,只能在VS2017上进行配置了,突然发现可以,生成解决方案和install都没有爆出错误,很玄学. 然后我执行的代码把原来的修改了一下

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using namespace std;
    9. using namespace cv;
    10. static void help(char** argv)
    11. {
    12. cout
    13. << "This is a sample usage of AffineFeature detector/extractor.\n"
    14. << "And this is a C++ version of samples/python/asift.py\n"
    15. << "Usage: " << argv[0] << "\n"
    16. << " [ --feature= ] # Feature to use.\n"
    17. << " [ --flann ] # use Flann-based matcher instead of bruteforce.\n"
    18. << " [ --maxlines= ] # The maximum number of lines in visualizing the matching result.\n"
    19. << " [ --image1= ]\n"
    20. << " [ --image2= ] # Path to images to compare."
    21. << endl;
    22. }
    23. static double timer()
    24. {
    25. return getTickCount() / getTickFrequency();
    26. }
    27. int main(int argc, char** argv)
    28. {
    29. vector fileName;
    30. cv::CommandLineParser parser(argc, argv,
    31. "{help h ||}"
    32. "{feature|brisk|}"
    33. "{flann||}"
    34. "{maxlines|50|}"
    35. "{image1|aero1.jpg|}{image2|aero3.jpg|}");
    36. if (parser.has("help"))
    37. {
    38. help(argv);
    39. return 0;
    40. }
    41. string feature = parser.get("feature");
    42. bool useFlann = parser.has("flann");
    43. int maxlines = parser.get<int>("maxlines");
    44. fileName.push_back(samples::findFile(parser.get("image1")));
    45. fileName.push_back(samples::findFile(parser.get("image2")));
    46. if (!parser.check())
    47. {
    48. parser.printErrors();
    49. cout << "See --help (or missing '=' between argument name and value?)" << endl;
    50. return 1;
    51. }
    52. Mat img1 = imread("茉莉清茶1.jpg", IMREAD_GRAYSCALE);
    53. Mat img2 = imread("茉莉清茶2.jpg", IMREAD_GRAYSCALE);
    54. if (img1.empty())
    55. {
    56. cerr << "Image1 " << " is empty or cannot be found" << endl;
    57. return 1;
    58. }
    59. if (img2.empty())
    60. {
    61. cerr << "Image2 " << " is empty or cannot be found" << endl;
    62. return 1;
    63. }
    64. Ptr backend;
    65. Ptr matcher;
    66. if (feature == "sift")
    67. {
    68. backend = SIFT::create();
    69. if (useFlann)
    70. matcher = DescriptorMatcher::create("FlannBased");
    71. else
    72. matcher = DescriptorMatcher::create("BruteForce");
    73. }
    74. else if (feature == "orb")
    75. {
    76. backend = ORB::create();
    77. if (useFlann)
    78. matcher = makePtr(makePtr(6, 12, 1));
    79. else
    80. matcher = DescriptorMatcher::create("BruteForce-Hamming");
    81. }
    82. else if (feature == "brisk")
    83. {
    84. backend = BRISK::create();
    85. if (useFlann)
    86. matcher = makePtr(makePtr(6, 12, 1));
    87. else
    88. matcher = DescriptorMatcher::create("BruteForce-Hamming");
    89. }
    90. else
    91. {
    92. cerr << feature << " is not supported. See --help" << endl;
    93. return 1;
    94. }
    95. cout << "extracting with " << feature << "..." << endl;
    96. Ptr ext = AffineFeature::create(backend);
    97. vector kp1, kp2;
    98. Mat desc1, desc2;
    99. ext->detectAndCompute(img1, Mat(), kp1, desc1);
    100. ext->detectAndCompute(img2, Mat(), kp2, desc2);
    101. cout << "img1 - " << kp1.size() << " features, "
    102. << "img2 - " << kp2.size() << " features"
    103. << endl;
    104. cout << "matching with " << (useFlann ? "flann" : "bruteforce") << "..." << endl;
    105. double start = timer();
    106. // match and draw
    107. vector< vector > rawMatches;
    108. vector p1, p2;
    109. vector<float> distances;
    110. matcher->knnMatch(desc1, desc2, rawMatches, 2);
    111. // filter_matches
    112. for (size_t i = 0; i < rawMatches.size(); i++)
    113. {
    114. const vector& m = rawMatches[i];
    115. if (m.size() == 2 && m[0].distance < m[1].distance * 0.75)
    116. {
    117. p1.push_back(kp1[m[0].queryIdx].pt);
    118. p2.push_back(kp2[m[0].trainIdx].pt);
    119. distances.push_back(m[0].distance);
    120. }
    121. }
    122. vector status;
    123. vector< pair > pointPairs;
    124. Mat H = findHomography(p1, p2, status, RANSAC);
    125. int inliers = 0;
    126. for (size_t i = 0; i < status.size(); i++)
    127. {
    128. if (status[i])
    129. {
    130. pointPairs.push_back(make_pair(p1[i], p2[i]));
    131. distances[inliers] = distances[i];
    132. // CV_Assert(inliers <= (int)i);
    133. inliers++;
    134. }
    135. }
    136. distances.resize(inliers);
    137. cout << "execution time: " << fixed << setprecision(2) << (timer() - start) * 1000 << " ms" << endl;
    138. cout << inliers << " / " << status.size() << " inliers/matched" << endl;
    139. cout << "visualizing..." << endl;
    140. vector<int> indices(inliers);
    141. cv::sortIdx(distances, indices, SORT_EVERY_ROW + SORT_ASCENDING);
    142. // explore_match
    143. int h1 = img1.size().height;
    144. int w1 = img1.size().width;
    145. int h2 = img2.size().height;
    146. int w2 = img2.size().width;
    147. Mat vis = Mat::zeros(max(h1, h2), w1 + w2, CV_8U);
    148. img1.copyTo(Mat(vis, Rect(0, 0, w1, h1)));
    149. img2.copyTo(Mat(vis, Rect(w1, 0, w2, h2)));
    150. cvtColor(vis, vis, COLOR_GRAY2BGR);
    151. vector corners(4);
    152. corners[0] = Point2f(0, 0);
    153. corners[1] = Point2f((float)w1, 0);
    154. corners[2] = Point2f((float)w1, (float)h1);
    155. corners[3] = Point2f(0, (float)h1);
    156. vector icorners;
    157. perspectiveTransform(corners, corners, H);
    158. transform(corners, corners, Matx23f(1, 0, (float)w1, 0, 1, 0));
    159. Mat(corners).convertTo(icorners, CV_32S);
    160. polylines(vis, icorners, true, Scalar(255, 255, 255));
    161. for (int i = 0; i < min(inliers, maxlines); i++)
    162. {
    163. int idx = indices[i];
    164. const Point2f& pi1 = pointPairs[idx].first;
    165. const Point2f& pi2 = pointPairs[idx].second;
    166. circle(vis, pi1, 2, Scalar(0, 255, 0), -1);
    167. circle(vis, pi2 + Point2f((float)w1, 0), 2, Scalar(0, 255, 0), -1);
    168. line(vis, pi1, pi2 + Point2f((float)w1, 0), Scalar(0, 255, 0));
    169. }
    170. if (inliers > maxlines)
    171. cout << "only " << maxlines << " inliers are visualized" << endl;
    172. imshow("affine find_obj", vis);
    173. waitKey();
    174. cout << "done" << endl;
    175. return 0;
    176. }

    貌似是没有配置相应的cuda加速,然后代码就是一直处于卡顿的状态.

    因为ASIFT本来就是比较慢的,那只能慢慢等啦.反正我的CPU已经跑疯了. 

    这个过程是需要等待很长的时间才可以出现结果的,害,慢慢等吧.受不了了,等的时间实在太长了,等了半个小时还不行,只能是把像素降低.输出结果如下所示:

    然后这个过程使用的VS2017+cmake+opencv4.5.1+contribe4.5.1(cuda没有配置),我就放在下面的百度网盘连接里了.

    链接:https://pan.baidu.com/s/1hPGGiupQW-1khpiCe4QHGw?pwd=1111 
    提取码:1111 

    第二种配置

    这里说的配置就是前面什么一个博客VS2012之中进行配置的一个过程,不知道为什么就是我的VS2017这个方式总是有问题,所以我使用了VS2015进行了配置.

    将这个里面的代码进行改正,为什么呢?因为我看了看之前使用cmd进行操作的时候,这里会输出一定的函数,然后这里argc集合的第一个函数就是exe文件,所以第一个我就不用管,剩下的值我是自己进行配置的,由于像素的限制,就是输入的图片像素是需要进行改变的,变成一定的大小,这个是很容易实现的.后面的代码我是改成了这样的,就直接可以自己使用了.

    1. // Copyright (c) 2008-2011, Guoshen Yu
    2. // Copyright (c) 2008-2011, Jean-Michel Morel
    3. //
    4. // WARNING:
    5. // This file implements an algorithm possibly linked to the patent
    6. //
    7. // Jean-Michel Morel and Guoshen Yu, Method and device for the invariant
    8. // affine recognition recognition of shapes (WO/2009/150361), patent pending.
    9. //
    10. // This file is made available for the exclusive aim of serving as
    11. // scientific tool to verify of the soundness and
    12. // completeness of the algorithm description. Compilation,
    13. // execution and redistribution of this file may violate exclusive
    14. // patents rights in certain countries.
    15. // The situation being different for every country and changing
    16. // over time, it is your responsibility to determine which patent
    17. // rights restrictions apply to you before you compile, use,
    18. // modify, or redistribute this file. A patent lawyer is qualified
    19. // to make this determination.
    20. // If and only if they don't conflict with any patent terms, you
    21. // can benefit from the following license terms attached to this
    22. // file.
    23. //
    24. // This program is provided for scientific and educational only:
    25. // you can use and/or modify it for these purposes, but you are
    26. // not allowed to redistribute this work or derivative works in
    27. // source or executable form. A license must be obtained from the
    28. // patent right holders for any other use.
    29. //
    30. //
    31. //*----------------------------- demo_ASIFT --------------------------------*/
    32. // Detect corresponding points in two images with the ASIFT method.
    33. // Please report bugs and/or send comments to Guoshen Yu yu@cmap.polytechnique.fr
    34. //
    35. // Reference: J.M. Morel and G.Yu, ASIFT: A New Framework for Fully Affine Invariant Image
    36. // Comparison, SIAM Journal on Imaging Sciences, vol. 2, issue 2, pp. 438-469, 2009.
    37. // Reference: ASIFT online demo (You can try ASIFT with your own images online.)
    38. // http://www.ipol.im/pub/algo/my_affine_sift/
    39. /*---------------------------------------------------------------------------*/
    40. #include
    41. #include
    42. #include
    43. #include
    44. #include
    45. using namespace std;
    46. #ifdef _OPENMP
    47. #include
    48. #endif
    49. #include "demo_lib_sift.h"
    50. #include "io_png/io_png.h"
    51. #include "library.h"
    52. #include "frot.h"
    53. #include "fproj.h"
    54. #include "compute_asift_keypoints.h"
    55. #include "compute_asift_matches.h"
    56. # define IM_X 800//800
    57. # define IM_Y 600//600
    58. int main(int argc, char **argv)
    59. {
    60. argc = 8;
    61. cout << "?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????" << endl;
    62. /*
    63. if ((argc != 8) && (argc != 9)) {
    64. std::cerr << " ******************************************************************************* " << std::endl
    65. << " *************************** ASIFT image matching **************************** " << std::endl
    66. << " ******************************************************************************* " << std::endl
    67. << "Usage: " << argv[0] << " imgIn1.png imgIn2.png imgOutVert.png imgOutHori.png " << std::endl
    68. << " matchings.txt keys1.txt keys2.txt [Resize option: 0/1] " << std::endl
    69. << "- imgIn1.png, imgIn2.png: input images (in PNG format). " << std::endl
    70. << "- imgOutVert.png, imgOutHori.png: output images (vertical/horizontal concatenated, " << std::endl
    71. << " in PNG format.) The detected matchings are connected by write lines." << std::endl
    72. << "- matchings.txt: coordinates of matched points (col1, row1, col2, row2). " << std::endl
    73. << "- keys1.txt keys2.txt: ASIFT keypoints of the two images." << std::endl
    74. << "- [optional 0/1]. 1: input images resize to 800x600 (default). 0: no resize. " << std::endl
    75. << " ******************************************************************************* " << std::endl
    76. << " ********************* Jean-Michel Morel, Guoshen Yu, 2010 ******************** " << std::endl
    77. << " ******************************************************************************* " << std::endl;
    78. return 1;
    79. }
    80. */
    81. Input
    82. // Read image1
    83. argv[1] = "C:\\Users\\td\\Desktop\\xianmu\\new_stereo_match\\book3.png";
    84. argv[2] = "C:\\Users\\td\\Desktop\\xianmu\\new_stereo_match\\book4.png";
    85. argv[3] = "C:\\Users\\td\\Desktop\\xianmu\\new_stereo_match\\bookshuchu1.png";
    86. argv[4] = "C:\\Users\\td\\Desktop\\xianmu\\new_stereo_match\\bookshuchu2.png";
    87. size_t w1 , h1 ;
    88. size_t w2 , h2 ;
    89. float * iarr1;
    90. //argv[1] = "C:\\Users\\td\\Desktop\\xianmu\\new_stereo_match\\adam1.png";
    91. if (NULL == (iarr1 = read_png_f32_gray(argv[1], &w1, &h1))) {
    92. cout << iarr1 << endl;
    93. std::cerr << "Unable to load image file " << argv[1] << std::endl;
    94. return 1;
    95. }
    96. std::vector<float> ipixels1(iarr1, iarr1 + w1 * h1);
    97. free(iarr1);
    98. // Read image2
    99. float * iarr2;
    100. //argv[2] = "C:\\Users\\td\\Desktop\\xianmu\\new_stereo_match\\adam2.png";
    101. if (NULL == (iarr2 = read_png_f32_gray(argv[2], &w2, &h2))) {
    102. std::cerr << "Unable to load image file " << argv[2] << std::endl;
    103. return 1;
    104. }
    105. std::vector<float> ipixels2(iarr2, iarr2 + w2 * h2);
    106. free(iarr2);
    107. cout << "?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????" << endl;
    108. / Resize the images to area wS*hW in remaining the apsect-ratio
    109. / Resize if the resize flag is not set or if the flag is set unequal to 0
    110. float wS = IM_X;
    111. float hS = IM_Y;
    112. float zoom1=0, zoom2=0;
    113. int wS1=0, hS1=0, wS2=0, hS2=0;
    114. vector<float> ipixels1_zoom, ipixels2_zoom;
    115. int flag_resize = 1;
    116. if (argc == 9)
    117. {
    118. flag_resize = atoi(argv[8]);
    119. }
    120. if ((argc == 8) || (flag_resize != 0))
    121. {
    122. cout << "WARNING: The input images are resized to " << wS << "x" << hS << " for ASIFT. " << endl
    123. << " But the results will be normalized to the original image size." << endl << endl;
    124. float InitSigma_aa = 1.6;
    125. float fproj_p, fproj_bg;
    126. char fproj_i;
    127. float *fproj_x4, *fproj_y4;
    128. int fproj_o;
    129. fproj_o = 3;
    130. fproj_p = 0;
    131. fproj_i = 0;
    132. fproj_bg = 0;
    133. fproj_x4 = 0;
    134. fproj_y4 = 0;
    135. float areaS = wS * hS;
    136. // Resize image 1
    137. float area1 = w1 * h1;
    138. zoom1 = sqrt(area1/areaS);
    139. wS1 = (int) (w1 / zoom1);
    140. hS1 = (int) (h1 / zoom1);
    141. int fproj_sx = wS1;
    142. int fproj_sy = hS1;
    143. float fproj_x1 = 0;
    144. float fproj_y1 = 0;
    145. float fproj_x2 = wS1;
    146. float fproj_y2 = 0;
    147. float fproj_x3 = 0;
    148. float fproj_y3 = hS1;
    149. /* Anti-aliasing filtering along vertical direction */
    150. if ( zoom1 > 1 )
    151. {
    152. float sigma_aa = InitSigma_aa * zoom1 / 2;
    153. GaussianBlur1D(ipixels1,w1,h1,sigma_aa,1);
    154. GaussianBlur1D(ipixels1,w1,h1,sigma_aa,0);
    155. }
    156. // simulate a tilt: subsample the image along the vertical axis by a factor of t.
    157. ipixels1_zoom.resize(wS1*hS1);
    158. fproj (ipixels1, ipixels1_zoom, w1, h1, &fproj_sx, &fproj_sy, &fproj_bg, &fproj_o, &fproj_p,
    159. &fproj_i , fproj_x1 , fproj_y1 , fproj_x2 , fproj_y2 , fproj_x3 , fproj_y3, fproj_x4, fproj_y4);
    160. // Resize image 2
    161. float area2 = w2 * h2;
    162. zoom2 = sqrt(area2/areaS);
    163. wS2 = (int) (w2 / zoom2);
    164. hS2 = (int) (h2 / zoom2);
    165. fproj_sx = wS2;
    166. fproj_sy = hS2;
    167. fproj_x2 = wS2;
    168. fproj_y3 = hS2;
    169. /* Anti-aliasing filtering along vertical direction */
    170. if ( zoom1 > 1 )
    171. {
    172. float sigma_aa = InitSigma_aa * zoom2 / 2;
    173. GaussianBlur1D(ipixels2,w2,h2,sigma_aa,1);
    174. GaussianBlur1D(ipixels2,w2,h2,sigma_aa,0);
    175. }
    176. // simulate a tilt: subsample the image along the vertical axis by a factor of t.
    177. ipixels2_zoom.resize(wS2*hS2);
    178. fproj (ipixels2, ipixels2_zoom, w2, h2, &fproj_sx, &fproj_sy, &fproj_bg, &fproj_o, &fproj_p,
    179. &fproj_i , fproj_x1 , fproj_y1 , fproj_x2 , fproj_y2 , fproj_x3 , fproj_y3, fproj_x4, fproj_y4);
    180. }
    181. else
    182. {
    183. ipixels1_zoom.resize(w1*h1);
    184. ipixels1_zoom = ipixels1;
    185. wS1 = w1;
    186. hS1 = h1;
    187. zoom1 = 1;
    188. ipixels2_zoom.resize(w2*h2);
    189. ipixels2_zoom = ipixels2;
    190. wS2 = w2;
    191. hS2 = h2;
    192. zoom2 = 1;
    193. }
    194. / Compute ASIFT keypoints
    195. // number N of tilts to simulate t = 1, \sqrt{2}, (\sqrt{2})^2, ..., {\sqrt{2}}^(N-1)
    196. int num_of_tilts1 = 7;
    197. int num_of_tilts2 = 7;
    198. // int num_of_tilts1 = 1;
    199. // int num_of_tilts2 = 1;
    200. int verb = 0;
    201. // Define the SIFT parameters
    202. siftPar siftparameters;
    203. default_sift_parameters(siftparameters);
    204. vector< vector< keypointslist > > keys1;
    205. vector< vector< keypointslist > > keys2;
    206. int num_keys1=0, num_keys2=0;
    207. cout << "Computing keypoints on the two images..." << endl;
    208. time_t tstart, tend;
    209. tstart = time(0);
    210. num_keys1 = compute_asift_keypoints(ipixels1_zoom, wS1, hS1, num_of_tilts1, verb, keys1, siftparameters);
    211. num_keys2 = compute_asift_keypoints(ipixels2_zoom, wS2, hS2, num_of_tilts2, verb, keys2, siftparameters);
    212. tend = time(0);
    213. cout << "Keypoints computation accomplished in " << difftime(tend, tstart) << " seconds." << endl;
    214. Match ASIFT keypoints
    215. int num_matchings;
    216. matchingslist matchings;
    217. cout << "Matching the keypoints..." << endl;
    218. tstart = time(0);
    219. num_matchings = compute_asift_matches(num_of_tilts1, num_of_tilts2, wS1, hS1, wS2,
    220. hS2, verb, keys1, keys2, matchings, siftparameters);
    221. tend = time(0);
    222. cout << "Keypoints matching accomplished in " << difftime(tend, tstart) << " seconds." << endl;
    223. / Output image containing line matches (the two images are concatenated one above the other)
    224. int band_w = 20; // insert a black band of width band_w between the two images for better visibility
    225. int wo = MAX(w1,w2);
    226. int ho = h1+h2+band_w;
    227. float *opixelsASIFT = new float[wo*ho];
    228. for(int j = 0; j < (int) ho; j++)
    229. for(int i = 0; i < (int) wo; i++) opixelsASIFT[j*wo+i] = 255;
    230. /// Copy both images to output
    231. for(int j = 0; j < (int) h1; j++)
    232. for(int i = 0; i < (int) w1; i++) opixelsASIFT[j*wo+i] = ipixels1[j*w1+i];
    233. for(int j = 0; j < (int) h2; j++)
    234. for(int i = 0; i < (int) (int)w2; i++) opixelsASIFT[(h1 + band_w + j)*wo + i] = ipixels2[j*w2 + i];
    235. Draw matches
    236. matchingslist::iterator ptr = matchings.begin();
    237. for(int i=0; i < (int) matchings.size(); i++, ptr++)
    238. {
    239. draw_line(opixelsASIFT, (int) (zoom1*ptr->first.x), (int) (zoom1*ptr->first.y),
    240. (int) (zoom2*ptr->second.x), (int) (zoom2*ptr->second.y) + h1 + band_w, 255.0f, wo, ho);
    241. }
    242. / Save imgOut
    243. write_png_f32(argv[3], opixelsASIFT, wo, ho, 1);
    244. delete[] opixelsASIFT; /*memcheck*/
    245. /// Output image containing line matches (the two images are concatenated one aside the other)
    246. int woH = w1+w2+band_w;
    247. int hoH = MAX(h1,h2);
    248. float *opixelsASIFT_H = new float[woH*hoH];
    249. for(int j = 0; j < (int) hoH; j++)
    250. for(int i = 0; i < (int) woH; i++) opixelsASIFT_H[j*woH+i] = 255;
    251. /// Copy both images to output
    252. for(int j = 0; j < (int) h1; j++)
    253. for(int i = 0; i < (int) w1; i++) opixelsASIFT_H[j*woH+i] = ipixels1[j*w1+i];
    254. for(int j = 0; j < (int) h2; j++)
    255. for(int i = 0; i < (int) w2; i++) opixelsASIFT_H[j*woH + w1 + band_w + i] = ipixels2[j*w2 + i];
    256. Draw matches
    257. matchingslist::iterator ptrH = matchings.begin();
    258. for(int i=0; i < (int) matchings.size(); i++, ptrH++)
    259. {
    260. draw_line(opixelsASIFT_H, (int) (zoom1*ptrH->first.x), (int) (zoom1*ptrH->first.y),
    261. (int) (zoom2*ptrH->second.x) + w1 + band_w, (int) (zoom2*ptrH->second.y), 255.0f, woH, hoH);
    262. }
    263. / Save imgOut
    264. write_png_f32(argv[4], opixelsASIFT_H, woH, hoH, 1);
    265. delete[] opixelsASIFT_H; /*memcheck*/
    266. // Write the coordinates of the matched points (row1, col1, row2, col2) to the file argv[5]
    267. std::ofstream file(argv[5]);
    268. if (file.is_open())
    269. {
    270. // Write the number of matchings in the first line
    271. file << num_matchings << std::endl;
    272. matchingslist::iterator ptr = matchings.begin();
    273. for(int i=0; i < (int) matchings.size(); i++, ptr++)
    274. {
    275. file << zoom1*ptr->first.x << " " << zoom1*ptr->first.y << " " << zoom2*ptr->second.x <<
    276. " " << zoom2*ptr->second.y << std::endl;
    277. }
    278. }
    279. else
    280. {
    281. std::cerr << "Unable to open the file matchings.";
    282. }
    283. file.close();
    284. // Write all the keypoints (row, col, scale, orientation, desciptor (128 integers)) to
    285. // the file argv[6] (so that the users can match the keypoints with their own matching algorithm if they wish to)
    286. // keypoints in the 1st image
    287. std::ofstream file_key1(argv[6]);
    288. if (file_key1.is_open())
    289. {
    290. // Follow the same convention of David Lowe:
    291. // the first line contains the number of keypoints and the length of the desciptors (128)
    292. file_key1 << num_keys1 << " " << VecLength << " " << std::endl;
    293. for (int tt = 0; tt < (int) keys1.size(); tt++)
    294. {
    295. for (int rr = 0; rr < (int) keys1[tt].size(); rr++)
    296. {
    297. keypointslist::iterator ptr = keys1[tt][rr].begin();
    298. for(int i=0; i < (int) keys1[tt][rr].size(); i++, ptr++)
    299. {
    300. file_key1 << zoom1*ptr->x << " " << zoom1*ptr->y << " " << zoom1*ptr->scale << " " << ptr->angle;
    301. for (int ii = 0; ii < (int) VecLength; ii++)
    302. {
    303. file_key1 << " " << ptr->vec[ii];
    304. }
    305. file_key1 << std::endl;
    306. }
    307. }
    308. }
    309. }
    310. else
    311. {
    312. std::cerr << "Unable to open the file keys1.";
    313. }
    314. file_key1.close();
    315. // keypoints in the 2nd image
    316. std::ofstream file_key2(argv[7]);
    317. if (file_key2.is_open())
    318. {
    319. // Follow the same convention of David Lowe:
    320. // the first line contains the number of keypoints and the length of the desciptors (128)
    321. file_key2 << num_keys2 << " " << VecLength << " " << std::endl;
    322. for (int tt = 0; tt < (int) keys2.size(); tt++)
    323. {
    324. for (int rr = 0; rr < (int) keys2[tt].size(); rr++)
    325. {
    326. keypointslist::iterator ptr = keys2[tt][rr].begin();
    327. for(int i=0; i < (int) keys2[tt][rr].size(); i++, ptr++)
    328. {
    329. file_key2 << zoom2*ptr->x << " " << zoom2*ptr->y << " " << zoom2*ptr->scale << " " << ptr->angle;
    330. for (int ii = 0; ii < (int) VecLength; ii++)
    331. {
    332. file_key2 << " " << ptr->vec[ii];
    333. }
    334. file_key2 << std::endl;
    335. }
    336. }
    337. }
    338. }
    339. else
    340. {
    341. std::cerr << "Unable to open the file keys2.";
    342. }
    343. file_key2.close();
    344. return 0;
    345. }

     原始图片实在是太大了,我就把他的像素变小了一点点,然后输出的结果如下所示: 

    终于弄好了,太难了,花费了几天........................................
    上面的两个代码我是同时运行的,总体是第一个方式比较快,需要进行降低像素.

  • 相关阅读:
    IIS方式部署项目发布上线
    web流程设计器andflow_js已支持独立配置节点样式
    【ShardingSphere】单实例模式创建分片表、广播表、单表
    求二叉树中最大的二叉搜索子树的头节点
    关于AD9777芯片的说明以及FPGA控制实现 I
    Docker+jenkins+Gitee+Maven构建自动化部署
    7个实用有效的shopify运营策略,跨境电商卖家必知
    【英语:基础进阶_读写专项训练】G3.记叙文写作
    使用React Context和Hooks在React Native中共享蓝牙数据
    使用Babel将ES6代码转为ES5代码,从而在现有环境执行。
  • 原文地址:https://blog.csdn.net/m0_47489229/article/details/127997036