mirror of
https://github.com/Karllzy/cotton_color.git
synced 2025-11-08 18:53:53 +00:00
将模板匹配出的结果显示为白色,其余为黑色
This commit is contained in:
parent
445d6fc37c
commit
171376aaa1
@ -112,7 +112,7 @@ void TemplateMatcher::loadTemplates(const std::vector<std::string>& 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<std::string, int> ¶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<std::strin
|
||||
);
|
||||
}
|
||||
|
||||
void TemplateMatcher::FindTemplates( const MIL_ID& inputImage,const MIL_ID& outputImage,TemplateMatcher& matcher)
|
||||
void TemplateMatcher::FindTemplates( const MIL_ID& inputImage, MIL_ID& outputImage,TemplateMatcher& matcher)
|
||||
{
|
||||
// Perform template matching
|
||||
matcher.findModels(inputImage);
|
||||
matcher.findModels(inputImage,outputImage);
|
||||
|
||||
// Notify user that matching is complete
|
||||
cout << "Template matching completed.\n";
|
||||
|
||||
@ -18,6 +18,7 @@ private:
|
||||
MIL_ID MilSearchContext;
|
||||
MIL_ID MilResult;
|
||||
MIL_ID GraphicList;
|
||||
#define SAVE_PATH2 MIL_TEXT("C:\\Users\\zjc\\Desktop\\detection.png")
|
||||
|
||||
std::vector<std::string> ModelImgPaths;
|
||||
std::vector<MIL_INT> ModelsOffsetX;
|
||||
@ -43,11 +44,11 @@ public:
|
||||
const std::vector<MIL_DOUBLE>& 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<std::string, int> ¶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
|
||||
|
||||
@ -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<std::string, int> 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 << "所有颜色检测已完成并合并。按 <Enter> 退出。" << std::endl;
|
||||
getchar();
|
||||
|
||||
|
||||
|
||||
MbufFree(detection_result);
|
||||
MbufFree(MilImage);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user