簡體   English   中英

使用ORB和RANSAC的Android關鍵點匹配和閾值在OpenCV中的性能問題

[英]Performance Issues in OpenCV for Android Keypoint Matching and threshold using ORB and RANSAC

我最近開始在Android Studio上開發應用程序,而我剛完成編寫代碼。 我獲得的精度令人滿意,但是設備花費的時間很多 {}我遵循了一些有關如何在android studio上監視性能的教程,我發現我的代碼的一小部分花了6秒鍾 ,這是我的應用顯示整個結果所需時間的一半 我已經看過很多Java OpenCV的帖子-從knnMatch提取好的匹配項OpenCV過濾 OpenCV / JavaCV上的ORB匹配項 ,但是沒有遇到任何問這個問題的人。 OpenCV鏈接http://docs.opencv.org/2.4/doc/tutorials/features2d/feature_homography/feature_homography.html確實提供了很好的教程,但是與C ++相比,OpenCV中的RANSAC函數采用了不同的關鍵點參數。

這是我的代碼

     public Mat ORB_detection (Mat Scene_image, Mat Object_image){
    /*This function is used to find the reference card in the captured image with the help of
    * the reference card saved in the application
    * Inputs - Captured image (Scene_image), Reference Image (Object_image)*/
    FeatureDetector orb = FeatureDetector.create(FeatureDetector.DYNAMIC_ORB);
    /*1.a Keypoint Detection for Scene Image*/
    //convert input to grayscale
    channels = new ArrayList<Mat>(3);
    Core.split(Scene_image, channels);
    Scene_image = channels.get(0);
    //Sharpen the image
    Scene_image = unsharpMask(Scene_image);
    MatOfKeyPoint keypoint_scene = new MatOfKeyPoint();
    //Convert image to eight bit, unsigned char
    Scene_image.convertTo(Scene_image, CvType.CV_8UC1);
    orb.detect(Scene_image, keypoint_scene);
    channels.clear();

    /*1.b Keypoint Detection for Object image*/
    //convert input to grayscale
    Core.split(Object_image,channels);
    Object_image = channels.get(0);
    channels.clear();
    MatOfKeyPoint keypoint_object = new MatOfKeyPoint();
    Object_image.convertTo(Object_image, CvType.CV_8UC1);
    orb.detect(Object_image, keypoint_object);

    //2. Calculate the descriptors/feature vectors
    //Initialize orb descriptor extractor
    DescriptorExtractor orb_descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    Mat Obj_descriptor = new Mat();
    Mat Scene_descriptor = new Mat();
    orb_descriptor.compute(Object_image, keypoint_object, Obj_descriptor);
    orb_descriptor.compute(Scene_image, keypoint_scene, Scene_descriptor);

    //3. Matching the descriptors using Brute-Force
    DescriptorMatcher brt_frc = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    MatOfDMatch matches = new MatOfDMatch();
    brt_frc.match(Obj_descriptor, Scene_descriptor, matches);

    //4. Calculating the max and min distance between Keypoints
    float max_dist = 0,min_dist = 100,dist =0;
    DMatch[] for_calculating;
    for_calculating = matches.toArray();
    for( int i = 0; i < Obj_descriptor.rows(); i++ )
    {   dist = for_calculating[i].distance;
        if( dist < min_dist ) min_dist = dist;
        if( dist > max_dist ) max_dist = dist;
    }

    System.out.print("\nInterval min_dist: " + min_dist + ", max_dist:" + max_dist);
    //-- Use only "good" matches (i.e. whose distance is less than 2.5*min_dist)
    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    double ratio_dist=2.5;
    ratio_dist = ratio_dist*min_dist;
    int i, iter = matches.toArray().length;
    matches.release();

    for(i = 0;i < iter; i++){
        if (for_calculating[i].distance <=ratio_dist)
            good_matches.addLast(for_calculating[i]);
    }
    System.out.print("\n done Good Matches");

    /*Necessary type conversion for drawing matches
    MatOfDMatch goodMatches = new MatOfDMatch();
    goodMatches.fromList(good_matches);
    Mat matches_scn_obj = new Mat();
    Features2d.drawKeypoints(Object_image, keypoint_object, new Mat(Object_image.rows(), keypoint_object.cols(), keypoint_object.type()), new Scalar(0.0D, 0.0D, 255.0D), 4);
    Features2d.drawKeypoints(Scene_image, keypoint_scene, new Mat(Scene_image.rows(), Scene_image.cols(), Scene_image.type()), new Scalar(0.0D, 0.0D, 255.0D), 4);
    Features2d.drawMatches(Object_image, keypoint_object, Scene_image, keypoint_scene, goodMatches, matches_scn_obj);
    SaveImage(matches_scn_obj,"drawing_good_matches.jpg");
    */

    if(good_matches.size() <= 6){
        ph_value = "7";
        System.out.println("Wrong Detection");
        return Scene_image;
    }
    else{
        //5. RANSAC thresholding for finding the optimum homography
        Mat outputImg = new Mat();
        LinkedList<Point> objList = new LinkedList<Point>();
        LinkedList<Point> sceneList = new LinkedList<Point>();

        List<org.opencv.core.KeyPoint> keypoints_objectList = keypoint_object.toList();
        List<org.opencv.core.KeyPoint> keypoints_sceneList = keypoint_scene.toList();

        //getting the object and scene points from good matches
        for(i = 0; i<good_matches.size(); i++){
            objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
            sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
        }
        good_matches.clear();
        MatOfPoint2f obj = new MatOfPoint2f();
        obj.fromList(objList);
        objList.clear();

        MatOfPoint2f scene = new MatOfPoint2f();
        scene.fromList(sceneList);
        sceneList.clear();

        float RANSAC_dist=(float)2.0;
        Mat hg = Calib3d.findHomography(obj, scene, Calib3d.RANSAC, RANSAC_dist);

        for(i = 0;i<hg.cols();i++) {
            String tmp = "";
            for ( int j = 0; j < hg.rows(); j++) {

                Point val = new Point(hg.get(j, i));
                tmp= tmp + val.x + " ";
            }
        }

        Mat scene_image_transformed_color = new Mat();
        Imgproc.warpPerspective(original_image, scene_image_transformed_color, hg, Object_image.size(), Imgproc.WARP_INVERSE_MAP);
        processing(scene_image_transformed_color, template_match);

        return outputImg;
    }
} }

而這部分需要6秒鍾才能在運行時實現-

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    double ratio_dist=2.5;
    ratio_dist = ratio_dist*min_dist;
    int i, iter = matches.toArray().length;
    matches.release();

    for(i = 0;i < iter; i++){
        if (for_calculating[i].distance <=ratio_dist)
            good_matches.addLast(for_calculating[i]);
    }
    System.out.print("\n done Good Matches");}

我當時想也許我可以使用NDK用C ++編寫這部分代碼,但我只是想確保語言是問題所在,而不是代碼本身。 請不要嚴格,第一個問題! 任何批評都非常感謝!

因此,問題在於logcat給了我錯誤的計時結果。 滯后是由於代碼后面的巨大高斯模糊。 我使用System.currentTimeMillis而不是System.out.print ,向我展示了該錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM