lolautoaccept/src/matcher.cpp

48 lines
1.5 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);
}
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_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());
double minVal; double maxVal;
cv::Point minLoc; cv::Point maxLoc;
cv::minMaxLoc( out, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );
Log::debug << "minValbn: " << minValbn << " maxValbn: " << maxValbn << " minVal: " << minVal << " maxVal: " << maxVal << " minLoc: " << minLoc.x << " " << minLoc.y << " maxLoc: " << maxLoc.x << " " << maxLoc.y;
bool matched = minValbn < 2e7;
return {matched, minLoc.x, minLoc.y, templ.cols, templ.rows};
}