Android opencv

作者: JaedenKil | 来源:发表于2019-03-18 15:17 被阅读0次

    Refer to android test with OpenCV.

    • Load Mat image from R drawable file:
        Mat loadImageFromR(int resourceId) throws IOException {
            Log.v(TAG, "loadImageFromR: Template image: " + ctx.getResources().getResourceEntryName(resourceId) + ".");
            return Utils.loadResource(ctx, resourceId, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        }
    
    • Get current ui Bitmap from screenshot
        Bitmap takeBitmapScreenshot(){
            Log.v(TAG, "takeBitmapScreenshot: Take a bitmap of current screenshot now.");
            //return mUiAutomation.takeScreenshot();
            Bitmap bmp = mUiAutomation.takeScreenshot();
            int width = bmp.getWidth();
            int height = bmp.getHeight();
            Log.v(TAG, "takeBitmapScreenshot: bitmap width is " + width);
            Log.v(TAG, "takeBitmapScreenshot: bitmap height is " + height);
            if (width != 1920 || height != 1080) {
                //bitmap dimension is not 1920*1080, need to transform
                Bitmap bitmap_1080p = resizeBitmap(bmp, 1920, 1080);
                int newWidth = bitmap_1080p.getWidth();
                int newHeight = bitmap_1080p.getHeight();
                Log.v(TAG, "takeBitmapScreenshot: After resize, the new width is: " + newWidth);
                Log.v(TAG, "takeBitmapScreenshot: After resize, the new height is: " + newHeight);
                if ((newWidth == 1920) && (newHeight == 1080)) {
                    Log.v(TAG, "takeBitmapScreenshot: The bitmap size is 1080p after resize.");
                } else {
                    Log.e(TAG, "takeBitmapScreenshot: Wrong bitmap size after resize! This test is likely to fail.");
                }
                return bitmap_1080p;
            }
            return bmp;
        }
    
    • Convert Bitmap to Mat
        Mat convertBitmapToMat(Bitmap bmp) {
             /*
            The newly created Mat is channel 4, type CV_8UC4,
            and the Mat loaded from R file is channel 3, type CV_8UC3.
            So we need to DO THE CONVERT!!!
             */
            Mat src = new Mat();
            Bitmap bmp32 = bmp.copy(Bitmap.Config.RGB_565, true);
            Utils.bitmapToMat(bmp32, src);
            Mat dest = new Mat();
            Imgproc.cvtColor(src, dest, Imgproc.COLOR_BGRA2RGB);
            if (!dest.empty()) {
                Log.v(TAG, "convertBitmapToMat: Current Mat image is not empty.");
            } else {
                Log.w(TAG, "convertBitmapToMat: Current Mat image is empty!!!");
            }
            return dest;
        }
    
    • Do image compare:
        private static final double THRESHOLD = 0.90;
    
        boolean matchImages(Mat img, Mat template) {
            /*
            This method to do image matching is a little less complex.
            It receives two Mat images, do compare.
            If the highest possibility equals or is above the threshold, it returns true, else returns false.
            But it does not draw the rectangle whether the match is found or not.
            This method is used when we only need to know if match found but there is no need to see the match result with our own eyes.
             */
            Log.v(TAG, "matchImages: Run image match method simple version.");
    
            if (!img.empty()){
                Log.v(TAG, "matchImages: Load source image succeed !");
            } else {
                Log.e(TAG, "matchImages: Fail to load source image !!!");
            }
    
            if (!template.empty()) {
                Log.v(TAG, "matchImages: Load template image succeed !");
            } else {
                Log.e(TAG, "matchImages: Fail to load template image !!!");
            }
    
            int result_cols = img.cols() - template.cols() + 1;
            int result_rows = img.rows() - template.rows() + 1;
            Log.v(TAG, "matchImages: Initialize the result Mat.");
            Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
    
            Mat img_gray = new Mat();
            Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_RGB2GRAY);
    
            //Do the matching and NOT normalize
            Log.v(TAG, "matchImages: Do the match.");
            Imgproc.matchTemplate(img_gray, template, result, Imgproc.TM_CCOEFF_NORMED);
    
            //Localize the best match with minMaxLoc
            Log.v(TAG, "matchImages: Localize the best match.");
            Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
    
            Log.v(TAG, "matchImages: Match percentage : " + mmr.maxVal);
    
            if (mmr.maxVal >= THRESHOLD) {
                Log.d(TAG, "matchImages: Match percentage is above the THRESHOLD.");
                return true;
            } else {
                Log.w(TAG, "matchImages: Match percentage is below the THRESHOLD!!!");
                return false;
            }
        }
    

    相关文章

      网友评论

        本文标题:Android opencv

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