#include "WaferRebuild.h" #include #include #include #include #include #include // 自定义比较函数 bool numeric_string_compare(const std::string& s1, const std::string& s2) { // 正则表达式匹配所有数字 std::regex re("(\\d+)"); std::sregex_iterator it1(s1.begin(), s1.end(), re), it2(s2.begin(), s2.end(), re); std::sregex_iterator reg_end; while (it1 != reg_end && it2 != reg_end) { int num1 = std::stoi(it1->str()); int num2 = std::stoi(it2->str()); if (num1 != num2) return num1 < num2; ++it1; ++it2; } // 如果所有数字相等,按字典顺序排序 return s1 < s2; } int read_files_in_dir(const char* p_dir_name, std::vector& file_names) { DIR* p_dir = opendir(p_dir_name); if (p_dir == nullptr) { return -1; } struct dirent* p_file = nullptr; while ((p_file = readdir(p_dir)) != nullptr) { if (strcmp(p_file->d_name, ".") != 0 && strcmp(p_file->d_name, "..") != 0) { //std::string cur_file_name(p_dir_name); //cur_file_name += "/"; //cur_file_name += p_file->d_name; std::string cur_file_name(p_file->d_name); file_names.push_back(cur_file_name); } } std::sort(file_names.begin(), file_names.end(), numeric_string_compare); closedir(p_dir); return 0; } int main() { WaferRebuild wr; nlohmann::json cfg; // A:/code/win_pratice/PmdPratice/cpp/WaferRebuild/x64/WaferRebuilddll/resource std::ifstream is("A:/code/win_pratice/PmdPratice/cpp/WaferRebuilddll_bak/WaferRebuilddll/resource/cfg.json"); is >> cfg; auto a = wr.initParam(cfg); std::vector file_names; const char* image_path = "A:/code/win_pratice/PmdPratice/cpp/WaferRebuilddll_bak/WaferRebuilddll/resource/img_pats/"; if (read_files_in_dir(image_path, file_names) < 0) { std::cout << "read_files_in_dir failed." << std::endl; } std::vector< std::shared_ptr> image_list; for (int f = 0; f < (int)file_names.size(); f++) { std::shared_ptr frame = std::make_shared(2048 * 2448); cv::Mat colorImage; cv::Mat img = cv::imread(std::string(image_path) + "/" + file_names[f],0); if (img.empty()) continue; // cv::cvtColor(img, colorImage, cv::COLOR_GRAY2BGR); // memcpy(frame->data(), colorImage.data, colorImage.rows * colorImage.cols * colorImage.channels()); memcpy(frame->data(), img.data, img.rows * img.cols * img.channels()); image_list.push_back(frame); } std::shared_ptr> cloud_points0 = std::make_shared>(); auto result = wr.calcCloud(image_list, cloud_points0, 0); /* const int numThreads = 4; std::vector threads; // 创建多个 cloud_points 对象 std::shared_ptr> cloud_points0 = std::make_shared>(); std::shared_ptr> cloud_points1 = std::make_shared>(); std::shared_ptr> cloud_points2 = std::make_shared>(); std::shared_ptr> cloud_points3 = std::make_shared>(); // 将 cloud_points 放入 vector std::vector>> cloud_points = { cloud_points0, cloud_points1, cloud_points2, cloud_points3 }; // 创建多个线程并运行 calcCloud 函数,每个线程传递不同的参数组合 for (int i = 0; i < numThreads; ++i) { threads.push_back(std::thread([&wr, &image_list, &cloud_points, i]() { auto result = wr.calcCloud(image_list, cloud_points[i], i); // 可以在这里处理函数返回的结果,如输出或其他逻辑 std::cout << "Thread " << i << " finished with result: " << std::get<0>(result) << ", " << std::get<1>(result) << "\n"; })); } // 等待所有线程完成 for (auto& thread : threads) { thread.join(); } std::map>> cloud_points_list = { {0, cloud_points0}, {1, cloud_points1}, {2, cloud_points2}, {3, cloud_points3} };; std::shared_ptr> cloud_out = std::make_shared>(); auto m_ret = wr.mergeCloud(cloud_points_list, cloud_out); std::cout << "Merge sucess!" << std::endl; std::cout << std::get<0>(m_ret) << ", " << std::get<1>(m_ret) << "\n"; */ std::cout << "Success!" << std::endl; system("pause"); return 0; }