ORB功能匹配检测错误图像

时间:2018-01-06 12:01:51

标签: android opencv knn

我正在尝试使用Android和Opencv制作货币检测应用程序,我正在使用ORB检测器和knnMatcher如下:

detector = FeatureDetector.create(FeatureDetector.ORB);    FastFeatureDetector.create(FastFeatureDetector.THRESHOLD,false,FastFeatureDetector.TYPE_9_16);
  matcher= BFMatcher.create(BFMatcher.BRUTEFORCE_HAMMING,false);
String fullPath = "/storage/emulated/0/Currency Resources Folder/"+"50-front.jpg";
 Mat blured_src = new Mat();
            Mat blured_test = new Mat();
            Mat resized_test = new Mat();
sizeToScale =1 ;
            Imgproc.resize(mGray,resized_test,new Size(mGray.width()*sizeToScale,mGray.height()*sizeToScale));
Imgproc.medianBlur(img_src,blured_src,3);
            Imgproc.medianBlur(resized_test,blured_test,3);

            Mat canny_src = new Mat();
            Mat canny_test = new Mat();
            Imgproc.Canny(blured_src,canny_src,50,200);
            Imgproc.Canny(blured_test,canny_test,50,200);
            MatOfKeyPoint kp_test = new MatOfKeyPoint();
            MatOfKeyPoint kp_src = new MatOfKeyPoint();
            detector.detect(blured_test,kp_test);
            detector.detect(blured_src,kp_src);
Mat desc_src = new Mat();
        Mat desc_test = new Mat();
        descriptorExtractor.compute(blured_src,kp_src,desc_src);
        descriptorExtractor.compute(blured_test,kp_test,desc_test);
List<MatOfDMatch> matches = new ArrayList<MatOfDMatch>();
        matcher.knnMatch(desc_test,desc_src,matches,2);
LinkedList<DMatch> good_matches = new LinkedList<>();

        for (Iterator<MatOfDMatch> iterator = matches.iterator(); iterator.hasNext();)
        {
            MatOfDMatch next = iterator.next();

            if (next.toArray()[0].distance / next.toArray()[1].distance < .92)
            {
                good_matches.add(next.toArray()[0]);
            }
        }
List<Point> pts1 = new ArrayList<Point>();
        List<Point> pts2 = new ArrayList<Point>();
        for(int i = 0; i<good_matches.size(); i++){
            pts1.add(kp_test.toList().get(good_matches.get(i).queryIdx).pt);
            pts2.add(kp_src.toList().get(good_matches.get(i).trainIdx).pt);
        }
 Mat outputMask = new Mat();
        MatOfPoint2f pts1Mat = new MatOfPoint2f();
        pts1Mat.fromList(pts1);
        MatOfPoint2f pts2Mat = new MatOfPoint2f();
        pts2Mat.fromList(pts2);
if (pts1Mat.toList().size() != 0 && pts2Mat.toList().size() != 0) {
            Mat Homog = Calib3d.findHomography(pts1Mat, pts2Mat, Calib3d.RANSAC, 15, outputMask, 2000, 0.995);

LinkedList<DMatch> better_matches = new LinkedList<DMatch>();
            for (int i = 0; i < good_matches.size(); i++) {
                if (outputMask.get(i, 0)[0] != 0.0) {
                    better_matches.add(good_matches.get(i));
                }
            }
MatOfDMatch matches_mat = new MatOfDMatch();
            matches_mat.fromList(better_matches);
MatOfByte mask = new MatOfByte(outputMask);
            Features2d.drawMatches(mGray, kp_test, img_src, kp_src, matches_mat, output,new Scalar(255,0,0),new Scalar(0,255,255),mask,Features2d.NOT_DRAW_SINGLE_POINTS);
            if (Imgcodecs.imwrite("/storage/emulated/0/Currency Resources Folder/match" + counter + ".jpg", output))
//////        if(SaveImage(output,"/storage/emulated/0/Currency Resources Folder/match"+counter+".jpg"))
            {
                Log.e(tag, "Match image saved");
            }
            Log.e(tag, "matches = " + better_matches.size());


            counter++;

return mRgba ;
}

我正面临着这些问题:

  1. 匹配列表总是40和60分的正确和错误 对象
  2. 匹配器检测我的键盘之类的虚假对象并产生一个 匹配的图像
  3. 我无法识别匹配器是否已检测到货币 纸或它只是检测到一个虚假对象(如我的电脑键盘)
  4. 我需要在货币周围放置一个边界并检测它 每次
  5. out put图像将更好地描述我想解释的内容 enter image description here

1 个答案:

答案 0 :(得分:0)

您需要修改检测器的阈值,以使其对背景噪声不太敏感。通过丢弃远距离匹配项,可以进一步提高检测器的性能。