123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "WaferRebuild.h"
- #include <fstream>
- #include <iostream>
- #include <regex>
- #include <opencv2/opencv.hpp>
- #include <dirent.h>
- #include <thread>
-
- // 自定义比较函数
- 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<std::string>& 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<std::string> 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<infra::Image>> image_list;
- for (int f = 0; f < (int)file_names.size(); f++) {
-
- std::shared_ptr<infra::Image> frame = std::make_shared<infra::Image>(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<infra::CloudPoint<double>> cloud_points0 = std::make_shared<infra::CloudPoint<double>>();
- auto result = wr.calcCloud(image_list, cloud_points0, 0);
-
-
- /*
- const int numThreads = 4;
- std::vector<std::thread> threads;
- // 创建多个 cloud_points 对象
- std::shared_ptr<infra::CloudPoint<double>> cloud_points0 = std::make_shared<infra::CloudPoint<double>>();
- std::shared_ptr<infra::CloudPoint<double>> cloud_points1 = std::make_shared<infra::CloudPoint<double>>();
- std::shared_ptr<infra::CloudPoint<double>> cloud_points2 = std::make_shared<infra::CloudPoint<double>>();
- std::shared_ptr<infra::CloudPoint<double>> cloud_points3 = std::make_shared<infra::CloudPoint<double>>();
-
- // 将 cloud_points 放入 vector
- std::vector<std::shared_ptr<infra::CloudPoint<double>>> 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<int32_t, std::shared_ptr<infra::CloudPoint<double>>> cloud_points_list = {
- {0, cloud_points0},
- {1, cloud_points1},
- {2, cloud_points2},
- {3, cloud_points3}
- };;
- std::shared_ptr<infra::CloudPoint<double>> cloud_out = std::make_shared<infra::CloudPoint<double>>();
-
- 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;
- }
|