Error matching with ORB in Android

后端 未结 2 2001
耶瑟儿~
耶瑟儿~ 2020-12-03 04:20

My code is working good but when it extracts keypoints, it matches poorly the two images. Here you can find my code, but I don\'t know how to draw good matched in JAVA

2条回答
  •  醉酒成梦
    2020-12-03 04:34

    Your code should be like this:

     FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
     DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);;
     DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    
     //first image
     Mat img1 = Highgui.imread("");
     Mat descriptors1 = new Mat();
     MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
    
     detector.detect(img1, keypoints1);
     descriptor.compute(img1, keypoints1, descriptors1);
    
     //second image
     Mat img2 = Highgui.imread("");
     Mat descriptors2 = new Mat();
     MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
    
     detector.detect(img2, keypoints2);
     descriptor.compute(img2, keypoints2, descriptors2);
    
     //matcher should include 2 different image's descriptors
     MatOfDMatch  matches = new MatOfDMatch();             
     matcher.match(descriptors1,descriptors2,matches);
     //feature and connection colors
     Scalar RED = new Scalar(255,0,0);
     Scalar GREEN = new Scalar(0,255,0);
     //output image
     Mat outputImg = new Mat();
     MatOfByte drawnMatches = new MatOfByte();
     //this will draw all matches, works fine
     Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches, 
     outputImg, GREEN, RED,  drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
    

    Also if you want to display just features, you can add this code:

     Mat featuredImg = new Mat();
     Scalar kpColor = new Scalar(255,159,10);//this will be color of keypoints
     //featuredImg will be the output of first image
     Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);
     //featuredImg will be the output of first image
     Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);
    

    Then you can show matched points like this:

      Bitmap imageMatched = Bitmap.createBitmap(outputImg.cols(), outputImg.rows(), Bitmap.Config.RGB_565);//need to save bitmap
      Utils.matToBitmap(outputImg, imageMatched);
      ImageView.setImageBitmap(imageMatched);
    

    Eventually you can implement good matches. I hope this thread will be helpful.

提交回复
热议问题