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);