Rework pattern count temp var

This commit is contained in:
WarmUpTill 2026-03-19 17:19:00 +01:00
parent e76de98234
commit dcb8a727a3
3 changed files with 41 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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<cv::Rect> MatchObject(QImage &img, cv::CascadeClassifier &cascade,
double scaleFactor, int minNeighbors,
const cv::Size &minSize,