From dcb8a727a3183d7a86da4696429340e75969905e Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Thu, 19 Mar 2026 17:19:00 +0100 Subject: [PATCH] Rework pattern count temp var --- plugins/video/macro-condition-video.cpp | 14 +++++++++--- plugins/video/opencv-helpers.cpp | 29 +++++++++++++++++++++++++ plugins/video/opencv-helpers.hpp | 1 + 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/plugins/video/macro-condition-video.cpp b/plugins/video/macro-condition-video.cpp index 425d5704..04746a5f 100644 --- a/plugins/video/macro-condition-video.cpp +++ b/plugins/video/macro-condition-video.cpp @@ -283,10 +283,18 @@ bool MacroConditionVideo::ScreenshotContainsPattern() SetTempVarValue("patternCount", "0"); return false; } - const auto count = countNonZero(result); + SetTempVarValue("similarity", std::to_string(bestMatchValue)); - SetTempVarValue("patternCount", std::to_string(count)); - return count > 0; + + if (IsTempVarInUse("patternCount")) { + const auto count = CountPatternMatches( + result, {_patternImageData.rgbaPattern.cols, + _patternImageData.rgbaPattern.rows}); + SetTempVarValue("patternCount", std::to_string(count)); + return count > 0; + } + + return countNonZero(result) > 0; } bool MacroConditionVideo::FileInputIsUpToDate() const diff --git a/plugins/video/opencv-helpers.cpp b/plugins/video/opencv-helpers.cpp index 63a920d0..df0f1f10 100644 --- a/plugins/video/opencv-helpers.cpp +++ b/plugins/video/opencv-helpers.cpp @@ -97,6 +97,35 @@ double MatchPattern(QImage &img, const PatternImageData &patternData, return bestFitValue; } +int CountPatternMatches(const cv::Mat &result, const cv::Size &patternSize) +{ + if (result.empty()) { + return 0; + } + + cv::Mat work = result.clone(); + int count = 0; + + while (true) { + double maxVal; + cv::Point maxLoc; + cv::minMaxLoc(work, nullptr, &maxVal, nullptr, &maxLoc); + if (maxVal <= 0.0) { + break; + } + count++; + // Suppress the template-sized region around this match so + // overlapping high-scoring positions are not counted separately + int x = std::max(0, maxLoc.x - patternSize.width / 2); + int y = std::max(0, maxLoc.y - patternSize.height / 2); + int w = std::min(patternSize.width, work.cols - x); + int h = std::min(patternSize.height, work.rows - y); + work(cv::Rect(x, y, w, h)) = 0.0f; + } + + return count; +} + double MatchPattern(QImage &img, QImage &pattern, double threshold, cv::Mat &result, bool useAlphaAsMask, cv::TemplateMatchModes matchColor) diff --git a/plugins/video/opencv-helpers.hpp b/plugins/video/opencv-helpers.hpp index 5f95d545..9ed6a95a 100644 --- a/plugins/video/opencv-helpers.hpp +++ b/plugins/video/opencv-helpers.hpp @@ -68,6 +68,7 @@ double MatchPattern(QImage &img, const PatternImageData &patternData, double MatchPattern(QImage &img, QImage &pattern, double threshold, cv::Mat &result, bool useAlphaAsMask, cv::TemplateMatchModes matchMode); +int CountPatternMatches(const cv::Mat &result, const cv::Size &patternSize); std::vector MatchObject(QImage &img, cv::CascadeClassifier &cascade, double scaleFactor, int minNeighbors, const cv::Size &minSize,