diff --git a/src/Matrox/mask.cpp b/src/Matrox/mask.cpp index e564bfa..87e259f 100644 --- a/src/Matrox/mask.cpp +++ b/src/Matrox/mask.cpp @@ -1,15 +1,21 @@ #include "mask.h" // 读取二值化的单通道一位图片并生成掩膜 -std::vector> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold = 10) { - // 读取图像 - cv::Mat image = cv::imread(imagePath, cv::IMREAD_GRAYSCALE); +#include +#include +#include - // 检查图像是否成功读取 - if (image.empty()) { - std::cerr << "无法加载图像,请检查路径是否正确: " << imagePath << std::endl; - exit(EXIT_FAILURE); - } +#include "utils.h" + +std::vector> generateMaskFromImage(const MIL_ID& inputImage, int widthBlocks, int heightBlocks, int threshold = 10, int rowRange = 50) { + // 读取图像 + cv::Mat image = mil2mat(inputImage); + // + // // 检查图像是否成功读取 + // if (image.empty()) { + // std::cerr << "无法加载图像,请检查路径是否正确: " << imagePath << std::endl; + // exit(EXIT_FAILURE); + // } // 确保图像是二值化的 cv::threshold(image, image, 128, 255, cv::THRESH_BINARY); @@ -22,8 +28,8 @@ std::vector> generateMaskFromImage(const std::string& imagePat int blockWidth = imageWidth / widthBlocks; int blockHeight = imageHeight / heightBlocks; - // 创建掩膜矩阵 - std::vector> mask(heightBlocks, std::vector(widthBlocks, false)); + // 创建掩膜矩阵 (uint8_t 类型) + std::vector> mask(heightBlocks, std::vector(widthBlocks, 0)); // 遍历每个块并统计白色像素点的数量 for (int i = 0; i < heightBlocks; ++i) { @@ -40,9 +46,24 @@ std::vector> generateMaskFromImage(const std::string& imagePat // 统计块中白色像素的数量 int whitePixelCount = cv::countNonZero(block); - // 如果白色像素数大于阈值,将该块标记为 true + // 如果白色像素数大于阈值,将该块标记为 255 if (whitePixelCount > threshold) { - mask[i][j] = true; + mask[i][j] = 1; + } + } + } + + // 遍历每一列,处理规则:当某列出现第一个1时,将其后rowRange行全部置为255 + for (int j = 0; j < widthBlocks; ++j) { + bool marked = false; // 标记当前列是否已经处理过第一个1 + + for (int i = 0; i < heightBlocks; ++i) { + if (mask[i][j] == 1&& !marked) { + // 找到第一个1,处理后面rowRange行 + for (int k = i; k < std::min(i + rowRange, heightBlocks); ++k) { + mask[k][j] = 1; + } + marked = true; // 标记为已处理,后续连续的1不再处理 } } } @@ -50,4 +71,3 @@ std::vector> generateMaskFromImage(const std::string& imagePat return mask; } - diff --git a/src/Matrox/mask.h b/src/Matrox/mask.h index fee2b3e..6a3ebfd 100644 --- a/src/Matrox/mask.h +++ b/src/Matrox/mask.h @@ -8,9 +8,9 @@ #include #include +#include"mil.h" #include -std::vector> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold); - +std::vector> generateMaskFromImage(const MIL_ID& inputImage, int widthBlocks, int heightBlocks, int threshold , int rowRange) ; #endif //MASK_H diff --git a/tests/test_mask.cpp b/tests/test_mask.cpp index 70a0aa6..f0411dd 100644 --- a/tests/test_mask.cpp +++ b/tests/test_mask.cpp @@ -1,39 +1,49 @@ // // Created by zjc on 24-11-26. // -#include +// #include #include "vector" #include"iostream" #include"string" #include"Matrox/mask.h" + +#include"mil.h" +#define Image_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\mask_1.png") +MIL_ID MilApplication, MilSystem, MilDisplay; + int main() { // 指定图像路径 - 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 = config["widthBlocks"]; - int heightBlocks = config["heightBlocks"]; - int threshold = config["threshold"]; + // std::string imagePath = "C:\\Users\\zjc\\Desktop\\mask_1.png"; + MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); + MIL_ID MilImage = M_NULL, MilHighSatResult = M_NULL, MilTemplateMatchingResult = M_NULL; + MbufRestore(Image_PATH3, MilSystem, &MilImage); + // std::unordered_map config = loadConfig("C:\\Users\\zjc\\Desktop\\config\\mask_config.txt"); + // // 设置分块数量和白色像素点阈值 + // int widthBlocks = config["widthBlocks"]; + // int heightBlocks = config["heightBlocks"]; + // int threshold = config["threshold"]; // 输出参数值 - std::cout << "widthBlocks: " << widthBlocks << std::endl; + // std::cout << "widthBlocks: " << widthBlocks << std::endl; - // int widthBlocks = 24; - // int heightBlocks = 24; - // int threshold = 20; + int widthBlocks = 24; + int heightBlocks = 1024; + int threshold = 20; + int rowRange = 50; // 后续50行设置为1 // 生成掩膜 - std::vector> mask = generateMaskFromImage(imagePath, widthBlocks, heightBlocks, threshold); + std::vector> mask = generateMaskFromImage(MilImage, widthBlocks, heightBlocks, threshold, rowRange); // 打印掩膜结果 for (int i = 0; i < heightBlocks; ++i) { - for (int j = 0; j < widthBlocks; ++j) - { - std::cout << (mask[i][j] ? "1 " : "0 "); + for (int j = 0; j < widthBlocks; ++j) { + std::cout << (int)mask[i][j] << " "; } std::cout << std::endl; } + MbufFree(MilImage); + MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); return 0; } \ No newline at end of file