lolautoaccept/src/matcher.cpp
2022-03-06 02:13:41 +01:00

45 lines
1.4 KiB
C++

#include "matcher.h"
#include <Log.h>
Matcher::Matcher(const std::string& filename) {
templ = cv::imread(filename, cv::IMREAD_COLOR);
}
Matcher::Matcher(const cv::Mat& templ) {
templ.copyTo(this->templ);
}
Matcher::~Matcher() {}
bool Matcher::Match::operator==(const Match& other) const {
return doesMatch == other.doesMatch && x == other.x && y == other.y && width == other.width && height == other.height;
}
bool Matcher::Match::operator!=(const Match& other) const {
return !(*this == other);
}
// 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;
int result_rows = img.rows - templ.rows + 1;
cv::Mat out(result_rows, result_cols, CV_32FC1);
// remove Alpha channel from input
cv::Mat converted;
cv::cvtColor(img, converted, cv::COLOR_RGBA2RGB);
// match
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;
bool matched = maxVal > 0.95;
return {matched, maxLoc.x, maxLoc.y, templ.cols, templ.rows};
}