From 526bb6c2db7987ffb4e7e35652b7c5d331cc6ced 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 | 2 -- 3 files changed, 24 insertions(+), 8 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..856f331 100644 --- a/tests/test_template_matching.cpp +++ b/tests/test_template_matching.cpp @@ -47,8 +47,6 @@ int main() { std::cout << "所有颜色检测已完成并合并。按 退出。" << std::endl; getchar(); - - MbufFree(detection_result); MbufFree(MilImage);