美文网首页Android
Android OpenCv image matching &a

Android OpenCv image matching &a

作者: JaedenKil | 来源:发表于2019-05-03 10:31 被阅读0次
    • 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'.");
    }
    

    相关文章

      网友评论

        本文标题:Android OpenCv image matching &a

        本文链接:https://www.haomeiwen.com/subject/cxyanqtx.html