Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include "opencv2/core/core.hpp"
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include <iostream>
  5.  
  6. //using namespace cv;
  7. //using namespace std;
  8.  
  9. int matching(cv::Mat img_1, cv::Mat img_2, std::vector<cv::KeyPoint> keypoints_1, cv::Mat descriptors_1, std::map<std::pair<float, float>, int> &points) {
  10. std::vector<cv::KeyPoint> keypoints_2;
  11. cv::Mat descriptors_2;
  12. cv::Ptr<cv::ORB> orb = cv::ORB::create();
  13.  
  14. //keypoints преобразованной картинки
  15. orb->detect(img_2, keypoints_2);
  16. orb->compute(img_2, keypoints_2, descriptors_2);
  17.  
  18. //-- matching descriptor vectors using FLANN matcher
  19. cv::BFMatcher matcher;
  20. std::vector<cv::DMatch> matches;
  21. cv::Mat img_matches, keypoints;
  22. if (!descriptors_1.empty() && !descriptors_2.empty()) {
  23. matcher.match(descriptors_1, descriptors_2, matches);
  24. double max_dist = 0; double min_dist = 100;
  25.  
  26. // calculation of max and min idstance between keypoints
  27. for (int i = 0; i < descriptors_1.rows; i++)
  28. {
  29. double dist = matches[i].distance;
  30. if (dist < min_dist) min_dist = dist;
  31. if (dist > max_dist) max_dist = dist;
  32. }
  33.  
  34. //Запоминаем хорошие совпадения
  35. //std::vector<cv::DMatch>good_matches;
  36. std::map<std::pair<float, float>, int>::iterator it;
  37. for (int i = 0; i < descriptors_1.rows; i++) {
  38. if (matches[i].distance <= std::max(2.8 * min_dist, 0.05)) {
  39. //good_matches.push_back(matches[i]);
  40. it = points.find({ keypoints_1[i].pt.x, keypoints_1[i].pt.y });
  41. if (it != points.end()) {
  42. it->second += 1;
  43. } else {
  44. points.insert({ { keypoints_1[i].pt.x, keypoints_1[i].pt.y }, 1 });
  45. }
  46. }
  47. }
  48. //drawKeypoints(img_2, keypoints_2, keypoints, cv::Scalar::all(-1), cv::DrawMatchesFlags::DEFAULT);
  49. //drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1),
  50. //std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  51. }
  52. //imshow("keypoints", keypoints);
  53. //imshow("match result", img_matches);
  54. cv::waitKey();
  55.  
  56. return 0;
  57. }
  58.  
  59. cv::Mat show_best_points(cv::Mat input_color) {
  60. cv::Mat input;
  61. cv::cvtColor(input_color, input, 0);
  62. std::map<std::pair<float, float>, int> points;
  63.  
  64. //keypoints исходной картинки
  65. std::vector<cv::KeyPoint> keypoints_1;
  66. cv::Mat descriptors_1;
  67. cv::Ptr<cv::ORB> orb = cv::ORB::create();
  68. orb->detect(input, keypoints_1);
  69. orb->compute(input, keypoints_1, descriptors_1);
  70.  
  71. //перспектива
  72. cv::Point2f inputQuad[4];
  73. cv::Point2f outputQuad[4];
  74. for (int j = 10; j < 30; j += 2) {
  75. std::vector<std::vector<int>> array = {{-2*j, 4 * j, -8*j, -4*j, -10 * j, -8 * j, 2 * j, -2 * j}, { 2*j, 4 * j, -8 * j, 4*j, 10 * j, 8 * j, -2 * j, 2 * j},
  76. {-j, 4 * j, 8 * j, 0, 10 * j, -8 * j, -2 * j, 2 * j}, {-j, -4 * j, 8 * j, 0, 10 * j, 8 * j, -2 * j, -2 * j} };
  77. for (int i = 0; i < array.size(); ++i) {
  78. inputQuad[0] = cv::Point2f(array[i][0], array[i][1]);
  79. inputQuad[1] = cv::Point2f(input.cols + array[i][2], array[i][3]);
  80. inputQuad[2] = cv::Point2f(input.cols + array[i][4], input.rows + array[i][5]);
  81. inputQuad[3] = cv::Point2f(array[i][6], input.rows + array[i][7]);
  82. outputQuad[0] = cv::Point2f(0, 0);
  83. outputQuad[1] = cv::Point2f(input.cols - 1, 0);
  84. outputQuad[2] = cv::Point2f(input.cols - 1, input.rows - 1);
  85. outputQuad[3] = cv::Point2f(0, input.rows - 1);
  86. cv::Mat lambda = getPerspectiveTransform(inputQuad, outputQuad);
  87. cv::Mat output;
  88. cv::warpPerspective(input, output, lambda, output.size());
  89. matching(input, output, keypoints_1, descriptors_1, points);
  90. }
  91. }
  92.  
  93. //поворот
  94. for (int i = -3; i <= 3; ++i) {
  95. cv::Mat output;
  96. cv::Mat lambda = cv::getRotationMatrix2D(cv::Point2f(input.cols / 2, input.rows / 2), 30*i, 1);
  97. cv::warpAffine(input, output, lambda, output.size());
  98. matching(input, output, keypoints_1, descriptors_1, points);
  99. }
  100. /*std::cout << points.size() << '\n';
  101. for (auto elem : points) {
  102. std::cout << elem.first.first << '\t' << elem.first.second << '\t' << elem.second << '\n';
  103. }*/
  104.  
  105. //выбираем самые частые точки
  106. std::multimap<int, std::pair<float, float>> reverse_points;
  107. for (auto elem : points) {
  108. reverse_points.insert({ elem.second, elem.first });
  109. }
  110. int i = 0;
  111. std::multimap<int, std::pair<float, float>>::iterator it = reverse_points.end();
  112. std::vector<cv::KeyPoint> best_keypoints;
  113. while (i < 20) {
  114. --it;
  115. cv::Point2f p(it->second.first, it->second.second);
  116. cv::KeyPoint new_point = cv::KeyPoint(p, 5, -1, 0, 0, -1);
  117. best_keypoints.push_back(new_point);
  118. ++i;
  119. }
  120.  
  121. //Возвращаем исходное фото с отмеченными точками
  122. cv::Mat keypoints;
  123. drawKeypoints(input_color, best_keypoints, keypoints, (60, 20, 220), cv::DrawMatchesFlags::DEFAULT);
  124. imshow("keypoints", keypoints);
  125. cv::waitKey();
  126. return keypoints;
  127. }
  128.  
  129. cv::Mat find_point(cv::Mat img_color, cv::Mat input_color, cv::KeyPoint point) {
  130. cv::Mat img;
  131. cv::cvtColor(input_color, img, 0);
  132. cv::Mat input;
  133. cv::cvtColor(input_color, input, 0);
  134. std::vector<cv::KeyPoint> keypoints;
  135. cv::Mat descriptors;
  136. cv::Ptr<cv::ORB> orb = cv::ORB::create();
  137. orb->detect(input, keypoints);
  138. orb->compute(input, keypoints, descriptors);
  139.  
  140. }
  141.  
  142. int main() {
  143. cv::Mat i1 = cv::imread("room1.bmp");
  144. show_best_points(i1);
  145. /*i1 = cv::imread("room2.bmp", 0);
  146. change_perspective(i1);
  147. i1 = cv::imread("room3.bmp", 0);
  148. change_perspective(i1);
  149. i1 = cv::imread("room4.bmp", 0);
  150. change_perspective(i1);
  151. //matching(input, output, keypoints_1, descriptors_1);*/
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement