OpenCVDetect.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/xfeatures2d.hpp>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <filesystem>
  7. using namespace cv;
  8. using namespace cv::xfeatures2d;
  9. using namespace std;
  10. using namespace std::filesystem;
  11. typedef void process(const path& input, const path& output);
  12. const auto detector1 = SIFT::create();
  13. const auto detector2 = SURF::create();
  14. /// <summary>
  15. /// 提取图片特征描述符
  16. /// </summary>
  17. /// <param name="image">待提取图片路径</param>
  18. /// <param name="output">保存描述符路径</param>
  19. void process_image1(const path& image, const path& output) {
  20. ofstream fs(output.string().c_str());
  21. if (!fs.is_open()) {
  22. cout << "create file error!";
  23. }
  24. fs.close();
  25. Mat input_image = imread(image.string());
  26. vector<KeyPoint> keypoints_cat;
  27. Mat descriptor_cat;
  28. detector1->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
  29. cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
  30. //for (int i = 0; i < descriptor_cat.rows; i++) {
  31. // for (int j = 0; j < descriptor_cat.cols; j++) {
  32. // auto element = descriptor_cat.at<float>(i, j);
  33. // cout << element << endl;
  34. // }
  35. //}
  36. Mat convert;
  37. if (descriptor_cat.type() != CV_8U)
  38. {
  39. descriptor_cat.convertTo(convert, CV_8U, 1.0, 0);
  40. }
  41. else
  42. {
  43. convert = descriptor_cat;
  44. }
  45. cout << "after: " << convert.type() << " - " << convert.channels() << endl;
  46. imwrite(output.string(), convert);
  47. }
  48. void process_image2(const path& image, const path& output) {
  49. ofstream fs(output.string().c_str());
  50. if (!fs.is_open()) {
  51. cout << "create file error!";
  52. }
  53. fs.close();
  54. Mat input_image = imread(image.string());
  55. vector<KeyPoint> keypoints_cat;
  56. Mat descriptor_cat;
  57. detector2->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
  58. cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
  59. //for (int i = 0; i < descriptor_cat.rows; i++) {
  60. // for (int j = 0; j < descriptor_cat.cols; j++) {
  61. // auto element = descriptor_cat.at<float>(i, j);
  62. // cout << element << endl;
  63. // }
  64. //}
  65. Mat convert;
  66. if (descriptor_cat.type() != CV_8U)
  67. {
  68. descriptor_cat.convertTo(convert, CV_8U, 255.0, 0);
  69. }
  70. else
  71. {
  72. convert = descriptor_cat;
  73. }
  74. cout << "after: " << convert.type() << " - " << convert.channels() << endl;
  75. imwrite(output.string(), convert);
  76. }
  77. void process_image3(const path& image, const path& output) {
  78. ofstream fs(output.string().c_str());
  79. if (!fs.is_open()) {
  80. cout << "create file error!";
  81. }
  82. fs.close();
  83. Mat input_image = imread(image.string());
  84. vector<KeyPoint> keypoints_cat;
  85. Mat descriptor_cat;
  86. detector1->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
  87. cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
  88. //for (int i = 0; i < descriptor_cat.rows; i++) {
  89. // for (int j = 0; j < descriptor_cat.cols; j++) {
  90. // auto element = descriptor_cat.at<float>(i, j);
  91. // cout << element << endl;
  92. // }
  93. //}
  94. cout << "after: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
  95. FileStorage file_storage(output.string(), FileStorage::WRITE);
  96. file_storage << "card-des" << descriptor_cat;
  97. file_storage.release();
  98. }
  99. void process_image4(const path& image, const path& output) {
  100. ofstream fs(output.string().c_str());
  101. if (!fs.is_open()) {
  102. cout << "create file error!";
  103. }
  104. fs.close();
  105. Mat input_image = imread(image.string());
  106. vector<KeyPoint> keypoints_cat;
  107. Mat descriptor_cat;
  108. detector2->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
  109. cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
  110. //for (int i = 0; i < descriptor_cat.rows; i++) {
  111. // for (int j = 0; j < descriptor_cat.cols; j++) {
  112. // auto element = descriptor_cat.at<float>(i, j);
  113. // cout << element << endl;
  114. // }
  115. //}
  116. FileStorage file_storage(output.string(), FileStorage::WRITE);
  117. file_storage << "card-des" << descriptor_cat;
  118. file_storage.release();
  119. }
  120. void deep_copy(const path& src, const path& des, const string& suffix, process* process) {
  121. if (is_regular_file(src)) {
  122. auto d = des.string().substr(0, des.string().size() - 3) + suffix + "\0";
  123. process(src, path(d));
  124. return;
  125. }
  126. if (!exists(des)) {
  127. create_directories(des);
  128. }
  129. for (auto& it : directory_iterator(src)) {
  130. string current = it.path().lexically_relative(it.path().parent_path()).string();
  131. path des_next(des);
  132. deep_copy(it.path(), des_next /= current, suffix, process);
  133. }
  134. }
  135. void process_mat(string input_dir, string output_dir, const string& suffix, process* process) {
  136. path input_dir_path(input_dir.c_str());
  137. path output_dir_path(output_dir.c_str());
  138. if (!exists(input_dir_path)) {
  139. cout << "请设置正确的卡片存放路径" << endl;
  140. }
  141. if (exists(output_dir_path)) {
  142. remove_all(output_dir_path);
  143. }
  144. else {
  145. create_directories(output_dir_path);
  146. }
  147. directory_entry input_entry(input_dir_path);
  148. assert(input_entry.status().type() == file_type::directory);
  149. copy(input_dir_path, output_dir_path);
  150. deep_copy(input_dir_path, output_dir_path, suffix, process);
  151. }
  152. int main(int argc, char** argv) {
  153. string input_dir = "D:\\Work\\Cards";
  154. //string output_dir = "D:\\Work\\Outputs";
  155. string output_dir1 = "D:\\Work\\SIFT";
  156. string output_dir2 = "D:\\Work\\SURF";
  157. string output_dir3 = "D:\\Work\\SIFT-XML";
  158. string output_dir4 = "D:\\Work\\SURF-XML";
  159. process_mat(input_dir, output_dir1, "png", process_image1);
  160. process_mat(input_dir, output_dir2, "png", process_image2);
  161. process_mat(input_dir, output_dir3, "xml", process_image3);
  162. process_mat(input_dir, output_dir4, "xml", process_image4);
  163. return 0;
  164. }