From f9609dbd342b24b0b05a13ae532c4dfccf632964 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Sun, 8 Dec 2024 19:38:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8B=96=E5=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Matrox/mask.cpp | 13 ++++++++----- tests/test_mask.cpp | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Matrox/mask.cpp b/src/Matrox/mask.cpp index 87e259f..cd9e02c 100644 --- a/src/Matrox/mask.cpp +++ b/src/Matrox/mask.cpp @@ -55,15 +55,18 @@ std::vector> generateMaskFromImage(const MIL_ID& inputImage // 遍历每一列,处理规则:当某列出现第一个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) { + int i = 0; + while (i < heightBlocks) { + if (mask[i][j] == 1) { // 找到第一个1,处理后面rowRange行 for (int k = i; k < std::min(i + rowRange, heightBlocks); ++k) { mask[k][j] = 1; } - marked = true; // 标记为已处理,后续连续的1不再处理 + // 跳过已经设置为255的后rowRange行 + i += rowRange; + } else { + // 如果当前位置为0,则继续检测下一行 + ++i; } } } diff --git a/tests/test_mask.cpp b/tests/test_mask.cpp index f0411dd..de2ad34 100644 --- a/tests/test_mask.cpp +++ b/tests/test_mask.cpp @@ -9,7 +9,7 @@ #include"Matrox/mask.h" #include"mil.h" -#define Image_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\mask_1.png") +#define Image_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\mask_5.png") MIL_ID MilApplication, MilSystem, MilDisplay; int main() { @@ -30,7 +30,7 @@ int main() { int widthBlocks = 24; int heightBlocks = 1024; int threshold = 20; - int rowRange = 50; // 后续50行设置为1 + int rowRange = 50; // 后续50行设置为 // 生成掩膜 std::vector> mask = generateMaskFromImage(MilImage, widthBlocks, heightBlocks, threshold, rowRange);