mirror of
https://github.com/Karllzy/cotton_color.git
synced 2025-11-08 18:53:53 +00:00
添加读取配置文件的工具函数,实现加载多个模板
This commit is contained in:
parent
a7c5f4fec5
commit
96c55d89c0
@ -32,10 +32,12 @@ class TemplateMatcher {
|
||||
|
||||
bool isInitialized;
|
||||
|
||||
map<string, int> param;
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
TemplateMatcher(MIL_ID system, MIL_ID display):
|
||||
isInitialized(false) {}
|
||||
TemplateMatcher(MIL_ID system, MIL_ID display, map<string, int>& param):
|
||||
isInitialized(false) {this->param = param;}
|
||||
|
||||
// Destructor: Free MIL objects
|
||||
~TemplateMatcher() {
|
||||
@ -75,7 +77,10 @@ class TemplateMatcher {
|
||||
MIL_ID template_temporary;
|
||||
MbufRestore(convert_to_wstring(ModelImgPaths[i]), MilSystem, &template_temporary);
|
||||
MIL_ID template_temporary_uint8 = convert_to_uint8(template_temporary);
|
||||
MdispSelect(MilDisplay, template_temporary_uint8);
|
||||
if (this->param["isdisplay"] == 1)
|
||||
{
|
||||
MdispSelect(MilDisplay, template_temporary_uint8);
|
||||
}
|
||||
/* 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. */
|
||||
@ -83,11 +88,22 @@ class TemplateMatcher {
|
||||
|
||||
|
||||
|
||||
|
||||
MmodDefine(MilSearchContext, M_IMAGE, template_temporary_uint8,
|
||||
static_cast<MIL_DOUBLE>(ModelsOffsetX[i]),
|
||||
static_cast<MIL_DOUBLE>(ModelsOffsetY[i]),
|
||||
static_cast<MIL_DOUBLE>(ModelsSizeX[i]),
|
||||
static_cast<MIL_DOUBLE>(ModelsSizeY[i]));
|
||||
|
||||
MgraColor(M_DEFAULT, ModelsDrawColor[i]);
|
||||
MmodDraw( M_DEFAULT, MilSearchContext, GraphicList,
|
||||
M_DRAW_BOX+M_DRAW_POSITION, i, M_ORIGINAL);
|
||||
if (this->param["isdisplay"] == 1)
|
||||
{
|
||||
MosGetch();
|
||||
}
|
||||
MbufFree(template_temporary);
|
||||
MbufFree(template_temporary_uint8);
|
||||
}
|
||||
|
||||
// 设置一些参数
|
||||
@ -111,19 +127,20 @@ class TemplateMatcher {
|
||||
cout << "Templates loaded and preprocessed successfully.\n";
|
||||
|
||||
/* Draw boxes and positions in the source image to identify the models. */
|
||||
for (int i=0; i<ModelImgPaths.size(); i++)
|
||||
{
|
||||
MgraColor(M_DEFAULT, ModelsDrawColor[i]);
|
||||
MmodDraw( M_DEFAULT, MilSearchContext, GraphicList,
|
||||
M_DRAW_BOX+M_DRAW_POSITION, i, M_ORIGINAL);
|
||||
}
|
||||
// for (int i=0; i<ModelImgPaths.size(); i++)
|
||||
// {
|
||||
// MgraColor(M_DEFAULT, ModelsDrawColor[i]);
|
||||
// MmodDraw( M_DEFAULT, MilSearchContext, GraphicList,
|
||||
// M_DRAW_BOX+M_DRAW_POSITION, i, M_ORIGINAL);
|
||||
// }
|
||||
|
||||
/* Pause to show the models. */
|
||||
MosPrintf(MIL_TEXT("A model context was defined with the ")
|
||||
MIL_TEXT("models in the displayed image.\n"));
|
||||
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
|
||||
MosGetch();
|
||||
|
||||
if (this->param["debug_mode"] == 1) {
|
||||
MosGetch();
|
||||
}
|
||||
}
|
||||
|
||||
// Search for models in the input image
|
||||
@ -173,22 +190,30 @@ class TemplateMatcher {
|
||||
MgraColor(M_DEFAULT, ModelsDrawColor[Models[i]]);
|
||||
MmodDraw(M_DEFAULT, MilResult, GraphicList,
|
||||
M_DRAW_EDGES + M_DRAW_POSITION, i, M_DEFAULT);
|
||||
|
||||
}
|
||||
} else {
|
||||
cout << "No models found.\n";
|
||||
}
|
||||
MosPrintf(MIL_TEXT("Press <Enter> to EXIT.\n\n"));
|
||||
MosGetch();
|
||||
|
||||
|
||||
MbufFree(input_image_uint8);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
void test_template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const map<string, int>& params) {
|
||||
void test_template_matching(const MIL_ID &inputImage, MIL_ID &outputImage, map<string, int> ¶ms)
|
||||
{
|
||||
// Create a TemplateMatcher instance
|
||||
TemplateMatcher matcher(MilSystem, MilDisplay);
|
||||
TemplateMatcher matcher(MilSystem, MilDisplay, params);
|
||||
|
||||
//TODO: 1加入加载多个模板的功能
|
||||
//TODO: 1加入加载多个模板的功能 已
|
||||
//TODO: 2加入配置文件解析功能,解析后的文件与当前的para map<string, int>兼容
|
||||
// 配置文件当中加入是否显示参数,能调控加载模板的过程是否显示。
|
||||
//TODO: 3修改当前的代码使模板匹配不出错
|
||||
// 配置文件当中加入是否显示参数,能调控加载模板的过程是否显示。已
|
||||
//TODO: 3修改当前的代码使模板匹配不出错 已
|
||||
//TODO: 4成立模板文件夹,能够加载文件夹下的全部模板并实现检测
|
||||
//TODO: 5制作标准结构的函数,例如:matcher.findModels(MIL_ID inputImage, MIL_ID output_image, map);
|
||||
//TODO: 6完善相应部分的手册
|
||||
@ -196,8 +221,8 @@ void test_template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const
|
||||
|
||||
// Load template models
|
||||
vector<string> template_paths = {"C:\\Users\\zjc\\Desktop\\template1.png",
|
||||
"C:\\Users\\zjc\\Desktop\\template1.png",
|
||||
"C:\\Users\\zjc\\Desktop\\template1.png",
|
||||
"C:\\Users\\zjc\\Desktop\\template2.png",
|
||||
"C:\\Users\\zjc\\Desktop\\template3.png",
|
||||
};
|
||||
vector<MIL_INT> offsetX = {0, 20, 30};
|
||||
vector<MIL_INT> offsetY = {0, 20, 30};
|
||||
@ -209,7 +234,10 @@ void test_template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const
|
||||
// Find models
|
||||
matcher.findModels(inputImage);
|
||||
|
||||
|
||||
// Free resources
|
||||
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, MilDisplay);
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -7,4 +7,5 @@
|
||||
void pre_process(const MIL_ID& inputImage, MIL_ID& outputImageSuspect, const std::map<std::string, int>& params);
|
||||
|
||||
void template_matching(const MIL_ID& inputImageSuspect, MIL_ID& outputImage, const std::map<std::string, int>& params);
|
||||
void test_template_matching(const MIL_ID &inputImage, MIL_ID &outputImage, std::map<std::string, int> ¶ms) ;
|
||||
#endif //TEMPLATE_MATCHING_H
|
||||
|
||||
@ -74,4 +74,49 @@ wstring convert_to_wstring(const string& str) {
|
||||
return wstring(str.begin(), str.end());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void read_params_from_file(const std::string& filename, std::map<std::string, int>& params) {
|
||||
std::ifstream infile(filename);
|
||||
if (!infile) {
|
||||
std::cerr << "无法打开文件: " << filename << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(infile, line)) {
|
||||
// 去除行首和行尾的空白字符
|
||||
line.erase(0, line.find_first_not_of(" \t\r\n"));
|
||||
line.erase(line.find_last_not_of(" \t\r\n") + 1);
|
||||
|
||||
// 跳过空行和注释行
|
||||
if (line.empty() || line[0] == '#')
|
||||
continue;
|
||||
|
||||
// 查找等号的位置
|
||||
size_t pos = line.find('=');
|
||||
if (pos == std::string::npos)
|
||||
continue; // 如果没有等号,跳过该行
|
||||
|
||||
// 分割键和值,并去除空白字符
|
||||
std::string key = line.substr(0, pos);
|
||||
std::string value_str = line.substr(pos + 1);
|
||||
key.erase(0, key.find_first_not_of(" \t"));
|
||||
key.erase(key.find_last_not_of(" \t") + 1);
|
||||
value_str.erase(0, value_str.find_first_not_of(" \t"));
|
||||
value_str.erase(value_str.find_last_not_of(" \t") + 1);
|
||||
|
||||
// 将字符串转换为整数
|
||||
int value;
|
||||
std::istringstream iss(value_str);
|
||||
if (!(iss >> value)) {
|
||||
std::cerr << "键 " << key << " 的值无效: " << value_str << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 将键值对添加到参数映射中
|
||||
params[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片转换函数,输入4096*1024*3的图片,输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1
|
||||
|
||||
@ -9,6 +9,10 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <Mil.h>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
// 声明全局变量(注意:这里只是声明,不是定义)
|
||||
extern __int64 MilApplication;
|
||||
@ -33,5 +37,6 @@ std::vector<int> opencvLabToPsLab(const std::vector<int>& lab_cv);
|
||||
MIL_ID convert_to_uint8(MIL_ID input_img);
|
||||
|
||||
std::wstring convert_to_wstring(const std::string& str);
|
||||
void read_params_from_file(const std::string& filename, std::map<std::string, int>& params) ;
|
||||
|
||||
#endif //UTILS_H
|
||||
|
||||
@ -23,51 +23,7 @@ int main()
|
||||
|
||||
// Define color ranges
|
||||
std::map<std::string, int> 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;
|
||||
read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\color_range_config.txt", params);
|
||||
|
||||
// Initialize combined result
|
||||
MIL_ID detection_result = M_NULL;
|
||||
|
||||
@ -17,23 +17,9 @@ MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
|
||||
|
||||
int main() {
|
||||
using namespace std;
|
||||
map<string, int> 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"] = 0;
|
||||
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;
|
||||
|
||||
std::map<std::string, int> params;
|
||||
read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\config.txt", params);
|
||||
// Initialize MIL application
|
||||
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL,
|
||||
M_NULL);
|
||||
@ -46,7 +32,8 @@ int main() {
|
||||
MIL_ID detection_result = M_NULL;
|
||||
|
||||
// Measure execution time
|
||||
measure_execution_time([&]() {
|
||||
measure_execution_time([&]()
|
||||
{
|
||||
pre_process(MilImage, detection_result, params);
|
||||
test_template_matching(detection_result, detection_result, params);
|
||||
});
|
||||
@ -56,9 +43,10 @@ int main() {
|
||||
std::cout << "所有颜色检测已完成并合并。按 <Enter> 退出。" << std::endl;
|
||||
getchar();
|
||||
|
||||
// Free resources
|
||||
|
||||
|
||||
MbufFree(detection_result);
|
||||
MbufFree(MilImage);
|
||||
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user