From 24a92b4a011d4b9d35c512fc581c6a6a809db0cb Mon Sep 17 00:00:00 2001 From: Status102 <102887808+status102@users.noreply.github.com> Date: Mon, 11 Aug 2025 18:12:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=90=88=E5=B9=B6ocrBinThreshold?= =?UTF-8?q?=20(#13635)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en-us/protocol/task-schema.md | 7 ++----- docs/ja-jp/protocol/task-schema.md | 5 +---- docs/ko-kr/protocol/task-schema.md | 5 +---- docs/maa_tasks_schema.json | 18 +++++++++--------- docs/maa_tasks_schema_en.json | 18 +++++++++--------- docs/zh-cn/protocol/task-schema.md | 7 ++----- docs/zh-tw/protocol/task-schema.md | 7 ++----- resource/tasks/Roguelike/JieGarden.json | 16 ++++++++-------- src/MaaCore/Common/AsstTypes.h | 17 ++++++++--------- src/MaaCore/Config/TaskData.cpp | 14 ++++---------- src/MaaCore/Vision/Config/OCRerConfig.cpp | 4 ++-- 11 files changed, 48 insertions(+), 70 deletions(-) diff --git a/docs/en-us/protocol/task-schema.md b/docs/en-us/protocol/task-schema.md index d294771dde..618b1f3b17 100644 --- a/docs/en-us/protocol/task-schema.md +++ b/docs/en-us/protocol/task-schema.md @@ -154,11 +154,8 @@ Please note that JSON files do not support comments. The comments in this docume "useRaw": true, // Optional, whether to use original image for matching // Default true, false for grayscale matching - "binThresholdLower": 140, // Optional, lower threshold for grayscale binarization (default 140) - // Pixels with grayscale below this value considered background, excluded from text area - - "binThresholdUpper": 255, // Optional, upper threshold for grayscale binarization (default 255) - // Pixels with grayscale above this value considered background, excluded from text area + "binThreshold": [140, 255], // Optional, threshold for grayscale binarization (default [140, 255]) + // Pixels with grayscale out of range are considered background, excluded from text area // Final text foreground includes pixels in [lower, upper] range /* Fields below only valid when algorithm is JustReturn and action is Input */ diff --git a/docs/ja-jp/protocol/task-schema.md b/docs/ja-jp/protocol/task-schema.md index 90d01fd918..0180e46105 100644 --- a/docs/ja-jp/protocol/task-schema.md +++ b/docs/ja-jp/protocol/task-schema.md @@ -152,10 +152,7 @@ JSONファイルはコメントをサポートしていません。テキスト "useRaw": true, // オプション、原画像でマッチングを行うかどうか // 指定されていない場合、デフォルトは true、false の場合はグレースケールでマッチング - "binThresholdLower": 140, // オプション項目。グレースケールの二値化下限(デフォルトは140) - // この値より低い画素は背景とみなされ、文字領域から除外されます - - "binThresholdUpper": 255, // オプション項目。グレースケールの二値化上限(デフォルトは255) + "binThreshold": [140, 255], // オプション項目。グレースケールの二値化上限(デフォルトは[140, 255]) // この値より高い画素は背景とみなされ、文字領域から除外されます // [lower, upper] 範囲内の画素のみが文字の前景として残ります diff --git a/docs/ko-kr/protocol/task-schema.md b/docs/ko-kr/protocol/task-schema.md index c208c1ca2b..256ea3bced 100644 --- a/docs/ko-kr/protocol/task-schema.md +++ b/docs/ko-kr/protocol/task-schema.md @@ -153,10 +153,7 @@ icon: material-symbols:task "useRaw": true, // 선택 사항, 원본 이미지를 사용하여 매칭할지 여부 // 기본값은 true이며, false이면 그레이스케일 매칭 - "binThresholdLower": 140, // 선택 사항. 그레이스케일 이진화의 하한 임계값 (기본값: 140) - // 이 값보다 낮은 픽셀은 배경으로 간주되어 문자 영역에서 제외됩니다 - - "binThresholdUpper": 255, // 선택 사항. 그레이스케일 이진화의 상한 임계값 (기본값: 255) + "binThreshold": [140, 255], // 선택 사항. 그레이스케일 이진화의 상한 임계값 (기본값: [140, 255]) // 이 값보다 높은 픽셀은 배경으로 간주되어 문자 영역에서 제외됩니다 // 최종적으로 [lower, upper] 범위의 픽셀만 문자 전경으로 유지됩니다 diff --git a/docs/maa_tasks_schema.json b/docs/maa_tasks_schema.json index 45fb50a4a4..2ba6eed3a4 100644 --- a/docs/maa_tasks_schema.json +++ b/docs/maa_tasks_schema.json @@ -315,15 +315,15 @@ "default": true, "description": "可选项,是否使用原图匹配\n不填写默认 true,false 时为灰度匹配\n仅当 algorithm 为 OcrDetect 且 withoutDet 为 false 时生效。" }, - "binThresholdLower": { - "type": "integer", - "default": 140, - "description": "可选项,二值化灰度下阈值(默认 140)\n灰度值小于该值的像素将被视为背景,排除在文字区域之外\n仅当 algorithm 为 OcrDetect 且 withoutDet 为 false 时生效。" - }, - "binThresholdUpper": { - "type": "integer", - "default": 255, - "description": "可选项,二值化灰度上阈值(默认 255)\n灰度值大于该值的像素将被视为背景,排除在文字区域之外\n最终保留 [lower, upper] 区间的像素作为文字前景\n仅当 algorithm 为 OcrDetect 且 withoutDet 为 false 时生效。" + "binThreshold": { + "type": "array", + "maxItems": 2, + "minItems": 2, + "items": { + "type": "integer" + }, + "default": [140, 255], + "description": "可选项,二值化灰度阈值(默认 [140, 255])\n灰度值不处于范围内的像素将被视为背景,排除在文字区域之外\n仅当 algorithm 为 OcrDetect 且 withoutDet 为 false 时生效。" } }, "required": ["algorithm", "text"], diff --git a/docs/maa_tasks_schema_en.json b/docs/maa_tasks_schema_en.json index 1dbd70a844..4483ce3471 100644 --- a/docs/maa_tasks_schema_en.json +++ b/docs/maa_tasks_schema_en.json @@ -315,15 +315,15 @@ "default": true, "description": "Optional, whether to use the original image for matching.\nDefault is true. If false, grayscale image matching will be used.\nThis is only effective when algorithm is OcrDetect and withoutDet is false." }, - "binThresholdLower": { - "type": "integer", - "default": 140, - "description": "Optional, lower threshold for grayscale binarisation (default 140).\nPixels with a grayscale value lower than this will be considered background and excluded from the text region.\nThis is only effective when algorithm is OcrDetect and withoutDet is false." - }, - "binThresholdUpper": { - "type": "integer", - "default": 255, - "description": "Optional, upper threshold for grayscale binarisation (default 255).\nPixels with a grayscale value higher than this will be considered background and excluded from the text region.\nOnly pixels within the [lower, upper] range will be kept as text foreground.\nThis is only effective when algorithm is OcrDetect and withoutDet is false." + "binThreshold": { + "type": "array", + "maxItems": 2, + "minItems": 2, + "items": { + "type": "integer" + }, + "default": [140, 255], + "description": "Optional, threshold for grayscale binarisation (default [140, 255]).\nPixels with a grayscale value out of range will be considered background and excluded from the text region.\nThis is only effective when algorithm is OcrDetect and withoutDet is false." } }, "required": ["algorithm", "text"], diff --git a/docs/zh-cn/protocol/task-schema.md b/docs/zh-cn/protocol/task-schema.md index 310f216314..f2195b7fff 100644 --- a/docs/zh-cn/protocol/task-schema.md +++ b/docs/zh-cn/protocol/task-schema.md @@ -153,11 +153,8 @@ icon: material-symbols:task "useRaw": true, // 可选项,是否使用原图匹配 // 不填写默认 true,false 时为灰度匹配 - "binThresholdLower": 140, // 可选项,二值化灰度下阈值(默认 140) - // 灰度值小于该值的像素将被视为背景,排除在文字区域之外 - - "binThresholdUpper": 255, // 可选项,二值化灰度上阈值(默认 255) - // 灰度值大于该值的像素将被视为背景,排除在文字区域之外 + "binThreshold": [140, 255], // 可选项,二值化灰度阈值(默认 [140, 255]) + // 灰度值不处于范围的像素将被视为背景,排除在文字区域之外 // 最终保留 [lower, upper] 区间的像素作为文字前景 /* 以下字段仅当 algorithm 为 JustReturn,action 为 Input 时有效 */ diff --git a/docs/zh-tw/protocol/task-schema.md b/docs/zh-tw/protocol/task-schema.md index 4676fc2f6e..72c9e4d6b2 100644 --- a/docs/zh-tw/protocol/task-schema.md +++ b/docs/zh-tw/protocol/task-schema.md @@ -151,11 +151,8 @@ icon: material-symbols:task "useRaw": true, // 可選項,是否使用原圖匹配 // 不填寫預設 true,false 時為灰階匹配 - "binThresholdLower": 140, // 可選項,灰階二值化下限閾值(預設為 140) - // 灰階值小於此值的像素會被視為背景,排除在文字區域之外 - - "binThresholdUpper": 255, // 可選項,灰階二值化上限閾值(預設為 255) - // 灰階值大於此值的像素會被視為背景,排除在文字區域之外 + "binThreshold": [140, 255], // 可選項,灰階二值化閾值(預設為 [140, 255]) + // 灰階值不在範圍的像素會被視為背景,排除在文字區域之外 // 最終僅保留 [lower, upper] 區間的像素作為文字前景 /* 以下欄位僅當 algorithm 為 JustReturn 且 action 為 Input 時有效 */ diff --git a/resource/tasks/Roguelike/JieGarden.json b/resource/tasks/Roguelike/JieGarden.json index ae8d0264e2..7fe60cfec5 100644 --- a/resource/tasks/Roguelike/JieGarden.json +++ b/resource/tasks/Roguelike/JieGarden.json @@ -998,7 +998,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode0": { @@ -1014,7 +1014,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode0_NOT_IN_USE": { @@ -1031,7 +1031,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 210, + "binThreshold": [210, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode1": { @@ -1047,7 +1047,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode10001": { @@ -1063,7 +1063,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode4": { @@ -1079,7 +1079,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode6": { @@ -1095,7 +1095,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@StrategyChange_mode7": { @@ -1111,7 +1111,7 @@ ], "roi": [627, 14, 71, 28], "withoutDet": true, - "binThresholdLower": 200, + "binThreshold": [200, 255], "useRaw": false }, "JieGarden@Roguelike@TodoEnter": {}, diff --git a/src/MaaCore/Common/AsstTypes.h b/src/MaaCore/Common/AsstTypes.h index 2f2405c62f..bff6a219e8 100644 --- a/src/MaaCore/Common/AsstTypes.h +++ b/src/MaaCore/Common/AsstTypes.h @@ -598,16 +598,15 @@ struct OcrTaskInfo : public TaskInfo constexpr OcrTaskInfo(OcrTaskInfo&&) noexcept = default; constexpr OcrTaskInfo& operator=(const OcrTaskInfo&) = default; constexpr OcrTaskInfo& operator=(OcrTaskInfo&&) noexcept = default; - std::vector text; // 文字的容器,匹配到这里面任一个,就算匹配上了 - bool full_match = false; // 是否需要全匹配,否则搜索到子串就算匹配上了 - bool is_ascii = false; // 是否启用字符数字模型 - bool without_det = false; // 是否不使用检测模型 - bool replace_full = false; // 匹配之后,是否将整个字符串replace(false是只替换match的部分) - bool use_raw = true; // 是否使用原始图片进行识别,false则使用灰度图 + std::vector text; // 文字的容器,匹配到这里面任一个,就算匹配上了 + bool full_match = false; // 是否需要全匹配,否则搜索到子串就算匹配上了 + bool is_ascii = false; // 是否启用字符数字模型 + bool without_det = false; // 是否不使用检测模型 + bool replace_full = false; // 匹配之后,是否将整个字符串replace(false是只替换match的部分) + bool use_raw = true; // 是否使用原始图片进行识别,false则使用灰度图 std::vector> - replace_map; // 部分文字容易识别错,字符串强制replace之后,再进行匹配 - int bin_threshold_lower = 140; - int bin_threshold_upper = 255; + replace_map; // 部分文字容易识别错,字符串强制replace之后,再进行匹配 + std::array bin_threshold = { 140, 255 }; // 二值化灰度上阈值 }; using OcrTaskPtr = std::shared_ptr; diff --git a/src/MaaCore/Config/TaskData.cpp b/src/MaaCore/Config/TaskData.cpp index 114d2d5f2d..73096a558c 100644 --- a/src/MaaCore/Config/TaskData.cpp +++ b/src/MaaCore/Config/TaskData.cpp @@ -792,15 +792,9 @@ asst::TaskPtr asst::TaskData::generate_ocr_task_info( utils::get_and_check_value_or( name, task_json, - "binThresholdLower", - ocr_task_info_ptr->bin_threshold_lower, - default_ptr->bin_threshold_lower); - utils::get_and_check_value_or( - name, - task_json, - "binThresholdUpper", - ocr_task_info_ptr->bin_threshold_upper, - default_ptr->bin_threshold_upper); + "binThreshold", + ocr_task_info_ptr->bin_threshold, + default_ptr->bin_threshold); utils::get_and_check_value_or(name, task_json, "useRaw", ocr_task_info_ptr->use_raw, default_ptr->use_raw); return ocr_task_info_ptr; } @@ -1072,7 +1066,7 @@ bool asst::TaskData::syntax_check(std::string_view task_name, const json::value& // specific "cache", "fullMatch", "isAscii", "ocrReplace", "rectMove", "replaceFull", "roi", "text", "withoutDet", "useRaw", - "binThresholdLower", "binThresholdUpper", + "binThreshold", } }, { AlgorithmType::FeatureMatch, { diff --git a/src/MaaCore/Vision/Config/OCRerConfig.cpp b/src/MaaCore/Vision/Config/OCRerConfig.cpp index 3c07d5d379..205f9d8692 100644 --- a/src/MaaCore/Vision/Config/OCRerConfig.cpp +++ b/src/MaaCore/Vision/Config/OCRerConfig.cpp @@ -98,8 +98,8 @@ void OCRerConfig::_set_task_info(OcrTaskInfo task_info) set_replace(task_info.replace_map, task_info.replace_full); m_params.use_char_model = task_info.is_ascii; m_params.without_det = task_info.without_det; - m_params.bin_threshold_lower = task_info.bin_threshold_lower; - m_params.bin_threshold_upper = task_info.bin_threshold_upper; + m_params.bin_threshold_lower = task_info.bin_threshold[0]; + m_params.bin_threshold_upper = task_info.bin_threshold[1]; m_params.use_raw = task_info.use_raw; _set_roi(task_info.roi);