Android OpenCv image matching &a
2019-05-03 本文已影响0人
JaedenKil
- Import OpenCV to android, may refer here.
- Method to return
Core.MinMaxLocResult
:
Core.MinMaxLocResult getMinMaxLocResult(Mat img, Mat temp) {
String prefix = "GetMinMaxLocResult: ";
if (!img.empty()) {
Log.v(TAG, prefix + "Load source image succeed.");
} else {
Log.e(TAG, prefix + "Fail to load source image !!!");
Assert.fail(prefix + "Fail to load source image !!!");
}
if (!temp.empty()) {
Log.v(TAG, prefix + "Load source image succeed.");
} else {
Log.e(TAG, prefix + "Fail to load source image !!!");
Assert.fail(prefix + "Fail to load source image !!!");
}
int cols = img.cols() - temp.cols();
int rows = img.rows() - temp.rows();
Mat res = new Mat(rows, cols, CvType.CV_32FC1);
Mat imgGray = new Mat();
Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_RGB2GRAY);
Imgproc.matchTemplate(imgGray, temp, res, Imgproc.TM_CCOEFF_NORMED);
return Core.minMaxLoc(res);
}
- Method to check whether match or not:
boolean match(Core.MinMaxLocResult mmr) {
String prefix = "Match: ";
double bestMatch = mmr.maxVal;
Log.d(TAG, prefix + "Currently the best match percentage is '" + bestMatch + "'.");
if (mmr.maxVal >= threshold) {
Log.d(TAG, prefix + "Match percentage is above the THRESHOLD.");
return true;
} else {
Log.w(TAG, prefix + "Match percentage is below the THRESHOLD!!!");
return false;
}
}
- Method to click on the center point of the match (if there is a match found):
void clickOnMatch(Core.MinMaxLocResult mmr, Mat temp) {
String prefix = "ClickOnMatch: ";
double cols = temp.cols();
double rows = temp.rows();
Point center = new Point(mmr.maxLoc.x + cols / 2, mmr.maxLoc.y + rows / 2);
int x = (int) center.x;
int y = (int) center.y;
Log.d(TAG, prefix + "Will click at matched image center '" + x + ", " + y + "'.");
device.click(x, y);
}
- Apply these methods:
bmp = methods.takeBitmapScreenshot();
Mat mat = methods.convertBitmapToPng(bmp);
Core.MinMaxLocResult mmr = methods.getMinMaxLocResult(mat, temp);
if (methods.match(mmr)) {
Log.d(TAG, prefix + "Found a match of button 'Mini-Game'.");
Log.i(TAG, prefix + "Will try to click at the center of the match.");
methods.clickOnMatch(mmr, temp);
} else {
Log.d(TAG, prefix + "Not found any match of button 'Mini-Game'.");
}