diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e3cb76..df8239d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,39 +37,45 @@ include_directories(E:/QTexamble/matrox/Include) link_directories(E:/QTexamble/matrox/LIB) file(GLOB MIL_LIBS E:/QTexamble/matrox/LIB/*.lib) -# 添加可执行文件 cotton_color -add_executable(cotton_color cotton_color.cpp) -# 链接 OpenCV 和 Qt 库 -target_link_libraries(cotton_color Qt6::Widgets ${OpenCV_LIBS} comdlg32) -# 添加可执行文件 cotton_color -add_executable(cotton_range Matrox/color_range.cpp - Matrox/color_range.h Matrox/utils.h Matrox/utils.cpp) -# 链接 OpenCV 和 Qt 库 -target_link_libraries(cotton_range Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) +# 添加子目录 +add_subdirectory(src) +add_subdirectory(tests) -# 添加可执行文件 cotton_color2 -add_executable(cotton_color2 cotton_color2.cpp) -# 链接 OpenCV 和 Qt 库 -target_link_libraries(cotton_color2 Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) - -add_executable(color_matching Matrox/template_matching.cpp - Matrox/utils.cpp - Matrox/utils.h) -target_link_libraries(color_matching Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) - - -add_executable(ui Matrox/ui.cpp) -target_link_libraries(ui Qt6::Widgets) - - -add_executable(onnx Matrox/onnx_running.cpp) -target_link_libraries(onnx Qt6::Widgets ${MIL_LIBS}) - -add_executable(opencv_onnx opencv_onnx.cpp) -# 链接 OpenCV 和 Qt 库 -target_link_libraries(opencv_onnx Qt6::Widgets ${OpenCV_LIBS} comdlg32) - - -add_executable(create_mask Matrox/mask.cpp) -target_link_libraries(create_mask Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) \ No newline at end of file +## 添加可执行文件 cotton_color +#add_executable(cotton_color cotton_color.cpp) +## 链接 OpenCV 和 Qt 库 +#target_link_libraries(cotton_color Qt6::Widgets ${OpenCV_LIBS} comdlg32) +# +## 添加可执行文件 cotton_color +#add_executable(cotton_range src/Matrox/color_range.cpp +# src/Matrox/color_range.h src/Matrox/utils.h src/Matrox/utils.cpp) +## 链接 OpenCV 和 Qt 库 +#target_link_libraries(cotton_range Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) +# +## 添加可执行文件 cotton_color2 +#add_executable(cotton_color2 cotton_color2.cpp) +## 链接 OpenCV 和 Qt 库 +#target_link_libraries(cotton_color2 Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) +# +#add_executable(template_matching src/Matrox/template_matching.cpp +# src/Matrox/color_range.cpp src/Matrox/color_range.h +# src/Matrox/utils.cpp src/Matrox/utils.h) +#target_link_libraries(template_matching Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) +# +# +#add_executable(ui src/Matrox/ui.cpp) +#target_link_libraries(ui Qt6::Widgets) +# +# +#add_executable(onnx src/Matrox/onnx_running.cpp) +#target_link_libraries(onnx Qt6::Widgets ${MIL_LIBS}) +# +#add_executable(opencv_onnx opencv_onnx.cpp) +## 链接 OpenCV 和 Qt 库 +#target_link_libraries(opencv_onnx Qt6::Widgets ${OpenCV_LIBS} comdlg32) +# +# +#add_executable(create_mask src/Matrox/mask.cpp) +#target_link_libraries(create_mask Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) +#add_executable(test_color_range tests/test_color_range.cpp) diff --git a/Matrox/template_matching.cpp b/Matrox/template_matching.cpp deleted file mode 100644 index ededb56..0000000 --- a/Matrox/template_matching.cpp +++ /dev/null @@ -1,222 +0,0 @@ - - -#include -//#include // 添加此行 -#include - -/* Example functions declarations. */ -void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay); - -/*****************************************************************************/ -/* Main. -******************************************************************************/ -int MosMain(void) -{ - MIL_ID MilApplication, /* Application identifier. */ - MilSystem, /* System Identifier. */ - MilDisplay; /* Display identifier. */ - - /* Allocate defaults. */ - MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); - - /* Run single model example. */ - SingleModelExample(MilSystem, MilDisplay); - - /* Free defaults. */ - MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); - - return 0; -} - -/*****************************************************************************/ -/* Single model example. */ - -/* Source MIL image file specifications. */ -#define SINGLE_MODEL_IMAGE MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai2.png") // 替换为您的模板RGB图像文件路径 - -/* Target MIL image file specifications. */ -#define SINGLE_MODEL_TARGET_IMAGE MIL_TEXT ("C:\\Users\\zjc\\Desktop\\diguandai.png") // 替换为您的待检测RGB图像文件路径 - -/* Search speed: M_VERY_HIGH for faster search, M_MEDIUM for precision and robustness. */ -#define SINGLE_MODEL_SEARCH_SPEED M_LOW - -/* Model specifications. */ -#define MODEL_OFFSETX 3200L // 根据您的模板图像调整 -#define MODEL_OFFSETY 550L // 根据您的模板图像调整 -#define MODEL_SIZEX 200L // 根据您的模板图像调整 -#define MODEL_SIZEY 200L // 根据您的模板图像调整 -#define MODEL_MAX_OCCURRENCES 6L - -void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay) -{ - clock_t start_time = clock(); - - MIL_ID MilColorImage, /* 彩色图像缓冲区标识符。*/ - MilImage, /* 灰度图像缓冲区标识符。*/ - GraphicList; /* 图形列表标识符。*/ - MIL_ID MilSearchContext, /* 搜索上下文。*/ - MilResult; /* 结果标识符。*/ - MIL_DOUBLE ModelDrawColor = M_COLOR_RED; /* 模板绘制颜色。*/ - MIL_INT Model[MODEL_MAX_OCCURRENCES], /* 模板索引。*/ - NumResults = 0L; /* 找到的结果数量。*/ - MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES], /* 模板匹配得分。*/ - XPosition[MODEL_MAX_OCCURRENCES], /* 模板X位置。*/ - YPosition[MODEL_MAX_OCCURRENCES], /* 模板Y位置。*/ - Angle[MODEL_MAX_OCCURRENCES], /* 模板角度。*/ - Scale[MODEL_MAX_OCCURRENCES], /* 模板缩放。*/ - Time = 0.0; /* 计时变量。*/ - int i; /* 循环变量。*/ - - - /* 加载RGB模板图像。 */ - MbufRestore(SINGLE_MODEL_IMAGE, MilSystem, &MilColorImage); - - /* 获取图像尺寸。 */ - MIL_INT Width = MbufInquire(MilColorImage, M_SIZE_X, M_NULL); - MIL_INT Height = MbufInquire(MilColorImage, M_SIZE_Y, M_NULL); - - /* 分配灰度图像缓冲区。 */ - MbufAlloc2d(MilSystem, Width, Height, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilImage); - - /* 将RGB图像转换为灰度图像。 */ - MimConvert(MilColorImage, MilImage, M_RGB_TO_L); - - /* 选择灰度图像进行显示。 */ - MdispSelect(MilDisplay, MilImage); - - /* 释放彩色图像缓冲区。 */ - MbufFree(MilColorImage); - - /* 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. */ - MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList); - - /* Allocate a Geometric Model Finder context. */ - MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext); - - /* Allocate a result buffer. */ - MmodAllocResult(MilSystem, M_DEFAULT, &MilResult); - - /* Define the model. */ - MmodDefine(MilSearchContext, M_IMAGE, MilImage, - MODEL_OFFSETX, MODEL_OFFSETY, MODEL_SIZEX, MODEL_SIZEY); - - /* Set the search speed. */ - MmodControl(MilSearchContext, M_CONTEXT, M_SPEED, SINGLE_MODEL_SEARCH_SPEED); - - /* Preprocess the search context. */ - MmodPreprocess(MilSearchContext, M_DEFAULT); - - /* Draw box and position it in the source image to show the model. */ - MgraColor(M_DEFAULT, ModelDrawColor); - MmodDraw(M_DEFAULT, MilSearchContext, GraphicList, - M_DRAW_BOX + M_DRAW_POSITION, 0, M_ORIGINAL); - clock_t end_time = clock(); - std::cout << "The run time is: " << (double)(end_time - start_time) / CLOCKS_PER_SEC << "s"; - /* Pause to show the model. */ - MosPrintf(MIL_TEXT("\nGEOMETRIC MODEL FINDER:\n")); - MosPrintf(MIL_TEXT("-----------------------\n\n")); - MosPrintf(MIL_TEXT("A model context was defined with ")); - MosPrintf(MIL_TEXT("the model in the displayed image.\n")); - MosPrintf(MIL_TEXT("Press to continue.\n\n")); - MosGetch(); - - /* Clear annotations. */ - MgraClear(M_DEFAULT, GraphicList); - - /* 加载RGB待检测图像。 */ - MbufRestore(SINGLE_MODEL_TARGET_IMAGE, MilSystem, &MilColorImage); - - /* 确保待检测图像的尺寸与模板图像一致。 */ - MIL_INT TargetWidth = MbufInquire(MilColorImage, M_SIZE_X, M_NULL); - MIL_INT TargetHeight = MbufInquire(MilColorImage, M_SIZE_Y, M_NULL); - - /* 如果尺寸不同,需要重新分配灰度图像缓冲区。 */ - if (TargetWidth != Width || TargetHeight != Height) - { - /* 释放之前的灰度图像缓冲区。 */ - MbufFree(MilImage); - - /* 分配新的灰度图像缓冲区。 */ - MbufAlloc2d(MilSystem, TargetWidth, TargetHeight, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilImage); - - /* 更新宽度和高度。 */ - Width = TargetWidth; - Height = TargetHeight; - } - - /* 将RGB待检测图像转换为灰度图像。 */ - MimConvert(MilColorImage, MilImage, M_RGB_TO_L); - - /* 释放彩色图像缓冲区。 */ - MbufFree(MilColorImage); - - /* 显示灰度待检测图像。 */ - MdispSelect(MilDisplay, MilImage); - - /* Dummy first call for bench measure purpose only (bench stabilization, - cache effect, etc...). This first call is NOT required by the application. */ - MmodFind(MilSearchContext, MilImage, MilResult); - - /* Reset the timer. */ - MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL); - - /* Find the model. */ - MmodFind(MilSearchContext, MilImage, MilResult); - - /* Read the find time. */ - MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time); - - /* Get the number of models found. */ - MmodGetResult(MilResult, M_DEFAULT, M_NUMBER + M_TYPE_MIL_INT, &NumResults); - - /* If a model was found above the acceptance threshold. */ - if ((NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES)) - { - /* Get the results of the single model. */ - MmodGetResult(MilResult, M_DEFAULT, M_INDEX + M_TYPE_MIL_INT, Model); - MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition); - MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition); - MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle); - MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale); - MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score); - - /* Print the results for each model found. */ - MosPrintf(MIL_TEXT("The model was found in the target image:\n\n")); - MosPrintf(MIL_TEXT("Result Model X Position Y Position ") - MIL_TEXT("Angle Scale Score\n\n")); - for (i = 0; i < NumResults; i++) - { - MosPrintf(MIL_TEXT("%-9d%-8d%-13.2f%-13.2f%-8.2f%-8.2f%-5.2f%%\n"), - i, (int)Model[i], XPosition[i], YPosition[i], - Angle[i], Scale[i], Score[i]); - } - MosPrintf(MIL_TEXT("\nThe search time is %.1f ms\n\n"), Time * 1000.0); - - /* Draw edges, position and box over the occurrences that were found. */ - for (i = 0; i < NumResults; i++) - { - MgraColor(M_DEFAULT, ModelDrawColor); - MmodDraw(M_DEFAULT, MilResult, GraphicList, - M_DRAW_EDGES + M_DRAW_BOX + M_DRAW_POSITION, i, M_DEFAULT); - } - } - else - { - MosPrintf(MIL_TEXT("The model was not found or the number of models ") - MIL_TEXT("found is greater than\n")); - MosPrintf(MIL_TEXT("the specified maximum number of occurrence !\n\n")); - } - - /* Wait for a key to be pressed. */ - MosPrintf(MIL_TEXT("Press to continue.\n\n")); - MosGetch(); - - /* Free MIL objects. */ - MgraFree(GraphicList); - MbufFree(MilImage); - MmodFree(MilSearchContext); - MmodFree(MilResult); -} \ No newline at end of file diff --git a/Matrox/template_matching.h b/Matrox/template_matching.h deleted file mode 100644 index bcbca12..0000000 --- a/Matrox/template_matching.h +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by zjc on 24-11-12. -// - -#ifndef TEMPLATE_MATCHING_H -#define TEMPLATE_MATCHING_H - -#endif //TEMPLATE_MATCHING_H diff --git a/Matrox/utils.cpp b/Matrox/utils.cpp deleted file mode 100644 index 6db5430..0000000 --- a/Matrox/utils.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by zjc on 24-11-18. -// - -#include "utils.h" - - - -// 图片转换函数,输入4096*1024*3的图片,输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..8859a18 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,12 @@ +# 定义 Matrox 模块的库 +add_library(Matrox + Matrox/color_range.cpp + Matrox/utils.cpp + Matrox/template_matching.cpp +) + +# 头文件路径 +target_include_directories(Matrox PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# 链接依赖库 +target_link_libraries(Matrox PUBLIC Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) diff --git a/Matrox/README.md b/src/Matrox/README.md similarity index 83% rename from Matrox/README.md rename to src/Matrox/README.md index d8335a5..36a4653 100644 --- a/Matrox/README.md +++ b/src/Matrox/README.md @@ -72,3 +72,15 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma | lab_denoising = 1 | | | ![diguandai](./README.assets/diguandai.png) | | lab_denoising = 2 | | | | + +这些是经过实验后的推荐参数: + +| | L_min | L_max | a_min | a_max | b_min | b_max | +| ------ | ----- | ----- | ----- | ----- | ----- | ----- | +| green | 27 | 49 | -27 | -8 | 2 | 12 | +| blue | 18 | 26 | 2 | 17 | -33 | -23 | +| orange | 65 | 75 | 7 | 14 | 32 | 46 | +| black | 0 | 8 | -1 | 5 | -2 | 6 | +| red | 28 | 38 | 15 | 25 | -95 | 26 | +| purple | 67 | 77 | 3 | 13 | -20 | -5 | + diff --git a/Matrox/color_range.cpp b/src/Matrox/color_range.cpp similarity index 70% rename from Matrox/color_range.cpp rename to src/Matrox/color_range.cpp index 3295e8b..10674cd 100644 --- a/Matrox/color_range.cpp +++ b/src/Matrox/color_range.cpp @@ -4,15 +4,10 @@ #include #include "utils.h" -#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton_image\\174.bmp") -#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai.png") - - -// Global variables -MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL; // Optimized LabProcess function -void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map& params) +void lab_process_raw(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map& params, + const std::vector& color_vector) { MIL_ID MilLabImage = M_NULL, MilLChannel = M_NULL, MilAChannel = M_NULL, MilBChannel = M_NULL; MIL_ID lab_result=M_NULL; @@ -56,11 +51,11 @@ void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::ma MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryB); MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &MilResultLab); MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &lab_result); - const std::vector colors = {"green", "blue", "orange", "black", "red", "purple"}; + // Iterate over colors // 遍历颜色 - for (const auto& color : colors) { + for (const auto& color : color_vector) { // 构建参数键 std::string L_min_key = color + "_L_min"; std::string L_max_key = color + "_L_max"; @@ -76,6 +71,18 @@ void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::ma int a_max = params.at(a_max_key); int b_min = params.at(b_min_key); int b_max = params.at(b_max_key); + std::vector lab_min_ps = {L_min, a_min, b_min}; + std::vector lab_max_ps = {L_max, a_max, b_max}; + + std::vector lab_min_cv = psLabToOpenCVLab(lab_min_ps); + std::vector lab_max_cv = psLabToOpenCVLab(lab_max_ps); + + L_min = lab_min_cv[0]; + L_max = lab_max_cv[0]; + a_min = lab_min_cv[1]; + a_max = lab_max_cv[1]; + b_min = lab_min_cv[2]; + b_max = lab_max_cv[2]; // 对每个通道进行二值化 MimBinarize(MilLChannel, MilBinaryL, M_IN_RANGE, L_min, L_max); @@ -107,6 +114,13 @@ void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::ma MbufFree(lab_result); } + +void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map& params) { + const std::vector colors = {"green", "blue", "orange", "black", "red", "purple"}; + lab_process_raw(inputImage, outputImageLab, params, colors); +} + + void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::map& params) { MIL_ID MilHSVImage = M_NULL, MilHChannel = M_NULL, MilSChannel = M_NULL, MilVChannel = M_NULL; @@ -142,13 +156,13 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma // 分配输出图像缓冲区 MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL), - MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED, + MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &hsv_result); MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL), - MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED, + MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &hsv_denoising); MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL), - MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED, + MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &outputImageHSV); // 对 S 通道进行阈值分割 @@ -167,6 +181,7 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma MbufFree(hsv_denoising); } + void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map& params) { MIL_ID output_hsv=M_NULL, output_lab=M_NULL; @@ -184,81 +199,4 @@ void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::m MbufFree(output_hsv); } -int main() -{ - // Initialize MIL application - MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, - M_NULL); - // Load input image - MIL_ID MilImage = M_NULL; - MbufRestore(IMAGE_PATH, MilSystem, &MilImage); - - // Define color ranges - std::map params; - params["green_L_min"] = 68; - params["green_L_max"] = 125; - params["green_a_min"] = 101; - params["green_a_max"] = 120; - params["green_b_min"] = 130; - params["green_b_max"] = 140; - - params["blue_L_min"] = 45; - params["blue_L_max"] = 66; - params["blue_a_min"] = 130; - params["blue_a_max"] = 145; - params["blue_b_min"] = 95; - params["blue_b_max"] = 105; - - params["orange_L_min"] = 166; - params["orange_L_max"] = 191; - params["orange_a_min"] = 135; - params["orange_a_max"] = 142; - params["orange_b_min"] = 160; - params["orange_b_max"] = 174; - - params["black_L_min"] = 0; - params["black_L_max"] = 21; - params["black_a_min"] = 127; - params["black_a_max"] = 133; - params["black_b_min"] = 126; - params["black_b_max"] = 134; - - params["red_L_min"] = 71; - params["red_L_max"] = 97; - params["red_a_min"] = 143; - params["red_a_max"] = 153; - params["red_b_min"] = 33; - params["red_b_max"] = 154; - - params["purple_L_min"] = 171; - params["purple_L_max"] = 197; - params["purple_a_min"] = 131; - params["purple_a_max"] = 141; - params["purple_b_min"] = 108; - params["purple_b_max"] = 123; - params["lab_denoising"] = 1; - - params["saturation_threshold"] = 150; - params["saturation_denoising"] = 1; - - // Initialize combined result - MIL_ID detection_result = M_NULL; - - // Measure execution time - measure_execution_time([&]() { - high_sat_detect(MilImage, detection_result, params); - }); - MbufSave(SAVE_PATH, detection_result); - // Display result - - std::cout << "所有颜色检测已完成并合并。按 退出。" << std::endl; - getchar(); - - // Free resources - MbufFree(detection_result); - MbufFree(MilImage); - MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); - - return 0; -} diff --git a/Matrox/color_range.h b/src/Matrox/color_range.h similarity index 84% rename from Matrox/color_range.h rename to src/Matrox/color_range.h index 0c4cfcc..d959163 100644 --- a/Matrox/color_range.h +++ b/src/Matrox/color_range.h @@ -60,7 +60,10 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma // params["purple_b_max"] = 123; // +void lab_process_raw(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map& params, + const std::vector& color_vector); +// 新增了可自定义的色彩矩阵 - +void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map& params); #endif //COLOR_RANGE_H diff --git a/Matrox/mask.cpp b/src/Matrox/mask.cpp similarity index 100% rename from Matrox/mask.cpp rename to src/Matrox/mask.cpp diff --git a/Matrox/onnx_running.cpp b/src/Matrox/onnx_running.cpp similarity index 82% rename from Matrox/onnx_running.cpp rename to src/Matrox/onnx_running.cpp index c5e6892..bbcee7a 100644 --- a/Matrox/onnx_running.cpp +++ b/src/Matrox/onnx_running.cpp @@ -13,9 +13,9 @@ #include // Path definitions. -#define EXAMPLE_ONNX_MODEL_PATH MIL_TEXT("C:\\Users\\zjc\\source\\repos\\cotton_color\\Matrox\\models\\2024_11_12_imgsz640_batch1.onnx") -#define TARGET_IMAGE_DIR_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.mim") -#define IMAGE_FILE MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.bmp") +#define EXAMPLE_ONNX_MODEL_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\WeChat Files\\wxid_ipl8u0ctajtn22\\FileStorage\\File\\2024-11\\2024_11_12_imgsz640_batch1(1).onnx") +#define TARGET_IMAGE_DIR_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo4.mim") + int MosMain(void) { @@ -31,14 +31,6 @@ int MosMain(void) MsysAlloc(M_DEFAULT, M_SYSTEM_DEFAULT, M_DEFAULT, M_DEFAULT, &MilSystem); MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay); - MIL_UNIQUE_BUF_ID dimo2; - MbufImport(IMAGE_FILE, M_DEFAULT, M_RESTORE+M_NO_GRAB+M_NO_COMPRESS, MilSystem, &dimo2); - //MIL_UNIQUE_BUF_ID MimArithdestination = MbufClone(dimo2, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_UNIQUE_ID); - MIL_UNIQUE_BUF_ID MimArithDestination = MbufAllocColor(MilSystem, 3, 640, 640, 32 + M_FLOAT, M_IMAGE + M_PROC, M_UNIQUE_ID); - // Post-Alloc Block for MimArith's destination - MbufClear(MimArithDestination, M_COLOR_BLACK); - - MimArith(dimo2, 255.0, MimArithDestination, M_DIV_CONST); // Load the image into memory. if (MbufRestore(TARGET_IMAGE_DIR_PATH, MilSystem, &MilImage) != M_NULL) @@ -51,7 +43,6 @@ int MosMain(void) return 1; // Exit if the image loading failed } - MdispSelect(MilDisplay, MimArithDestination); // MbufInquire(MilImage, , NULL); @@ -70,7 +61,7 @@ int MosMain(void) // Perform object detection on the image using MclassPredict. - MclassPredict(DetectCtx, MimArithDestination, DetectRes, M_DEFAULT); + MclassPredict(DetectCtx, MilImage, DetectRes, M_DEFAULT); MosPrintf(MIL_TEXT("Object detection completed.\n")); diff --git a/Matrox/onnx_running.h b/src/Matrox/onnx_running.h similarity index 100% rename from Matrox/onnx_running.h rename to src/Matrox/onnx_running.h diff --git a/src/Matrox/template_matching.cpp b/src/Matrox/template_matching.cpp new file mode 100644 index 0000000..850c94c --- /dev/null +++ b/src/Matrox/template_matching.cpp @@ -0,0 +1,219 @@ +#include +#include +#include +#include "color_range.h" +#include "utils.h" + + +void pre_process(const MIL_ID& inputImage, MIL_ID& outputImageSuspect, const std::map& params) { + // 异色检测, 检测出不同与棉花颜色的物体作为模板匹配的对象 + const std::vector colors = {"cotton", "background"}; + std::map param_temp = params; + param_temp["lab_denoising"] = param_temp["cotton_denoising"]; + param_temp["saturation_threshold"] = param_temp["cotton_saturation_threshold"]; + param_temp["saturation_denoising"] = param_temp["cotton_saturation_denoising"]; + lab_process_raw(inputImage, outputImageSuspect, param_temp, colors); + MimArith(outputImageSuspect, M_NULL, outputImageSuspect, M_NOT); +} + +/* Number of models. */ +#define NUMBER_OF_MODELS 3L +#define MODELS_MAX_OCCURRENCES 16L + +/* Model 1 specifications. */ +#define MODEL0_OFFSETX 34L +#define MODEL0_OFFSETY 93L +#define MODEL0_SIZEX 214L +#define MODEL0_SIZEY 76L +#define MODEL0_DRAW_COLOR M_COLOR_RED + +/* Model 2 specifications. */ +#define MODEL1_OFFSETX 73L +#define MODEL1_OFFSETY 232L +#define MODEL1_SIZEX 150L +#define MODEL1_SIZEY 154L +#define MODEL1_REFERENCEX 23L +#define MODEL1_REFERENCEY 127L +#define MODEL1_DRAW_COLOR M_COLOR_GREEN + +/* Model 3 specifications. */ +#define MODEL2_OFFSETX 308L +#define MODEL2_OFFSETY 39L +#define MODEL2_SIZEX 175L +#define MODEL2_SIZEY 357L +#define MODEL2_REFERENCEX 62L +#define MODEL2_REFERENCEY 150L +#define MODEL2_DRAW_COLOR M_COLOR_BLUE +/* Search speed: M_VERY_HIGH for faster search, M_MEDIUM for precision and robustness.*/ +#define MULTI_MODELS_SEARCH_SPEED M_VERY_HIGH + +/* Models array specifications. */ +#define MODELS_ARRAY_SIZE 3L +#define MODELS_OFFSETX {MODEL0_OFFSETX, MODEL1_OFFSETX, MODEL2_OFFSETX} +#define MODELS_OFFSETY {MODEL0_OFFSETY, MODEL1_OFFSETY, MODEL2_OFFSETY} +#define MODELS_SIZEX {MODEL0_SIZEX, MODEL1_SIZEX, MODEL2_SIZEX} +#define MODELS_SIZEY {MODEL0_SIZEY, MODEL1_SIZEY, MODEL2_SIZEY} +#define MODELS_DRAW_COLOR {MODEL0_DRAW_COLOR, MODEL1_DRAW_COLOR, MODEL2_DRAW_COLOR} + + +void template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map& params) { + MIL_ID MilImage, /* Image buffer identifier. */ + GraphicList; /* Graphic list identifier. */ + MIL_ID MilSearchContext, /* Search context */ + MilResult; /* Result identifier. */ + MIL_INT Models[MODELS_MAX_OCCURRENCES], /* Model indices. */ + ModelsOffsetX[MODELS_ARRAY_SIZE] = MODELS_OFFSETX,/* Model X offsets array. */ + ModelsOffsetY[MODELS_ARRAY_SIZE] = MODELS_OFFSETY,/* Model Y offsets array. */ + ModelsSizeX[MODELS_ARRAY_SIZE] = MODELS_SIZEX, /* Model X sizes array. */ + ModelsSizeY[MODELS_ARRAY_SIZE] = MODELS_SIZEY; /* Model Y sizes array. */ + MIL_DOUBLE ModelsDrawColor[MODELS_ARRAY_SIZE]=MODELS_DRAW_COLOR; /* Model drawing colors.*/ + MIL_INT NumResults = 0L; /* Number of results found. */ + MIL_DOUBLE Score[MODELS_MAX_OCCURRENCES], /* Model correlation scores.*/ + XPosition[MODELS_MAX_OCCURRENCES], /* Model X positions. */ + YPosition[MODELS_MAX_OCCURRENCES], /* Model Y positions. */ + Angle[MODELS_MAX_OCCURRENCES], /* Model occurrence angles. */ + Scale[MODELS_MAX_OCCURRENCES], /* Model occurrence scales. */ + Time = 0.0; /* Time variable. */ + int i; /* Loop variable */ + MIL_ID inputImageSuspect = convert_to_uint8(inputImage); + /* Restore the model image and display it. */ + MdispSelect(MilDisplay, inputImageSuspect); + + // 用于显示的部分 + /* 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. */ + MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList); + + + /* Allocate a geometric model finder. */ + MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext); + // 取出一个buffer用来存储结果 + /* Allocate a result buffer. */ + MmodAllocResult(MilSystem, M_DEFAULT, &MilResult); + + /* Define the models. */ + for (i=0; i1) + /* Change the reference point of the second model. */ + MmodControl(MilSearchContext, 1, M_REFERENCE_X, MODEL1_REFERENCEX); + MmodControl(MilSearchContext, 1, M_REFERENCE_Y, MODEL1_REFERENCEY); + + #if (NUMBER_OF_MODELS>2) + /* Change the reference point of the third model. */ + MmodControl(MilSearchContext, 2, M_REFERENCE_X, MODEL2_REFERENCEX); + MmodControl(MilSearchContext, 2, M_REFERENCE_Y, MODEL2_REFERENCEY); + #endif + #endif + + /* Preprocess the search context. */ + MmodPreprocess(MilSearchContext, M_DEFAULT); + + /* Draw boxes and positions in the source image to identify the models. */ + for (i=0; i to continue.\n\n")); + MosGetch(); + + /* Clear annotations. */ + MgraClear(M_DEFAULT, GraphicList); + + + /* Dummy first call for bench measure purpose only (bench stabilization, + cache effect, etc...). This first call is NOT required by the application. */ + // MmodFind(MilSearchContext, inputImageSuspect, MilResult); + /* Reset the timer. */ + MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL); + + /* Find the models. */ + MmodFind(MilSearchContext, inputImageSuspect, MilResult); + + /* Read the find time. */ + MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time); + + /* Get the number of models found. */ + MmodGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults); + + /* If the models were found above the acceptance threshold. */ + if( (NumResults >= 1) && (NumResults <= MODELS_MAX_OCCURRENCES) ) + { + /* Get the results for each model. */ + MmodGetResult(MilResult, M_DEFAULT, M_INDEX+M_TYPE_MIL_INT, Models); + MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition); + MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition); + MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle); + MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale); + MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score); + + /* Print information about the target image. */ + MosPrintf(MIL_TEXT("The models were found in the target ")); + MosPrintf(MIL_TEXT("image although there is:\n ")); + MosPrintf(MIL_TEXT("Full rotation\n Small scale change\n ")); + MosPrintf(MIL_TEXT("Contrast variation\n Specular reflection\n ")); + MosPrintf(MIL_TEXT("Occlusion\n Multiple models\n")); + MosPrintf(MIL_TEXT(" Multiple occurrences\n\n")); + + /* Print the results for the found models. */ + MosPrintf(MIL_TEXT("Result Model X Position Y Position ") + MIL_TEXT("Angle Scale Score\n\n")); + for (i=0; i to end.\n\n")); + MosGetch(); + + /* Free MIL objects. */ + MgraFree(GraphicList); + // MbufFree(MilImage); + MmodFree(MilSearchContext); + MmodFree(MilResult); +} diff --git a/src/Matrox/template_matching.h b/src/Matrox/template_matching.h new file mode 100644 index 0000000..0e0ec60 --- /dev/null +++ b/src/Matrox/template_matching.h @@ -0,0 +1,10 @@ +// +// Created by zjc on 24-11-12. +// + +#ifndef TEMPLATE_MATCHING_H +#define TEMPLATE_MATCHING_H +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); +#endif //TEMPLATE_MATCHING_H diff --git a/Matrox/ui.cpp b/src/Matrox/ui.cpp similarity index 100% rename from Matrox/ui.cpp rename to src/Matrox/ui.cpp diff --git a/Matrox/ui.h b/src/Matrox/ui.h similarity index 100% rename from Matrox/ui.h rename to src/Matrox/ui.h diff --git a/src/Matrox/utils.cpp b/src/Matrox/utils.cpp new file mode 100644 index 0000000..951f0e9 --- /dev/null +++ b/src/Matrox/utils.cpp @@ -0,0 +1,63 @@ +// +// Created by zjc on 24-11-18. +// + +#include "utils.h" + + +#include +#include +#include + +using namespace std; + +/** + * Convert Lab values from Photoshop range to OpenCV range. + * + * @param lab_ps A vector of Lab values in Photoshop range [L (0-100), a (-128 to 127), b (-128 to 127)]. + * @return A vector of Lab values in OpenCV range [L (0-255), a (0-255), b (0-255)]. + */ +vector psLabToOpenCVLab(const vector& lab_ps) { + int l_ps = lab_ps[0]; + int a_ps = lab_ps[1]; + int b_ps = lab_ps[2]; + + // Conversion formulas + int l_cv = round((l_ps / 100.0) * 255.0); // Scale L from 0-100 to 0-255 + int a_cv = round(((a_ps + 128.0) / 255.0) * 255.0); // Shift and scale a + int b_cv = round(((b_ps + 128.0) / 255.0) * 255.0); // Shift and scale b + + return {l_cv, a_cv, b_cv}; +} + +/** + * Convert Lab values from OpenCV range to Photoshop range. + * + * @param lab_cv A vector of Lab values in OpenCV range [L (0-255), a (0-255), b (0-255)]. + * @return A vector of Lab values in Photoshop range [L (0-100), a (-128 to 127), b (-128 to 127)]. + */ +vector opencvLabToPsLab(const vector& lab_cv) { + int l_cv = lab_cv[0]; + int a_cv = lab_cv[1]; + int b_cv = lab_cv[2]; + + // Conversion formulas + int l_ps = round((l_cv / 255.0) * 100.0); // Scale L from 0-255 to 0-100 + int a_ps = round((a_cv / 255.0) * 255.0 - 128.0); // Scale and shift a + int b_ps = round((b_cv / 255.0) * 255.0 - 128.0); // Scale and shift b + + return {l_ps, a_ps, b_ps}; +} + +MIL_ID convert_to_uint8(MIL_ID input_img) { + MIL_ID output_img; + MIL_ID MilSystem = MbufInquire(input_img, M_OWNER_SYSTEM, M_NULL); + MIL_INT size_x = MbufInquire(input_img, M_SIZE_X, M_NULL); + MIL_INT size_y = MbufInquire(input_img, M_SIZE_Y, M_NULL); + MbufAlloc2d(MilSystem, size_x, size_y, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &output_img); + MimArith(output_img, input_img, output_img, M_ADD); + MimArith(output_img, 255.0, output_img, M_MULT_CONST); + return output_img; +} + +// 图片转换函数,输入4096*1024*3的图片,输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1 diff --git a/Matrox/utils.h b/src/Matrox/utils.h similarity index 60% rename from Matrox/utils.h rename to src/Matrox/utils.h index 154a12a..b194df1 100644 --- a/Matrox/utils.h +++ b/src/Matrox/utils.h @@ -7,6 +7,14 @@ #include #include +#include +#include + +// 声明全局变量(注意:这里只是声明,不是定义) +extern __int64 MilApplication; +extern __int64 MilSystem; +extern __int64 MilDisplay; + template // Time measurement function void measure_execution_time(Func func) { @@ -18,4 +26,10 @@ void measure_execution_time(Func func) { std::cout << "Function execution time: " << duration.count() << " milliseconds" << std::endl; } +std::vector psLabToOpenCVLab(const std::vector& lab_ps); + +std::vector opencvLabToPsLab(const std::vector& lab_cv); + +MIL_ID convert_to_uint8(MIL_ID input_img); + #endif //UTILS_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..981cb19 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,16 @@ + +# 测试用例 1: template_color_range +add_executable(test_color_range + ${CMAKE_CURRENT_SOURCE_DIR}/test_color_range.cpp +) + +# 链接 Matrox 模块和依赖库 +target_link_libraries(test_color_range Matrox ${OpenCV_LIBS} ${MIL_LIBS}) + +# 测试用例 2: template_template_matching +add_executable(test_template_matching + ${CMAKE_CURRENT_SOURCE_DIR}/test_template_matching.cpp +) + +# 链接 Matrox 模块和依赖库 +target_link_libraries(test_template_matching Matrox ${OpenCV_LIBS} ${MIL_LIBS}) \ No newline at end of file diff --git a/tests/test_color_range.cpp b/tests/test_color_range.cpp new file mode 100644 index 0000000..dd0c6af --- /dev/null +++ b/tests/test_color_range.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include "Matrox/utils.h" +#include "Matrox/color_range.h" + +#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton_image\\174.bmp") +#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai.png") + +// Global variables +MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL; + +int main() +{ + // Initialize MIL application + MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, + M_NULL); + + // Load input image + MIL_ID MilImage = M_NULL; + MbufRestore(IMAGE_PATH, MilSystem, &MilImage); + + // 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; + + // Initialize combined result + MIL_ID detection_result = M_NULL; + + // Measure execution time + measure_execution_time([&]() { + high_sat_detect(MilImage, detection_result, params); + }); + MbufSave(SAVE_PATH, detection_result); + // Display result + + 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 diff --git a/tests/test_template_matching.cpp b/tests/test_template_matching.cpp new file mode 100644 index 0000000..36caf62 --- /dev/null +++ b/tests/test_template_matching.cpp @@ -0,0 +1,64 @@ +// +// Created by zjc on 24-11-20. +// +#include +#include +#include +#include +#include "Matrox/utils.h" +#include "Matrox/template_matching.h" + +#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton2.bmp") +#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png") + +// Global variables +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"] = -5; + 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; + + // Initialize MIL application + MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, + M_NULL); + + // Load input image + MIL_ID MilImage = M_NULL; + MbufRestore(IMAGE_PATH, MilSystem, &MilImage); + + // Initialize combined result + MIL_ID detection_result = M_NULL; + + // Measure execution time + measure_execution_time([&]() { + pre_process(MilImage, detection_result, params); + template_matching(detection_result, detection_result, params); + }); + MbufSave(SAVE_PATH, detection_result); + // Display result + + 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