test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "WaferRebuild.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <regex>
  5. #include <opencv2/opencv.hpp>
  6. #include <dirent.h>
  7. #include <thread>
  8. // 自定义比较函数
  9. bool numeric_string_compare(const std::string& s1, const std::string& s2) {
  10. // 正则表达式匹配所有数字
  11. std::regex re("(\\d+)");
  12. std::sregex_iterator it1(s1.begin(), s1.end(), re), it2(s2.begin(), s2.end(), re);
  13. std::sregex_iterator reg_end;
  14. while (it1 != reg_end && it2 != reg_end) {
  15. int num1 = std::stoi(it1->str());
  16. int num2 = std::stoi(it2->str());
  17. if (num1 != num2)
  18. return num1 < num2;
  19. ++it1;
  20. ++it2;
  21. }
  22. // 如果所有数字相等,按字典顺序排序
  23. return s1 < s2;
  24. }
  25. int read_files_in_dir(const char* p_dir_name, std::vector<std::string>& file_names) {
  26. DIR* p_dir = opendir(p_dir_name);
  27. if (p_dir == nullptr) {
  28. return -1;
  29. }
  30. struct dirent* p_file = nullptr;
  31. while ((p_file = readdir(p_dir)) != nullptr) {
  32. if (strcmp(p_file->d_name, ".") != 0 &&
  33. strcmp(p_file->d_name, "..") != 0) {
  34. //std::string cur_file_name(p_dir_name);
  35. //cur_file_name += "/";
  36. //cur_file_name += p_file->d_name;
  37. std::string cur_file_name(p_file->d_name);
  38. file_names.push_back(cur_file_name);
  39. }
  40. }
  41. std::sort(file_names.begin(), file_names.end(), numeric_string_compare);
  42. closedir(p_dir);
  43. return 0;
  44. }
  45. int main()
  46. {
  47. WaferRebuild wr;
  48. nlohmann::json cfg;
  49. // A:/code/win_pratice/PmdPratice/cpp/WaferRebuild/x64/WaferRebuilddll/resource
  50. std::ifstream is("A:/code/win_pratice/PmdPratice/cpp/WaferRebuilddll_bak/WaferRebuilddll/resource/cfg.json");
  51. is >> cfg;
  52. auto a = wr.initParam(cfg);
  53. std::vector<std::string> file_names;
  54. const char* image_path = "A:/code/win_pratice/PmdPratice/cpp/WaferRebuilddll_bak/WaferRebuilddll/resource/img_pats/";
  55. if (read_files_in_dir(image_path, file_names) < 0) {
  56. std::cout << "read_files_in_dir failed." << std::endl;
  57. }
  58. std::vector< std::shared_ptr<infra::Image>> image_list;
  59. for (int f = 0; f < (int)file_names.size(); f++) {
  60. std::shared_ptr<infra::Image> frame = std::make_shared<infra::Image>(2048 * 2448);
  61. cv::Mat colorImage;
  62. cv::Mat img = cv::imread(std::string(image_path) + "/" + file_names[f],0);
  63. if (img.empty()) continue;
  64. // cv::cvtColor(img, colorImage, cv::COLOR_GRAY2BGR);
  65. // memcpy(frame->data(), colorImage.data, colorImage.rows * colorImage.cols * colorImage.channels());
  66. memcpy(frame->data(), img.data, img.rows * img.cols * img.channels());
  67. image_list.push_back(frame);
  68. }
  69. std::shared_ptr<infra::CloudPoint<double>> cloud_points0 = std::make_shared<infra::CloudPoint<double>>();
  70. auto result = wr.calcCloud(image_list, cloud_points0, 0);
  71. /*
  72. const int numThreads = 4;
  73. std::vector<std::thread> threads;
  74. // 创建多个 cloud_points 对象
  75. std::shared_ptr<infra::CloudPoint<double>> cloud_points0 = std::make_shared<infra::CloudPoint<double>>();
  76. std::shared_ptr<infra::CloudPoint<double>> cloud_points1 = std::make_shared<infra::CloudPoint<double>>();
  77. std::shared_ptr<infra::CloudPoint<double>> cloud_points2 = std::make_shared<infra::CloudPoint<double>>();
  78. std::shared_ptr<infra::CloudPoint<double>> cloud_points3 = std::make_shared<infra::CloudPoint<double>>();
  79. // 将 cloud_points 放入 vector
  80. std::vector<std::shared_ptr<infra::CloudPoint<double>>> cloud_points = { cloud_points0, cloud_points1, cloud_points2, cloud_points3 };
  81. // 创建多个线程并运行 calcCloud 函数,每个线程传递不同的参数组合
  82. for (int i = 0; i < numThreads; ++i) {
  83. threads.push_back(std::thread([&wr, &image_list, &cloud_points, i]() {
  84. auto result = wr.calcCloud(image_list, cloud_points[i], i);
  85. // 可以在这里处理函数返回的结果,如输出或其他逻辑
  86. std::cout << "Thread " << i << " finished with result: " << std::get<0>(result) << ", " << std::get<1>(result) << "\n";
  87. }));
  88. }
  89. // 等待所有线程完成
  90. for (auto& thread : threads) {
  91. thread.join();
  92. }
  93. std::map<int32_t, std::shared_ptr<infra::CloudPoint<double>>> cloud_points_list = {
  94. {0, cloud_points0},
  95. {1, cloud_points1},
  96. {2, cloud_points2},
  97. {3, cloud_points3}
  98. };;
  99. std::shared_ptr<infra::CloudPoint<double>> cloud_out = std::make_shared<infra::CloudPoint<double>>();
  100. auto m_ret = wr.mergeCloud(cloud_points_list, cloud_out);
  101. std::cout << "Merge sucess!" << std::endl;
  102. std::cout << std::get<0>(m_ret) << ", " << std::get<1>(m_ret) << "\n";
  103. */
  104. std::cout << "Success!" << std::endl;
  105. system("pause");
  106. return 0;
  107. }