Skip to content

Commit

Permalink
fix: 修复截图获取错误,添加一些日志
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jul 16, 2023
1 parent 9bc1cd3 commit 4ec5ed8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion source/Controller/ControllerMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ cv::Mat ControllerMgr::screencap()
{
std::unique_lock<std::mutex> lock(image_mutex_);
action_runner_->post({ .type = Action::Type::screencap }, true);
return image_;
return image_.clone();
}

void ControllerMgr::start_app()
Expand Down
4 changes: 3 additions & 1 deletion source/Task/PipelineTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions source/Vision/Comparator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Comparator.h"

#include "MaaUtils/Logger.hpp"
#include "Utils/NoWarningCV.h"
#include "VisionUtils.hpp"

Expand All @@ -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;
}
}
Expand Down
41 changes: 17 additions & 24 deletions source/Vision/Matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
}

Expand Down
4 changes: 2 additions & 2 deletions source/Vision/Matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
};
Expand Down

0 comments on commit 4ec5ed8

Please sign in to comment.