From 4ec5ed89f91d479771805b61bf73d5680481f36a Mon Sep 17 00:00:00 2001 From: MistEO Date: Mon, 17 Jul 2023 00:35:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=88=AA=E5=9B=BE?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=94=99=E8=AF=AF=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Controller/ControllerMgr.cpp | 2 +- source/Task/PipelineTask.cpp | 4 ++- source/Vision/Comparator.cpp | 8 ++++-- source/Vision/Matcher.cpp | 41 ++++++++++++----------------- source/Vision/Matcher.h | 4 +-- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/source/Controller/ControllerMgr.cpp b/source/Controller/ControllerMgr.cpp index 48e9232c7..9eabcc145 100644 --- a/source/Controller/ControllerMgr.cpp +++ b/source/Controller/ControllerMgr.cpp @@ -156,7 +156,7 @@ cv::Mat ControllerMgr::screencap() { std::unique_lock lock(image_mutex_); action_runner_->post({ .type = Action::Type::screencap }, true); - return image_; + return image_.clone(); } void ControllerMgr::start_app() diff --git a/source/Task/PipelineTask.cpp b/source/Task/PipelineTask.cpp index 5c3ae0bad..abee9cd70 100644 --- a/source/Task/PipelineTask.cpp +++ b/source/Task/PipelineTask.cpp @@ -330,6 +330,8 @@ void PipelineTask::wait_freezes(const MAA_PIPELINE_RES_NS::WaitFreezesParams& pa } using namespace MAA_VISION_NS; + LogFunc << VAR(param.time); + cv::Rect target = get_target_rect(param.target, param.target_param, cur_box); Comparator comp; @@ -339,8 +341,8 @@ void PipelineTask::wait_freezes(const MAA_PIPELINE_RES_NS::WaitFreezesParams& pa .method = param.method, }); - auto pre_time = std::chrono::steady_clock::now(); cv::Mat pre_image = controller()->screencap(); + auto pre_time = std::chrono::steady_clock::now(); while (!need_exit()) { cv::Mat cur_image = controller()->screencap(); diff --git a/source/Vision/Comparator.cpp b/source/Vision/Comparator.cpp index 59ac44e54..308c51f1a 100644 --- a/source/Vision/Comparator.cpp +++ b/source/Vision/Comparator.cpp @@ -1,5 +1,6 @@ #include "Comparator.h" +#include "MaaUtils/Logger.hpp" #include "Utils/NoWarningCV.h" #include "VisionUtils.hpp" @@ -19,8 +20,11 @@ Comparator::Result Comparator::analyze(const cv::Mat& lhs, const cv::Mat& rhs) c for (const cv::Rect& roi : param_.roi) { cv::Mat lhs_roi = lhs(correct_roi(roi, lhs)); cv::Mat rhs_roi = rhs(correct_roi(roi, rhs)); - bool ret = comp(lhs_roi, rhs_roi, param_.method) > param_.threshold; - if (!ret) { + double similarity = comp(lhs_roi, rhs_roi, param_.method); + + LogTrace << VAR(roi) << VAR(similarity) << VAR(param_.threshold); + + if (similarity < param_.threshold) { return false; } } diff --git a/source/Vision/Matcher.cpp b/source/Vision/Matcher.cpp index 6c316d97d..9f43f836a 100644 --- a/source/Vision/Matcher.cpp +++ b/source/Vision/Matcher.cpp @@ -31,45 +31,47 @@ Matcher::ResultOpt Matcher::analyze() const continue; } double threshold = param_.thresholds.at(i); + auto start = std::chrono::steady_clock::now(); - LogTrace << param_.template_paths.at(i) << VAR(i) << VAR(threshold); - auto ret = traverse_rois(templ, threshold); - if (ret) { - return ret; + auto res = traverse_rois(templ, threshold); + + auto costs = duration_since(start); + LogTrace << param_.template_paths.at(i) << VAR(res.score) << VAR(threshold) << VAR(costs); + + if (res.score > threshold) { + return res; } } return std::nullopt; } -Matcher::ResultOpt Matcher::traverse_rois(const cv::Mat& templ, double threshold) const +Matcher::Result Matcher::traverse_rois(const cv::Mat& templ, double threshold) const { if (!cache_.empty()) { - return match_and_postproc(cache_, templ, threshold); + return match_and_postproc(cache_, templ); } if (param_.roi.empty()) { - return match_and_postproc(cv::Rect(0, 0, image_.cols, image_.rows), templ, threshold); + return match_and_postproc(cv::Rect(0, 0, image_.cols, image_.rows), templ); } for (const cv::Rect& roi : param_.roi) { - auto opt = match_and_postproc(roi, templ, threshold); - if (opt) { - return opt; + auto res = match_and_postproc(roi, templ); + if (res.score > threshold) { + return res; } } - return std::nullopt; + return {}; } -Matcher::ResultOpt Matcher::match_and_postproc(const cv::Rect& roi, const cv::Mat& templ, double threshold) const +Matcher::Result Matcher::match_and_postproc(const cv::Rect& roi, const cv::Mat& templ) const { - auto start = std::chrono::steady_clock::now(); - cv::Mat image = image_with_roi(roi); cv::Mat matched = match_template(image, templ, param_.method, param_.green_mask); if (matched.empty()) { - return std::nullopt; + return {}; } double min_val = 0.0, max_val = 0.0; @@ -80,17 +82,8 @@ Matcher::ResultOpt Matcher::match_and_postproc(const cv::Rect& roi, const cv::Ma max_val = 0; } - if (max_val < threshold) { - return std::nullopt; - } - cv::Rect box(max_loc.x + roi.x, max_loc.y + roi.y, templ.cols, templ.rows); - if (max_val > threshold * 0.7) { - auto costs = duration_since(start); - LogTrace << VAR(box) << VAR(max_val) << VAR(costs); - } - return Result { .box = box, .score = max_val }; } diff --git a/source/Vision/Matcher.h b/source/Vision/Matcher.h index 4b620e106..a9e9b7dfa 100644 --- a/source/Vision/Matcher.h +++ b/source/Vision/Matcher.h @@ -25,8 +25,8 @@ class Matcher : public VisionBase ResultOpt analyze() const; private: - ResultOpt traverse_rois(const cv::Mat& templ, double threshold) const; - ResultOpt match_and_postproc(const cv::Rect& roi, const cv::Mat& templ, double threshold) const; + Result traverse_rois(const cv::Mat& templ, double threshold) const; + Result match_and_postproc(const cv::Rect& roi, const cv::Mat& templ) const; TemplMatchingParams param_; };