From dc4eef197d0a970fd1f65d7bacc8bb1249074154 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Sep 2022 23:44:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=9D=E6=AD=A5=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E6=95=B0=E5=AD=97ROI=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/tasks.json | 20 +++++++++++---- src/MeoAssistant/DebugTask.cpp | 5 ++-- src/MeoAssistant/DepotImageAnalyzer.cpp | 34 ++++++++++++++++++------- test | 2 +- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/resource/tasks.json b/resource/tasks.json index bd4d7f1c1b..7e923663d8 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -8718,7 +8718,7 @@ 100 ] }, - "DeoptMatchData": { + "DepotMatchData": { "template": "empty.png", "templThreshold": 0.6, "roi": [ @@ -8733,7 +8733,7 @@ 255 ] }, - "DeoptBaseRect": { + "DepotBaseRect": { "template": "empty.png", "roi": [ 0, @@ -8742,18 +8742,28 @@ 150 ] }, - "DeoptQuantity": { + "DepotQuantity": { "template": "empty.png", - "templThreshold": 3, + "templThreshold": 4, "templThreshold_Doc": "这里用来作为字间距阈值", + "specialThreshold": 120, + "specialThreshold_Doc": "背景色亮度值上限", "roi": [ 36, 69, 47, 20 ], + "maskRange_Doc": "拿来做H通道的范围", "maskRange": [ - 150, + 0, + 180 + ], + "specificRect_Doc": "拿来做S、V通道的范围", + "specificRect": [ + 0, + 35, + 140, 255 ] }, diff --git a/src/MeoAssistant/DebugTask.cpp b/src/MeoAssistant/DebugTask.cpp index 614e31b1c3..e24086f980 100644 --- a/src/MeoAssistant/DebugTask.cpp +++ b/src/MeoAssistant/DebugTask.cpp @@ -7,6 +7,7 @@ // #include "RoguelikeSkillSelectionTaskPlugin.h" #include "AsstImageIo.hpp" +#include "DepotImageAnalyzer.h" #include "Logger.hpp" #include "StageDropsImageAnalyzer.h" @@ -22,7 +23,7 @@ bool asst::DebugTask::run() { size_t total = 0; size_t success = 0; - for (const auto& entry : std::filesystem::directory_iterator("../../test/drops/screenshots")) { + for (const auto& entry : std::filesystem::directory_iterator("../../test/depot/screenshots")) { cv::Mat image = asst::imread(entry.path()); if (image.empty()) { continue; @@ -30,7 +31,7 @@ bool asst::DebugTask::run() total += 1; cv::Mat resized; cv::resize(image, resized, cv::Size(1280, 720), 0, 0, cv::INTER_AREA); - StageDropsImageAnalyzer analyzer(resized); + DepotImageAnalyzer analyzer(resized); success += analyzer.analyze(); } Log.info(__FUNCTION__, success, "/", total); diff --git a/src/MeoAssistant/DepotImageAnalyzer.cpp b/src/MeoAssistant/DepotImageAnalyzer.cpp index 1c5a6c83bb..6dea3d4c24 100644 --- a/src/MeoAssistant/DepotImageAnalyzer.cpp +++ b/src/MeoAssistant/DepotImageAnalyzer.cpp @@ -46,7 +46,7 @@ void asst::DepotImageAnalyzer::resize() { LogTraceFunction; - m_resized_rect = Task.get("DeoptMatchData")->roi; + m_resized_rect = Task.get("DepotMatchData")->roi; cv::Size d_size(m_resized_rect.width, m_resized_rect.height); cv::resize(m_image, m_image_resized, d_size, 0, 0, cv::INTER_AREA); #ifdef ASST_DEBUG @@ -58,7 +58,7 @@ bool asst::DepotImageAnalyzer::analyze_base_rect() { LogTraceFunction; - Rect base_roi = Task.get("DeoptBaseRect")->roi; + Rect base_roi = Task.get("DepotBaseRect")->roi; ItemInfo base_item_info; size_t pos = match_item(base_roi, base_item_info, 0ULL, false); if (pos == NPos) { @@ -140,6 +140,11 @@ bool asst::DepotImageAnalyzer::analyze_all_items() info.rect = resize_rect_to_raw_size(info.rect); m_result.emplace(std::move(item_id), std::move(info)); } +#ifdef ASST_DEBUG + cv::Mat hsv; + cv::cvtColor(m_image_resized, hsv, cv::COLOR_BGR2HSV); +#endif + return !m_result.empty(); } @@ -158,7 +163,7 @@ size_t asst::DepotImageAnalyzer::match_item(const Rect& roi, /* out */ ItemInfo& const auto& all_items = ItemData.get_ordered_material_item_id(); MatchImageAnalyzer analyzer(m_image_resized); - analyzer.set_task_info("DeoptMatchData"); + analyzer.set_task_info("DepotMatchData"); // spacing 有时候算的差一个像素,干脆把 roi 扩大一点好了 Rect enlarged_roi = roi; if (with_enlarge) { @@ -197,18 +202,26 @@ size_t asst::DepotImageAnalyzer::match_item(const Rect& roi, /* out */ ItemInfo& int asst::DepotImageAnalyzer::match_quantity(const Rect& roi) { - auto task_ptr = Task.get("DeoptQuantity"); + auto task_ptr = Task.get("DepotQuantity"); Rect quantity_roi = roi.move(task_ptr->roi); cv::Mat quantity_img = m_image_resized(utils::make_rect(quantity_roi)); + cv::Mat hsv; + cv::cvtColor(quantity_img, hsv, cv::COLOR_BGR2HSV); + + const int h_lower = task_ptr->mask_range.first; + const int h_upper = task_ptr->mask_range.second; + const int s_lower = task_ptr->specific_rect.x; + const int s_upper = task_ptr->specific_rect.y; + const int v_lower = task_ptr->specific_rect.width; + const int v_upper = task_ptr->specific_rect.height; - cv::Mat gray; - cv::cvtColor(quantity_img, gray, cv::COLOR_BGR2GRAY); cv::Mat bin; - cv::inRange(gray, task_ptr->mask_range.first, task_ptr->mask_range.second, bin); + cv::inRange(hsv, cv::Scalar(h_lower, s_lower, v_lower), cv::Scalar(h_upper, s_upper, v_upper), bin); // split const int max_spacing = static_cast(task_ptr->templ_threshold); + const int bg_v_upper = static_cast(task_ptr->special_threshold); std::vector contours; int i_right = bin.cols - 1, i_left = 0; bool in = false; @@ -233,7 +246,10 @@ int asst::DepotImageAnalyzer::match_quantity(const Rect& roi) in = true; } else if (!in) { - if (++spacing > max_spacing && i_left != 0) { + ++spacing; + uchar bg_top_v = hsv.at(0, i)[2]; + uchar bg_btm_v = hsv.at(bin.rows - 1, i)[2]; + if (i_left != 0 && (spacing > max_spacing || bg_top_v > bg_v_upper || bg_btm_v > bg_v_upper)) { // filter out noise break; } @@ -294,7 +310,7 @@ asst::Rect asst::DepotImageAnalyzer::resize_rect_to_raw_size(const asst::Rect& r { LogTraceFunction; - m_resized_rect = Task.get("DeoptMatchData")->roi; + m_resized_rect = Task.get("DepotMatchData")->roi; double kx = static_cast(m_image.cols) / m_resized_rect.width; double ky = static_cast(m_image.rows) / m_resized_rect.height; diff --git a/test b/test index 856281d4f1..6486eaf4d7 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit 856281d4f1371cff8378d17ba9cdc862a70cfa72 +Subproject commit 6486eaf4d7b9ea6217dcc789fbddcae37712515f