From 96c55d89c005bb326a90128eb928d3a7a4f8517b Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Thu, 21 Nov 2024 12:38:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=BB=E5=8F=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E7=9A=84=E5=B7=A5=E5=85=B7=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=8A=A0=E8=BD=BD=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Matrox/template_matching.cpp | 66 +++++++++++++++++++++++--------- src/Matrox/template_matching.h | 1 + src/Matrox/utils.cpp | 45 ++++++++++++++++++++++ src/Matrox/utils.h | 5 +++ tests/test_color_range.cpp | 46 +--------------------- tests/test_template_matching.cpp | 26 ++++--------- 6 files changed, 106 insertions(+), 83 deletions(-) diff --git a/src/Matrox/template_matching.cpp b/src/Matrox/template_matching.cpp index 8d24a69..deba732 100644 --- a/src/Matrox/template_matching.cpp +++ b/src/Matrox/template_matching.cpp @@ -32,10 +32,12 @@ class TemplateMatcher { bool isInitialized; + map param; + public: // Constructor - TemplateMatcher(MIL_ID system, MIL_ID display): - isInitialized(false) {} + TemplateMatcher(MIL_ID system, MIL_ID display, map& param): + isInitialized(false) {this->param = param;} // Destructor: Free MIL objects ~TemplateMatcher() { @@ -75,7 +77,10 @@ class TemplateMatcher { MIL_ID template_temporary; MbufRestore(convert_to_wstring(ModelImgPaths[i]), MilSystem, &template_temporary); MIL_ID template_temporary_uint8 = convert_to_uint8(template_temporary); - MdispSelect(MilDisplay, template_temporary_uint8); + if (this->param["isdisplay"] == 1) + { + MdispSelect(MilDisplay, template_temporary_uint8); + } /* Allocate a graphic list to hold the subpixel annotations to draw. */ MgraAllocList(MilSystem, M_DEFAULT, &GraphicList); /* Associate the graphic list to the display for annotations. */ @@ -83,11 +88,22 @@ class TemplateMatcher { + MmodDefine(MilSearchContext, M_IMAGE, template_temporary_uint8, static_cast(ModelsOffsetX[i]), static_cast(ModelsOffsetY[i]), static_cast(ModelsSizeX[i]), static_cast(ModelsSizeY[i])); + + MgraColor(M_DEFAULT, ModelsDrawColor[i]); + MmodDraw( M_DEFAULT, MilSearchContext, GraphicList, + M_DRAW_BOX+M_DRAW_POSITION, i, M_ORIGINAL); + if (this->param["isdisplay"] == 1) + { + MosGetch(); + } + MbufFree(template_temporary); + MbufFree(template_temporary_uint8); } // 设置一些参数 @@ -111,19 +127,20 @@ class TemplateMatcher { cout << "Templates loaded and preprocessed successfully.\n"; /* Draw boxes and positions in the source image to identify the models. */ - for (int i=0; i to continue.\n\n")); - MosGetch(); - + if (this->param["debug_mode"] == 1) { + MosGetch(); + } } // Search for models in the input image @@ -173,22 +190,30 @@ class TemplateMatcher { MgraColor(M_DEFAULT, ModelsDrawColor[Models[i]]); MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_EDGES + M_DRAW_POSITION, i, M_DEFAULT); + } } else { cout << "No models found.\n"; } + MosPrintf(MIL_TEXT("Press to EXIT.\n\n")); + MosGetch(); + + + MbufFree(input_image_uint8); } + }; -void test_template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const map& params) { +void test_template_matching(const MIL_ID &inputImage, MIL_ID &outputImage, map ¶ms) +{ // Create a TemplateMatcher instance - TemplateMatcher matcher(MilSystem, MilDisplay); + TemplateMatcher matcher(MilSystem, MilDisplay, params); - //TODO: 1加入加载多个模板的功能 + //TODO: 1加入加载多个模板的功能 已 //TODO: 2加入配置文件解析功能,解析后的文件与当前的para map兼容 - // 配置文件当中加入是否显示参数,能调控加载模板的过程是否显示。 - //TODO: 3修改当前的代码使模板匹配不出错 + // 配置文件当中加入是否显示参数,能调控加载模板的过程是否显示。已 + //TODO: 3修改当前的代码使模板匹配不出错 已 //TODO: 4成立模板文件夹,能够加载文件夹下的全部模板并实现检测 //TODO: 5制作标准结构的函数,例如:matcher.findModels(MIL_ID inputImage, MIL_ID output_image, map); //TODO: 6完善相应部分的手册 @@ -196,8 +221,8 @@ void test_template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const // Load template models vector template_paths = {"C:\\Users\\zjc\\Desktop\\template1.png", - "C:\\Users\\zjc\\Desktop\\template1.png", - "C:\\Users\\zjc\\Desktop\\template1.png", + "C:\\Users\\zjc\\Desktop\\template2.png", + "C:\\Users\\zjc\\Desktop\\template3.png", }; vector offsetX = {0, 20, 30}; vector offsetY = {0, 20, 30}; @@ -209,7 +234,10 @@ void test_template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const // Find models matcher.findModels(inputImage); + // Free resources - MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, MilDisplay); + // } + + diff --git a/src/Matrox/template_matching.h b/src/Matrox/template_matching.h index 0e0ec60..6225c96 100644 --- a/src/Matrox/template_matching.h +++ b/src/Matrox/template_matching.h @@ -7,4 +7,5 @@ void pre_process(const MIL_ID& inputImage, MIL_ID& outputImageSuspect, const std::map& params); void template_matching(const MIL_ID& inputImageSuspect, MIL_ID& outputImage, const std::map& params); +void test_template_matching(const MIL_ID &inputImage, MIL_ID &outputImage, std::map ¶ms) ; #endif //TEMPLATE_MATCHING_H diff --git a/src/Matrox/utils.cpp b/src/Matrox/utils.cpp index 6ab2987..31b3f0e 100644 --- a/src/Matrox/utils.cpp +++ b/src/Matrox/utils.cpp @@ -74,4 +74,49 @@ wstring convert_to_wstring(const string& str) { return wstring(str.begin(), str.end()); } + + +void read_params_from_file(const std::string& filename, std::map& params) { + std::ifstream infile(filename); + if (!infile) { + std::cerr << "无法打开文件: " << filename << std::endl; + return; + } + + std::string line; + while (std::getline(infile, line)) { + // 去除行首和行尾的空白字符 + line.erase(0, line.find_first_not_of(" \t\r\n")); + line.erase(line.find_last_not_of(" \t\r\n") + 1); + + // 跳过空行和注释行 + if (line.empty() || line[0] == '#') + continue; + + // 查找等号的位置 + size_t pos = line.find('='); + if (pos == std::string::npos) + continue; // 如果没有等号,跳过该行 + + // 分割键和值,并去除空白字符 + std::string key = line.substr(0, pos); + std::string value_str = line.substr(pos + 1); + key.erase(0, key.find_first_not_of(" \t")); + key.erase(key.find_last_not_of(" \t") + 1); + value_str.erase(0, value_str.find_first_not_of(" \t")); + value_str.erase(value_str.find_last_not_of(" \t") + 1); + + // 将字符串转换为整数 + int value; + std::istringstream iss(value_str); + if (!(iss >> value)) { + std::cerr << "键 " << key << " 的值无效: " << value_str << std::endl; + continue; + } + + // 将键值对添加到参数映射中 + params[key] = value; + } +} + // 图片转换函数,输入4096*1024*3的图片,输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1 diff --git a/src/Matrox/utils.h b/src/Matrox/utils.h index 09fb061..08f505e 100644 --- a/src/Matrox/utils.h +++ b/src/Matrox/utils.h @@ -9,6 +9,10 @@ #include #include #include +#include +#include +#include +#include // 声明全局变量(注意:这里只是声明,不是定义) extern __int64 MilApplication; @@ -33,5 +37,6 @@ std::vector opencvLabToPsLab(const std::vector& lab_cv); MIL_ID convert_to_uint8(MIL_ID input_img); std::wstring convert_to_wstring(const std::string& str); +void read_params_from_file(const std::string& filename, std::map& params) ; #endif //UTILS_H diff --git a/tests/test_color_range.cpp b/tests/test_color_range.cpp index dd0c6af..c57a590 100644 --- a/tests/test_color_range.cpp +++ b/tests/test_color_range.cpp @@ -23,51 +23,7 @@ int main() // Define color ranges std::map params; - params["green_L_min"] = 27; - params["green_L_max"] = 49; - params["green_a_min"] = -27; - params["green_a_max"] = -8; - params["green_b_min"] = 2; - params["green_b_max"] = 12; - - params["blue_L_min"] = 18; - params["blue_L_max"] = 26; - params["blue_a_min"] = 2; - params["blue_a_max"] = 17; - params["blue_b_min"] = -33; - params["blue_b_max"] = -23; - - params["orange_L_min"] = 65; - params["orange_L_max"] = 75; - params["orange_a_min"] = 7; - params["orange_a_max"] = 14; - params["orange_b_min"] = 32; - params["orange_b_max"] = 46; - - params["black_L_min"] = 0; - params["black_L_max"] = 8; - params["black_a_min"] = -1; - params["black_a_max"] = 5; - params["black_b_min"] = -2; - params["black_b_max"] = 6; - - params["red_L_min"] = 28; - params["red_L_max"] = 38; - params["red_a_min"] = 15; - params["red_a_max"] = 25; - params["red_b_min"] = -95; - params["red_b_max"] = 26; - - params["purple_L_min"] = 67; - params["purple_L_max"] = 77; - params["purple_a_min"] = 3; - params["purple_a_max"] = 13; - params["purple_b_min"] = -20; - params["purple_b_max"] = -5; - params["lab_denoising"] = 1; - - params["saturation_threshold"] = 150; - params["saturation_denoising"] = 1; + read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\color_range_config.txt", params); // Initialize combined result MIL_ID detection_result = M_NULL; diff --git a/tests/test_template_matching.cpp b/tests/test_template_matching.cpp index 792c152..6c6b8b3 100644 --- a/tests/test_template_matching.cpp +++ b/tests/test_template_matching.cpp @@ -17,23 +17,9 @@ MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL; int main() { using namespace std; - map params; - params["cotton_L_min"] = 40; - params["cotton_L_max"] = 90; - params["cotton_a_min"] = -5; - params["cotton_a_max"] = 6; - params["cotton_b_min"] = 0; - params["cotton_b_max"] = 30; - - params["background_L_min"] = 95; - params["background_L_max"] = 100; - params["background_a_min"] = -3; - params["background_a_max"] = 3; - params["background_b_min"] = -3; - params["background_b_max"] = 3; - - params["cotton_denoising"] = 1; + std::map params; + read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\config.txt", params); // Initialize MIL application MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); @@ -46,7 +32,8 @@ int main() { MIL_ID detection_result = M_NULL; // Measure execution time - measure_execution_time([&]() { + measure_execution_time([&]() + { pre_process(MilImage, detection_result, params); test_template_matching(detection_result, detection_result, params); }); @@ -56,9 +43,10 @@ int main() { std::cout << "所有颜色检测已完成并合并。按 退出。" << std::endl; getchar(); - // Free resources + + MbufFree(detection_result); MbufFree(MilImage); - MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); + return 0; } \ No newline at end of file