diff --git a/AlgorithmModule/include/CheckUtil.hpp b/AlgorithmModule/include/CheckUtil.hpp index 237b064..e44536e 100644 --- a/AlgorithmModule/include/CheckUtil.hpp +++ b/AlgorithmModule/include/CheckUtil.hpp @@ -62,6 +62,9 @@ public: //创建目录 static int CreateDir(const std::string &dir); + // 异步存图 + static int SaveImgAsync(const std::string &path, const cv::Mat &img); + // 点的距离是否过小 static bool bcalDis(cv::Point p1, cv::Point p2, int disT); static double calDis(cv::Point p1, cv::Point p2); diff --git a/AlgorithmModule/src/CheckUtil.cpp b/AlgorithmModule/src/CheckUtil.cpp index 87e78de..c0f50ef 100644 --- a/AlgorithmModule/src/CheckUtil.cpp +++ b/AlgorithmModule/src/CheckUtil.cpp @@ -501,6 +501,32 @@ int CheckUtil::CreateDir(const std::string &dir) return ret; } +int CheckUtil::SaveImgAsync(const std::string &path, const cv::Mat &img) +{ + if (path.empty() || img.empty()) + { + return -1; + } + + // 拷贝图像,避免异步线程执行时原图像被释放或修改 + cv::Mat imgCopy = img.clone(); + + // 创建保存路径的父目录 + std::string::size_type pos = path.find_last_of('/'); + if (pos != std::string::npos) + { + std::string parentDir = path.substr(0, pos); + CheckUtil::CreateDir(parentDir); + } + + // 启动后台线程异步存图 + std::thread([path, imgCopy]() { + cv::imwrite(path, imgCopy); + }).detach(); + + return 0; +} + bool CheckUtil::bcalDis(cv::Point p1, cv::Point p2, int disT) { double dis = std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));