matcher uses alpha channel as mask

This commit is contained in:
mrbesen 2022-04-24 16:44:07 +02:00
parent 965a0dab20
commit 494f2b23a1
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
9 changed files with 28 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

BIN
imgs/accept.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 894 B

After

Width:  |  Height:  |  Size: 380 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
imgs/pick.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

@ -5,6 +5,7 @@
class Matcher {
private:
cv::Mat templ;
cv::Mat mask;
int32_t posx = -1;
int32_t posy = -1;
@ -31,4 +32,7 @@ public:
private:
Match matchAll(const cv::Mat& img);
Match matchPos(const cv::Mat& img);
// when the template has a alpha channel try to create a mask from that
void maskFromTemplate();
};

View File

@ -171,7 +171,7 @@ void LolAutoAccept::innerRun() {
cv::resize(img, img, cv::Size(ScreenShot::DEFAULTWIDTH, ScreenShot::DEFAULTHEIGHT));
for(size_t i = 0; i < stages.size(); ++i) {
Log::trace << "processing stage" << i;
// Log::trace << "processing stage " << i;
Stage* stage = stages.at(i);
if(stage->process(*this, img)) {
Log::debug << "stage successful: " << i;

View File

@ -1,6 +1,6 @@
#include "lolautoaccept.h"
LolAutoAccept::AcceptStage::AcceptStage() : Stage("imgs/Accept.png") {
LolAutoAccept::AcceptStage::AcceptStage() : Stage("imgs/accept.png") {
matcher.setOffset(539, 529);
}

View File

@ -5,10 +5,12 @@
#include "util.h"
Matcher::Matcher(const std::string& filename) {
templ = cv::imread(filename, cv::IMREAD_COLOR);
templ = cv::imread(filename, cv::IMREAD_UNCHANGED); // unchanged so alpha channel does not get dropped
maskFromTemplate();
}
Matcher::Matcher(const cv::Mat& templ) {
templ.copyTo(this->templ);
maskFromTemplate();
}
Matcher::~Matcher() {}
@ -47,11 +49,11 @@ Matcher::Match Matcher::matchAll(const cv::Mat& img) {
Log::info << "match size: " << result_cols << " " << result_cols;
// match
cv::matchTemplate(img, templ, out, cv::TM_CCORR_NORMED);
cv::matchTemplate(img, templ, out, cv::TM_CCORR_NORMED, mask);
double minVal; double maxVal;
cv::Point minLoc; cv::Point maxLoc;
cv::minMaxLoc( out, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );
cv::minMaxLoc( out, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
Log::debug << "pixelcount: " << (templ.cols * templ.rows) << " minVal: " << minVal << " maxVal: " << maxVal << " minLoc: " << minLoc.x << " " << minLoc.y << " maxLoc: " << maxLoc.x << " " << maxLoc.y;
@ -67,7 +69,7 @@ Matcher::Match Matcher::matchPos(const cv::Mat& img) {
cv::Mat out(1, 1, CV_32FC1);
// match
cv::matchTemplate(matchpart, templ, out, cv::TM_CCORR_NORMED);
cv::matchTemplate(matchpart, templ, out, cv::TM_CCORR_NORMED, mask);
float val = out.at<float>({0, 0});
bool matched = val > 0.95;
@ -76,3 +78,19 @@ Matcher::Match Matcher::matchPos(const cv::Mat& img) {
return {matched, posx, posy, templ.cols, templ.rows};
}
void Matcher::maskFromTemplate() {
if(templ.channels() == 4) {
// split channels
std::vector<cv::Mat> split;
cv::split(templ, split);
// template without alpha channel
cv::merge(std::vector(split.begin(), split.end()-1), templ);
// 3*alpha channel
cv::Mat alphaChannel = split.at(3);
alphaChannel.convertTo(alphaChannel, CV_8U);
cv::merge(std::vector(3, alphaChannel), mask);
}
}