#include "Read_Image.h" #include // 转小写 std::string toLower(const std::string &str) { std::string lower = str; std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); return lower; } // 4 相机代码, 顺序与原有实现保持一致 static const std::string g_strCameraCodes[] = {"BCA", "DCA", "BTA", "DTA"}; // 解析文件名中的 相机代码 与 半幅(左右) // 支持两种命名: // 1) {前缀}_{相机}_{0|1}_{后缀} 如 3ABD670013A8BG1_BCA_0_src (0=左半, 1=右半) // 2) {前缀}{相机}{A|B} 如 BCAA / A_BCAB (A=左半, B=右半) // 对应关系: BCA_0 <=> BCAA(左半), BCA_1 <=> BCAB(右半) std::string Extract_Camera_And_Half(const std::string &strStem, std::string &strHalf) { strHalf = ""; // 方式1: {前缀}_{相机}_{0|1}_{后缀} (merg 目录下的命名) // 形如 3ABD670013A8BG1_BCA_0_src.jpg, 其中 0 对应 BCAA, 1 对应 BCAB for (const auto &strCam : g_strCameraCodes) { const std::string strKey = "_" + strCam + "_"; size_t pos = strStem.find(strKey); while (pos != std::string::npos) { const size_t nHalfPos = pos + strKey.size(); if (nHalfPos < strStem.size() && (strStem[nHalfPos] == '0' || strStem[nHalfPos] == '1') && (nHalfPos + 1 >= strStem.size() || strStem[nHalfPos + 1] == '_' || strStem[nHalfPos + 1] == '.')) { strHalf = (strStem[nHalfPos] == '0') ? "A" : "B"; return strCam; } pos = strStem.find(strKey, pos + 1); } } // 方式2: {前缀}{相机}{A|B} (原有的 4 相机命名) for (const auto &strCam : g_strCameraCodes) { const size_t pos = strStem.find(strCam); if (pos != std::string::npos && pos + strCam.size() < strStem.size()) { const char cHalf = strStem[pos + strCam.size()]; if (cHalf == 'A' || cHalf == 'B') { strHalf = std::string(1, cHalf); return strCam; } } } // 兜底: 文件名中只包含相机代码, 无法判定左右 for (const auto &strCam : g_strCameraCodes) { if (strStem.find(strCam) != std::string::npos) { return strCam; } } return ""; } int Extract_Name_Base::Read_Product_Image(std::string strPath) { return 0; } int Extract_Name_Base::Read_Camera_Product_Image(std::string strPath, int userflag, std::string strproduct) { std::vector img_paths; std::cout << strPath << std::endl; cv::glob(strPath, img_paths, true); for (int i = 0; i < img_paths.size(); i++) { string strImagePath = img_paths[i]; Extract_Info(strImagePath, strproduct, userflag); } return 0; } int Extract_Name_Base::Read_Product_Camera_Image(std::string strPath) { return 0; } std::shared_ptr Extract_Name_Base::GetProduct(std::string strProductID) { for (int i = 0; i < m_product_Camera_List.size(); i++) { if (m_product_Camera_List[i]->strProductID == strProductID) { return m_product_Camera_List[i]; } } return std::shared_ptr(); } std::shared_ptr Extract_Name_Base::GetCamera(std::string strProductID, std::string strCamName) { for (int i = 0; i < m_product_Camera_List.size(); i++) { if (m_product_Camera_List[i]->strProductID == strProductID) { for (int j = 0; j < m_product_Camera_List[i]->camera_list.size(); j++) { if (m_product_Camera_List[i]->camera_list[j]->strCamName == strCamName) { return m_product_Camera_List[i]->camera_list[j]; } } } } return std::shared_ptr(); } std::shared_ptr Extract_Name_Base::GetCamera(std::shared_ptr product, std::string strCamName) { for (int i = 0; i < product->camera_list.size(); i++) { if (product->camera_list[i]->strCamName == strCamName) { return product->camera_list[i]; } } return std::shared_ptr(); } // 判断目录部分是否包含某些关键词(不区分大小写) bool Extract_Name_Base::dirPathContains(const fs::path &fullPath, const std::string KeyName) { fs::path parentDir = fullPath.parent_path(); std::string lowKeyName = toLower(KeyName); for (const auto &dir : parentDir) { std::string dirName = toLower(dir.string()); if (dirName == lowKeyName) { return true; } } return false; } Read_Image::Read_Image(/* args */) { } Read_Image::~Read_Image() { } int Read_Image::Read_Image_List(std::string strPath, std::string strproduct) { m_extract.pimage_ReadChannel = pimage_ReadChannel; m_extract.m_product_Camera_List.clear(); int re = m_extract.Read_Image_List(strPath, strproduct); if (re != 0) { return re; } m_product_Camera_List.clear(); m_product_Camera_List.assign(m_extract.m_product_Camera_List.begin(), m_extract.m_product_Camera_List.end()); for (int i = 0; i < m_extract.m_product_Camera_List.size(); i++) { m_extract.m_product_Camera_List[i]->print(); } return 0; } int Read_Image::Read_Image_List(std::string strPath_left, std::string strPath_right, std::string strproduct) { m_extract.pimage_ReadChannel = pimage_ReadChannel; m_extract.m_product_Camera_List.clear(); int re = m_extract.Read_Image_List(strPath_left, strPath_right, strproduct); if (re != 0) { return re; } m_product_Camera_List.clear(); m_product_Camera_List.assign(m_extract.m_product_Camera_List.begin(), m_extract.m_product_Camera_List.end()); for (int i = 0; i < m_extract.m_product_Camera_List.size(); i++) { m_extract.m_product_Camera_List[i]->print(); } return 0; } int Extract_ALL::Extract_Info(std::string strPath, std::string strproduct, int userflag) { std::cout << strPath << std::endl; // 1、获取 产品名称 std::string strProductName = Extract_Product_Name(strPath, userflag); if (strProductName.empty()) { return -1; } std::shared_ptr pProduct = GetProduct(strProductName); if (strproduct != "" && strproduct != strProductName) { return -2; } if (pProduct.get() == nullptr) { printf("pProduct %s is NULL \n", strProductName.c_str()); pProduct = std::make_shared(); pProduct->strProductID = strProductName; m_product_Camera_List.push_back(pProduct); } // 2、获取 相机名称 std::string strCameraName = Extract_Camera_Name(strPath, userflag); if (strCameraName.empty()) { printf("strCameraName is error \n"); return -1; } std::shared_ptr pCamera = GetCamera(pProduct, strCameraName); if (pCamera.get() == nullptr) { printf("pCamera %s is NULL \n", strCameraName.c_str()); pCamera = std::make_shared(); pCamera->strCamName = strCameraName; pProduct->camera_list.push_back(pCamera); } // 3、获取 通道 std::string strChannelName = Extract_Channel_Name(strPath, userflag); if (strChannelName.empty()) { printf("strChannelName is NULL \n"); return -1; } fs::path p = fs::path(strPath); std::string filenameWithoutExt = p.stem().string(); // 记录半幅信息: 左半(A/BCAA/…_0_) / 右半(B/BCAB/…_1_), 用于配对时确定左右图 std::string strHalf; Extract_Camera_And_Half(filenameWithoutExt, strHalf); std::shared_ptr pImage = std::make_shared(); pImage->strProductID = strProductName; pImage->strCamID = strCameraName; pImage->strchannelName = strChannelName; pImage->strName = filenameWithoutExt; pImage->strHalf = strHalf; pImage->strPath = strPath; pCamera->image_list.push_back(pImage); return 0; } std::string Extract_ALL::Extract_Camera_Name(std::string strPath, int userflag) { fs::path p = fs::path(strPath); std::string filename = p.stem(); // 相机名从统一的命名解析中获取: // {相机}{A|B} 形式(BCAA/BCAB) 与 {相机}_{0|1} 形式(BCA_0/BCA_1) 均支持 std::string strHalf; return Extract_Camera_And_Half(filename, strHalf); } std::string Extract_ALL::Extract_Product_Name(std::string strPath, int userflag) { // std::cout << strPath << std::endl; fs::path p = fs::path(strPath); fs::path lastDir = p.parent_path().filename(); std::string name = lastDir.string(); if (name.length()<7) { lastDir = p.parent_path().parent_path().filename(); name = lastDir.string(); } // std::cout << name << std::endl; if (name.empty()) { return ""; } return name; } std::string Extract_ALL::Extract_Channel_Name(std::string strPath, int userflag) { std::string strchannelName = ""; fs::path p = fs::path(strPath); std::string filename = p.stem(); // 新格式: {Prefix}_{Camera}{AB}, 如 A_BCAA, M_DTAB, A_BCAB // 通道名 = 前缀 (A/M), 表示不同打光方式 // 支持4相机: BCAA, BCAB, BTAA, BTAB, DCAA, DCAB, DTAA, DTAB // 兼容 {Camera}_{0|1} 命名(如 BCA_0 / BCA_1), 其 0/1 对应 A/B size_t underscorePos = filename.find('_'); if (underscorePos != std::string::npos) { std::string prefix = filename.substr(0, underscorePos); std::string rest = filename.substr(underscorePos + 1); // 4相机格式 if (rest == "BCAA" || rest == "BCAB" || rest == "BTAA" || rest == "BTAB" || rest == "DCAA" || rest == "DCAB" || rest == "DTAA" || rest == "DTAB" || rest == "BCA_0" || rest == "BCA_1" || rest == "BTA_0" || rest == "BTA_1" || rest == "DCA_0" || rest == "DCA_1" || rest == "DTA_0" || rest == "DTA_1") { return prefix; // "A" or "M" } } // 旧格式兼容: CA_xxx.ytimage, TA_xxx.ytimage // 4相机格式兼容: BCA_xxx, BTA_xxx, DCA_xxx, DTA_xxx if (filename.find("BCAA") != std::string::npos || filename.find("BCAB") != std::string::npos) strchannelName = "BCA"; else if (filename.find("BTAA") != std::string::npos || filename.find("BTAB") != std::string::npos) strchannelName = "BTA"; else if (filename.find("DCAA") != std::string::npos || filename.find("DCAB") != std::string::npos) strchannelName = "DCA"; else if (filename.find("DTAA") != std::string::npos || filename.find("DTAB") != std::string::npos) strchannelName = "DTA"; else if (filename.find("BCA") != std::string::npos) strchannelName = "BCA"; else if (filename.find("BTA") != std::string::npos) strchannelName = "BTA"; else if (filename.find("DCA") != std::string::npos) strchannelName = "DCA"; else if (filename.find("DTA") != std::string::npos) strchannelName = "DTA"; return strchannelName; } int Extract_ALL::Read_Image_List(std::string strPath, std::string strproduct) { std::vector imgExts = {"*.jpg", "*.png", "*.bmp", "*.tif"}; for (const auto &ext : imgExts) { std::string strSearchImgPath = strPath + "/" + ext; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); } return 0; } int Extract_ALL::Read_Image_List(std::string strPath_left, std::string strPath_right, std::string strproduct) { std::vector imgExts = {"*.jpg", "*.png", "*.bmp", "*.tif"}; for (const auto &ext : imgExts) { std::string strSearchImgPath = strPath_left + "/" + ext; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); strSearchImgPath = strPath_right + "/" + ext; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); } return 0; }