From e93b5fbff7393f01692112e232a8cb8f954edc62 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Tue, 26 Nov 2024 15:15:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mask=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E8=AF=BB=E5=8F=96=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Matrox/template_matching.cpp | 2 +- src/Matrox/utils.cpp | 88 ++++++++++++++++---------------- src/Matrox/utils.h | 2 + tests/test_mask.cpp | 19 +++++-- 4 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/Matrox/template_matching.cpp b/src/Matrox/template_matching.cpp index c0c3642..57a3c38 100644 --- a/src/Matrox/template_matching.cpp +++ b/src/Matrox/template_matching.cpp @@ -264,7 +264,7 @@ void TemplateMatcher::FindTemplates( const MIL_ID& inputImage, MIL_ID& outputIma -// TODO: Opencv ONNX runner, +// TODO: Opencv ONNX runner, 已 // 1. 构建相应的模型加载和模型运行函数- // 2. 在src里头添加另一个cvdl库,专用于视觉深度学习 // 3. 添加一个类OnnxRunner diff --git a/src/Matrox/utils.cpp b/src/Matrox/utils.cpp index 0021ebe..80b8856 100644 --- a/src/Matrox/utils.cpp +++ b/src/Matrox/utils.cpp @@ -121,6 +121,50 @@ void read_params_from_file(const std::string& filename, std::map +#include +#include +#include + + + +// 函数:从配置文件中读取参数 +std::unordered_map loadConfig(const std::string& filename){ + unordered_map config; + ifstream file(filename); + string line; + + while (getline(file, line)) { + // 跳过空行和注释行 + if (line.empty() || line[0] == ';' || line[0] == '#') + continue; + + // 删除行尾的空格 + line.erase(line.find_last_not_of(" \t\n\r") + 1); + + // 查找 '=' 分隔符的位置 + size_t pos = line.find('='); + if (pos == string::npos) { + continue; // 没有 '=',跳过此行 + } + + string key = line.substr(0, pos); // 获取参数名 + string valueStr = line.substr(pos + 1); // 获取参数值 + + // 删除键和值两端的空格 + key.erase(key.find_last_not_of(" \t\n\r") + 1); + valueStr.erase(valueStr.find_last_not_of(" \t\n\r") + 1); + + // 尝试将参数值转换为整数 + int value; + stringstream(valueStr) >> value; + + config[key] = value; // 将参数存入 map + } + + return config; +} + // 图片转换函数,输入4096*1024*3的图片,输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1 #include "mil.h" // 包含 MIL 库的头文件 @@ -181,47 +225,3 @@ cv::Mat milToMat(MIL_ID milImage) { return matImage; } -void processImage(cv::Mat& img) { - // 1. 将图像从BGR转换到HSV色彩空间 - cv::Mat hsv; - cv::cvtColor(img, hsv, cv::COLOR_BGR2HSV); - - // 2. 定义绿色的HSV色彩范围 - cv::Scalar lower_green(35, 40, 40); // 低阈值 (H, S, V) - cv::Scalar upper_green(85, 255, 255); // 高阈值 (H, S, V) - - // 3. 根据绿色范围创建掩膜 - cv::Mat green_mask; - cv::inRange(hsv, lower_green, upper_green, green_mask); - - // 4. 使用形态学操作去除噪点,增强绿色区域 - cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5)); - cv::morphologyEx(green_mask, green_mask, cv::MORPH_CLOSE, kernel); - - // 5. 查找轮廓 - std::vector> contours; - cv::findContours(green_mask, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); - - // 6. 创建一个黑色背景图像 - cv::Mat result = cv::Mat::zeros(img.size(), img.type()); - - // 7. 遍历轮廓,找到矩形框并将其内部填充为白色 - for (size_t i = 0; i < contours.size(); i++) { - // 通过近似多边形来检测矩形 - std::vector approx; - cv::approxPolyDP(contours[i], approx, cv::arcLength(contours[i], true) * 0.02, true); - - // 如果是四边形,认为是矩形 - if (approx.size() == 4) { - // 计算矩形的bounding box - cv::Rect rect = cv::boundingRect(approx); - - // 仅在绿色区域内将矩形框填充为白色 - cv::rectangle(result, rect, cv::Scalar(255, 255, 255), cv::FILLED); - } - } - - // 8. 显示结果图像 - cv::imshow("Processed Image", result); - cv::waitKey(0); -} \ No newline at end of file diff --git a/src/Matrox/utils.h b/src/Matrox/utils.h index 72b933f..9e6cc8d 100644 --- a/src/Matrox/utils.h +++ b/src/Matrox/utils.h @@ -41,4 +41,6 @@ std::wstring convert_to_wstring(const std::string& str); void read_params_from_file(const std::string& filename, std::map& params) ; cv::Mat milToMat(MIL_ID MilImage); void processImage(cv::Mat& img); + +std::unordered_map loadConfig(const std::string& filename); #endif //UTILS_H diff --git a/tests/test_mask.cpp b/tests/test_mask.cpp index 76f6396..70a0aa6 100644 --- a/tests/test_mask.cpp +++ b/tests/test_mask.cpp @@ -1,18 +1,27 @@ // // Created by zjc on 24-11-26. // +#include + #include "vector" #include"iostream" #include"string" #include"Matrox/mask.h" int main() { // 指定图像路径 - std::string imagePath = "C:\\Users\\zjc\\Desktop\\diguandai.png"; - + std::string imagePath = "C:\\Users\\zjc\\Desktop\\suspect_mask.png"; + std::unordered_map config = loadConfig("C:\\Users\\zjc\\Desktop\\config\\mask_config.txt"); // 设置分块数量和白色像素点阈值 - int widthBlocks = 24; - int heightBlocks = 24; - int threshold = 20; + int widthBlocks = config["widthBlocks"]; + int heightBlocks = config["heightBlocks"]; + int threshold = config["threshold"]; + + // 输出参数值 + std::cout << "widthBlocks: " << widthBlocks << std::endl; + + // int widthBlocks = 24; + // int heightBlocks = 24; + // int threshold = 20; // 生成掩膜 std::vector> mask = generateMaskFromImage(imagePath, widthBlocks, heightBlocks, threshold);