From 8b3177ce671848c1c8c7dc661f2f0790280cc723 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 6 Mar 2022 01:08:32 +0100 Subject: [PATCH] normalized matcher --- include/screen.h | 4 ++-- src/lolautoaccept.cpp | 6 +++++- src/main.cpp | 13 ------------- src/matcher.cpp | 13 +++++-------- src/scaleableinputs.cpp | 5 +++++ 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/include/screen.h b/include/screen.h index 6cb340c..866678a 100644 --- a/include/screen.h +++ b/include/screen.h @@ -51,10 +51,10 @@ public: } constexpr double getXScale() const { - return wattrib.width / DEFAULTWIDTH; + return wattrib.width / (double) DEFAULTWIDTH; } constexpr double getYScale() const { - return wattrib.height / DEFAULTHEIGHT; + return wattrib.height / (double) DEFAULTHEIGHT; } }; diff --git a/src/lolautoaccept.cpp b/src/lolautoaccept.cpp index b1bc781..6eaf216 100644 --- a/src/lolautoaccept.cpp +++ b/src/lolautoaccept.cpp @@ -63,7 +63,7 @@ void LolAutoAccept::checkForGame() { void LolAutoAccept::performClick(uint32_t nr) { inputs.setOffset(screen->getXOffset(), screen->getYOffset()); - inputs.setScale(1 / screen->getXScale(), 1 / screen->getYScale()); + inputs.setScale(screen->getXScale(), screen->getYScale()); auto p = inputs.get(nr); Log::info << "click " << nr << " @ " << p.x << " " << p.y; @@ -94,6 +94,7 @@ void LolAutoAccept::pickFirst(const std::string& search) { } LolAutoAccept::LolAutoAccept() : acceptmatcher("imgs/Accept.png"), arrowmatcher("imgs/arrowdown.png"), sim(XInputSimulator::getInstance()) { + // click positions in 1280x720 scale inputs.addPoint({645, 560}); // accept game inputs.addPoint({775, 105}); // search box inputs.addPoint({180, 160}); // first champ @@ -109,6 +110,9 @@ void LolAutoAccept::run() { screen = (wins.at(0)); // just take the first + // testing: whole screen: + // screen = new ScreenShot(); + //delete other screens for(uint32_t i = 1; i < wins.size(); ++i) { ScreenShot* ss = wins.at(i); diff --git a/src/main.cpp b/src/main.cpp index fa8b09c..7bff905 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,19 +12,6 @@ int main(int argc, const char** argv) { Log::info << "Hello, World!"; - Matcher test1("imgs/arrowdown.png"); - Matcher test2("imgs/blitzcranktest.png"); - auto testimg = cv::imread("imgs/Bildschirmfoto vom 2022-02-28 19-27-59.png"); - auto testimg2 = cv::imread("imgs/Bildschirmfoto vom 2022-02-28 19-27-44.png"); - test1.match(testimg); - test2.match(testimg); - - test1.match(testimg2); - test2.match(testimg2); - - return 0; - - LolAutoAccept lolaa; lolaa.run(); diff --git a/src/matcher.cpp b/src/matcher.cpp index eead0fd..868dddb 100644 --- a/src/matcher.cpp +++ b/src/matcher.cpp @@ -19,6 +19,7 @@ bool Matcher::Match::operator!=(const Match& other) const { } +// https://stackoverflow.com/a/43133263/4399859 Matcher::Match Matcher::match(const cv::Mat& img) { // create out mat int result_cols = img.cols - templ.cols + 1; @@ -30,19 +31,15 @@ Matcher::Match Matcher::match(const cv::Mat& img) { cv::cvtColor(img, converted, cv::COLOR_RGBA2RGB); // match - cv::matchTemplate(converted, templ, out, cv::TM_SQDIFF); - - double minValbn; double maxValbn; - cv::minMaxLoc( out, &minValbn, &maxValbn, NULL, NULL, cv::Mat() ); - cv::normalize(out, out, 0, 1, cv::NORM_MINMAX, -1, cv::Mat()); + cv::matchTemplate(converted, templ, out, cv::TM_CCORR_NORMED); double minVal; double maxVal; cv::Point minLoc; cv::Point maxLoc; cv::minMaxLoc( out, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() ); - Log::debug << "pixelcount: " << (templ.cols * templ.rows) << " minValbn: " << minValbn << " maxValbn: " << maxValbn << " minVal: " << minVal << " maxVal: " << maxVal << " minLoc: " << minLoc.x << " " << minLoc.y << " maxLoc: " << maxLoc.x << " " << maxLoc.y; + Log::debug << "pixelcount: " << (templ.cols * templ.rows) << /* " minValbn: " << minValbn << " maxValbn: " << maxValbn << */ " minVal: " << minVal << " maxVal: " << maxVal << " minLoc: " << minLoc.x << " " << minLoc.y << " maxLoc: " << maxLoc.x << " " << maxLoc.y; - bool matched = minValbn < 2e7; + bool matched = maxVal > 0.95; - return {matched, minLoc.x, minLoc.y, templ.cols, templ.rows}; + return {matched, maxLoc.x, maxLoc.y, templ.cols, templ.rows}; } \ No newline at end of file diff --git a/src/scaleableinputs.cpp b/src/scaleableinputs.cpp index 90f853a..491929c 100644 --- a/src/scaleableinputs.cpp +++ b/src/scaleableinputs.cpp @@ -1,5 +1,7 @@ #include "scaleableinputs.h" +#include + void ScaleableInputs::addPoint(Point p) { points.push_back(p); } @@ -16,5 +18,8 @@ void ScaleableInputs::setOffset(double x, double y) { ScaleableInputs::Point ScaleableInputs::get(uint32_t nr) const { Point p = points.at(nr); + + Log::debug << "scaling: " << p.x << " " << p.y << " with: " << xOffset << " " << yOffset << " scale: " << xScale << " " << yScale; + return {(uint32_t) ((p.x * xScale) + xOffset), (uint32_t) ((p.y * yScale) + yOffset)}; } \ No newline at end of file