From 171376aaa1a010f22387f64fd252690a39d76caf Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Sat, 23 Nov 2024 14:46:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=A8=A1=E6=9D=BF=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E5=87=BA=E7=9A=84=E7=BB=93=E6=9E=9C=E6=98=BE=E7=A4=BA=E4=B8=BA?= =?UTF-8?q?=E7=99=BD=E8=89=B2=EF=BC=8C=E5=85=B6=E4=BD=99=E4=B8=BA=E9=BB=91?= =?UTF-8?q?=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Matrox/template_matching.cpp | 25 +++++++++++++++++++++---- src/Matrox/template_matching.h | 5 +++-- tests/test_template_matching.cpp | 13 ++++++++++--- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Matrox/template_matching.cpp b/src/Matrox/template_matching.cpp index dd3077b..7b16eef 100644 --- a/src/Matrox/template_matching.cpp +++ b/src/Matrox/template_matching.cpp @@ -112,7 +112,7 @@ void TemplateMatcher::loadTemplates(const std::vector& template_pat } // Search for models in the input image -void TemplateMatcher::findModels(const MIL_ID& inputImage) +void TemplateMatcher::findModels(const MIL_ID& inputImage,MIL_ID& outputImage) { if (!isInitialized) { std::cerr << "Templates are not loaded. Please load templates before searching.\n"; @@ -147,6 +147,13 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage) MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale.data()); MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score.data()); + // Create a binary image buffer + MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL), + MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED, + M_IMAGE + M_PROC, &outputImage); + // Initialize the binary image to black + MbufClear(outputImage, 0); + // Display results std::cout << "Found " << NumResults << " model(s) in " << Time * 1000.0 << " ms:\n"; std::cout << "Result Model X Position Y Position Angle Scale Score\n"; @@ -155,11 +162,20 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage) << YPosition[i] << " " << Angle[i] << " " << Scale[i] << " " << Score[i] << "%\n"; - // Draw results + // Draw results onto the binary image + MgraColor(M_DEFAULT, 255); // White color for binary image + MmodDraw(M_DEFAULT, MilResult, outputImage, M_DRAW_EDGES + M_DRAW_POSITION, i, M_DEFAULT); + + // Draw results on the graphical list for display MgraColor(M_DEFAULT, ModelsDrawColor[Models[i]]); MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_EDGES + M_DRAW_POSITION, i, M_DEFAULT); } + + // Display or save the binary image + + MbufSave(SAVE_PATH2, outputImage); + } else { std::cout << "No models found.\n"; } @@ -167,6 +183,7 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage) MosGetch(); MbufFree(input_image_uint8); } + void TemplateMatcher::LoadTemplate(TemplateMatcher& matcher, std::map ¶ms) { // Create a TemplateMatcher instance (consider making it static if you want to retain it between calls) @@ -189,10 +206,10 @@ void TemplateMatcher::LoadTemplate(TemplateMatcher& matcher, std::map ModelImgPaths; std::vector ModelsOffsetX; @@ -43,11 +44,11 @@ public: const std::vector& drawColor); // Search for models in the input image - void findModels(const MIL_ID& inputImage); +void findModels(const MIL_ID& inputImage,MIL_ID& outputImage); void LoadTemplate(TemplateMatcher &matcher, std::map ¶ms); - void FindTemplates(const MIL_ID &inputImage, const MIL_ID &outputImage, TemplateMatcher &matcher); + void FindTemplates(const MIL_ID &inputImage, MIL_ID &outputImage, TemplateMatcher &matcher); // Destructor diff --git a/tests/test_template_matching.cpp b/tests/test_template_matching.cpp index a719b21..7a788f1 100644 --- a/tests/test_template_matching.cpp +++ b/tests/test_template_matching.cpp @@ -8,9 +8,10 @@ #include "Matrox/utils.h" #include "Matrox/template_matching.h" -#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton2.bmp") +#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\8.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; @@ -18,6 +19,8 @@ MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL; int main() { using namespace std; + + std::map params; read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\template_config.txt", params); // Initialize MIL application @@ -30,6 +33,7 @@ int main() { // Initialize combined result MIL_ID detection_result = M_NULL; + MIL_ID detection_resize = M_NULL; MIL_ID output_Image= M_NULL; TemplateMatcher matcher(MilSystem, MilDisplay, params); @@ -37,6 +41,11 @@ int main() { measure_execution_time([&]() { pre_process(MilImage, detection_result, params); + MbufAlloc2d(MilSystem, MbufInquire(detection_result, M_SIZE_X, M_NULL)/2, + MbufInquire(detection_result, M_SIZE_Y, M_NULL)/2, 1 + M_UNSIGNED, + M_IMAGE + M_PROC, &detection_resize); + MimResize(detection_result,detection_resize,0.5,0.5,M_DEFAULT); + matcher.LoadTemplate(matcher,params); matcher.FindTemplates(detection_result,output_Image,matcher); //最后的释放问题应该出在寻找模板里面 @@ -47,8 +56,6 @@ int main() { std::cout << "所有颜色检测已完成并合并。按 退出。" << std::endl; getchar(); - - MbufFree(detection_result); MbufFree(MilImage);