11

我必须使用 OpenCV 检查两个图像在 java 中是否相似,我为此使用 OpenCV 并使用 ORB

这是我的主要课程

System.out.println("Welcome to OpenCV " + Core.VERSION);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);());
System.out.println(System.getProperty("user.dir"));
File f1 = new File(System.getProperty("user.dir") + "\\test.jpg");
File f2 = new File(System.getProperty("user.dir") + "\\test2.jpg");
MatchingDemo2 m = new MatchingDemo2();
m.mth(f1.getAbsolutePath(), f2.getAbsolutePath());

这是我的MatchingDemo2.java文件

public class MatchingDemo2 {

public void mth(String inFile, String templateFile){
        FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
       //Create descriptors

        //first image
       // generate descriptors

        //second image
       // generate descriptors



        System.out.println("size " + matches.size());
        //HOW DO I KNOW IF IMAGES MATCHED OR NOT ????

        //THIS CODE IS FOR CONNECTIONS BUT I AM NOT ABLE TO DO IT
        //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);
        int DIST_LIMIT = 80;
        List<DMatch> matchList = matches.toList();
        List<DMatch> matches_final = new ArrayList<DMatch>();
        for(int i=0; i<matchList.size(); i++){
            if(matchList.get(i).distance <= DIST_LIMIT){
                matches_final.add(matches.toList().get(i));
            } 
        }

        MatOfDMatch matches_final_mat = new MatOfDMatch();
        matches_final_mat.fromList(matches_final);
        for(int i=0; i< matches_final.size(); i++){
            System.out.println("Good Matchs "+ matches_final.get(i));
        }
}
}

但是当我检查 Good Matchs 时,我得到了这个

 size 1x328
 Good Matchs DMatch [queryIdx=0, trainIdx=93, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=1, trainIdx=173, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=2, trainIdx=92, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=3, trainIdx=80, imgIdx=0, distance=26.0]
 Good Matchs DMatch [queryIdx=5, trainIdx=164, imgIdx=0, distance=40.0]
 Good Matchs DMatch [queryIdx=6, trainIdx=228, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=7, trainIdx=179, imgIdx=0, distance=14.0]
 Good Matchs DMatch [queryIdx=8, trainIdx=78, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=9, trainIdx=166, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=10, trainIdx=74, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=11, trainIdx=245, imgIdx=0, distance=38.0]
 Good Matchs DMatch [queryIdx=12, trainIdx=120, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=13, trainIdx=244, imgIdx=0, distance=41.0]
 Good Matchs DMatch [queryIdx=14, trainIdx=67, imgIdx=0, distance=50.0]
 Good Matchs DMatch [queryIdx=15, trainIdx=185, imgIdx=0, distance=55.0]
 Good Matchs DMatch [queryIdx=16, trainIdx=97, imgIdx=0, distance=21.0]
 Good Matchs DMatch [queryIdx=17, trainIdx=172, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=18, trainIdx=354, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=19, trainIdx=302, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=20, trainIdx=176, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=21, trainIdx=60, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=22, trainIdx=72, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=23, trainIdx=63, imgIdx=0, distance=54.0]
 Good Matchs DMatch [queryIdx=24, trainIdx=176, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=25, trainIdx=49, imgIdx=0, distance=58.0]
 Good Matchs DMatch [queryIdx=26, trainIdx=77, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=27, trainIdx=302, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=28, trainIdx=265, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=29, trainIdx=67, imgIdx=0, distance=49.0]
 Good Matchs DMatch [queryIdx=30, trainIdx=302, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=31, trainIdx=265, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=32, trainIdx=73, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=33, trainIdx=67, imgIdx=0, distance=55.0]
 Good Matchs DMatch [queryIdx=34, trainIdx=283, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=35, trainIdx=145, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=36, trainIdx=71, imgIdx=0, distance=54.0]
 Good Matchs DMatch [queryIdx=37, trainIdx=167, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=38, trainIdx=94, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=39, trainIdx=88, imgIdx=0, distance=68.0]
 Good Matchs DMatch [queryIdx=40, trainIdx=88, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=41, trainIdx=179, imgIdx=0, distance=28.0]
 Good Matchs DMatch [queryIdx=42, trainIdx=64, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=43, trainIdx=223, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=44, trainIdx=80, imgIdx=0, distance=30.0]
 Good Matchs DMatch [queryIdx=45, trainIdx=196, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=46, trainIdx=52, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=47, trainIdx=93, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=48, trainIdx=187, imgIdx=0, distance=49.0]
 Good Matchs DMatch [queryIdx=49, trainIdx=179, imgIdx=0, distance=50.0]
 Good Matchs DMatch [queryIdx=50, trainIdx=283, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=51, trainIdx=171, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=52, trainIdx=302, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=53, trainIdx=67, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=54, trainIdx=15, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=55, trainIdx=173, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=56, trainIdx=302, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=57, trainIdx=47, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=58, trainIdx=187, imgIdx=0, distance=58.0]
 Good Matchs DMatch [queryIdx=59, trainIdx=344, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=60, trainIdx=164, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=61, trainIdx=125, imgIdx=0, distance=50.0]
 Good Matchs DMatch [queryIdx=62, trainIdx=77, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=63, trainIdx=22, imgIdx=0, distance=79.0]
 Good Matchs DMatch [queryIdx=64, trainIdx=82, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=65, trainIdx=93, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=66, trainIdx=241, imgIdx=0, distance=35.0]
 Good Matchs DMatch [queryIdx=67, trainIdx=80, imgIdx=0, distance=18.0]
 Good Matchs DMatch [queryIdx=68, trainIdx=179, imgIdx=0, distance=20.0]
 Good Matchs DMatch [queryIdx=69, trainIdx=242, imgIdx=0, distance=50.0]
 Good Matchs DMatch [queryIdx=70, trainIdx=80, imgIdx=0, distance=22.0]
 Good Matchs DMatch [queryIdx=71, trainIdx=179, imgIdx=0, distance=19.0]
 Good Matchs DMatch [queryIdx=72, trainIdx=92, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=73, trainIdx=94, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=74, trainIdx=173, imgIdx=0, distance=49.0]
 Good Matchs DMatch [queryIdx=75, trainIdx=94, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=76, trainIdx=94, imgIdx=0, distance=48.0]
 Good Matchs DMatch [queryIdx=77, trainIdx=92, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=78, trainIdx=80, imgIdx=0, distance=20.0]
 Good Matchs DMatch [queryIdx=80, trainIdx=119, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=81, trainIdx=228, imgIdx=0, distance=47.0]
 Good Matchs DMatch [queryIdx=82, trainIdx=179, imgIdx=0, distance=14.0]
 Good Matchs DMatch [queryIdx=83, trainIdx=227, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=84, trainIdx=84, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=85, trainIdx=245, imgIdx=0, distance=40.0]
 Good Matchs DMatch [queryIdx=86, trainIdx=58, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=87, trainIdx=14, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=88, trainIdx=187, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=89, trainIdx=185, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=90, trainIdx=178, imgIdx=0, distance=25.0]
 Good Matchs DMatch [queryIdx=91, trainIdx=220, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=92, trainIdx=205, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=93, trainIdx=60, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=94, trainIdx=44, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=95, trainIdx=16, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=96, trainIdx=157, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=97, trainIdx=135, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=98, trainIdx=60, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=99, trainIdx=344, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=100, trainIdx=77, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=101, trainIdx=95, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=102, trainIdx=72, imgIdx=0, distance=45.0]
 Good Matchs DMatch [queryIdx=103, trainIdx=134, imgIdx=0, distance=70.0]
 Good Matchs DMatch [queryIdx=104, trainIdx=154, imgIdx=0, distance=54.0]
 Good Matchs DMatch [queryIdx=105, trainIdx=208, imgIdx=0, distance=77.0]
 Good Matchs DMatch [queryIdx=106, trainIdx=73, imgIdx=0, distance=79.0]
 Good Matchs DMatch [queryIdx=107, trainIdx=72, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=108, trainIdx=64, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=109, trainIdx=72, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=110, trainIdx=365, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=111, trainIdx=148, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=112, trainIdx=81, imgIdx=0, distance=42.0]
 Good Matchs DMatch [queryIdx=113, trainIdx=56, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=114, trainIdx=162, imgIdx=0, distance=48.0]
 Good Matchs DMatch [queryIdx=115, trainIdx=56, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=116, trainIdx=120, imgIdx=0, distance=58.0]
 Good Matchs DMatch [queryIdx=117, trainIdx=72, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=118, trainIdx=92, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=119, trainIdx=131, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=120, trainIdx=72, imgIdx=0, distance=46.0]
 Good Matchs DMatch [queryIdx=121, trainIdx=74, imgIdx=0, distance=78.0]
 Good Matchs DMatch [queryIdx=122, trainIdx=94, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=123, trainIdx=72, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=124, trainIdx=134, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=125, trainIdx=72, imgIdx=0, distance=46.0]
 Good Matchs DMatch [queryIdx=126, trainIdx=15, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=127, trainIdx=72, imgIdx=0, distance=50.0]
 Good Matchs DMatch [queryIdx=128, trainIdx=93, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=129, trainIdx=68, imgIdx=0, distance=46.0]
 Good Matchs DMatch [queryIdx=130, trainIdx=205, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=131, trainIdx=187, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=132, trainIdx=72, imgIdx=0, distance=47.0]
 Good Matchs DMatch [queryIdx=133, trainIdx=220, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=134, trainIdx=289, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=135, trainIdx=82, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=136, trainIdx=93, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=137, trainIdx=244, imgIdx=0, distance=18.0]
 Good Matchs DMatch [queryIdx=138, trainIdx=244, imgIdx=0, distance=25.0]
 Good Matchs DMatch [queryIdx=139, trainIdx=92, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=140, trainIdx=244, imgIdx=0, distance=20.0]
 Good Matchs DMatch [queryIdx=141, trainIdx=93, imgIdx=0, distance=45.0]
 Good Matchs DMatch [queryIdx=142, trainIdx=93, imgIdx=0, distance=51.0]
 Good Matchs DMatch [queryIdx=143, trainIdx=94, imgIdx=0, distance=51.0]
 Good Matchs DMatch [queryIdx=144, trainIdx=94, imgIdx=0, distance=40.0]
 Good Matchs DMatch [queryIdx=145, trainIdx=93, imgIdx=0, distance=47.0]
 Good Matchs DMatch [queryIdx=146, trainIdx=244, imgIdx=0, distance=28.0]
 Good Matchs DMatch [queryIdx=147, trainIdx=172, imgIdx=0, distance=77.0]
 Good Matchs DMatch [queryIdx=148, trainIdx=170, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=149, trainIdx=261, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=150, trainIdx=228, imgIdx=0, distance=55.0]
 Good Matchs DMatch [queryIdx=151, trainIdx=179, imgIdx=0, distance=19.0]
 Good Matchs DMatch [queryIdx=152, trainIdx=227, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=153, trainIdx=107, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=154, trainIdx=174, imgIdx=0, distance=41.0]
 Good Matchs DMatch [queryIdx=155, trainIdx=283, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=156, trainIdx=254, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=157, trainIdx=185, imgIdx=0, distance=51.0]
 Good Matchs DMatch [queryIdx=158, trainIdx=178, imgIdx=0, distance=30.0]
 Good Matchs DMatch [queryIdx=159, trainIdx=278, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=160, trainIdx=91, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=161, trainIdx=148, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=162, trainIdx=157, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=163, trainIdx=373, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=164, trainIdx=226, imgIdx=0, distance=48.0]
 Good Matchs DMatch [queryIdx=165, trainIdx=278, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=166, trainIdx=283, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=167, trainIdx=196, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=168, trainIdx=344, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=169, trainIdx=157, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=170, trainIdx=144, imgIdx=0, distance=79.0]
 Good Matchs DMatch [queryIdx=171, trainIdx=154, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=172, trainIdx=211, imgIdx=0, distance=75.0]
 Good Matchs DMatch [queryIdx=173, trainIdx=279, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=174, trainIdx=211, imgIdx=0, distance=79.0]
 Good Matchs DMatch [queryIdx=175, trainIdx=220, imgIdx=0, distance=68.0]
 Good Matchs DMatch [queryIdx=176, trainIdx=218, imgIdx=0, distance=45.0]
 Good Matchs DMatch [queryIdx=177, trainIdx=289, imgIdx=0, distance=75.0]
 Good Matchs DMatch [queryIdx=178, trainIdx=223, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=179, trainIdx=57, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=180, trainIdx=36, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=181, trainIdx=111, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=182, trainIdx=93, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=183, trainIdx=137, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=184, trainIdx=157, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=185, trainIdx=72, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=186, trainIdx=172, imgIdx=0, distance=47.0]
 Good Matchs DMatch [queryIdx=187, trainIdx=279, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=188, trainIdx=72, imgIdx=0, distance=55.0]
 Good Matchs DMatch [queryIdx=189, trainIdx=96, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=190, trainIdx=220, imgIdx=0, distance=68.0]
 Good Matchs DMatch [queryIdx=191, trainIdx=93, imgIdx=0, distance=48.0]
 Good Matchs DMatch [queryIdx=192, trainIdx=279, imgIdx=0, distance=54.0]
 Good Matchs DMatch [queryIdx=193, trainIdx=157, imgIdx=0, distance=54.0]
 Good Matchs DMatch [queryIdx=194, trainIdx=91, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=195, trainIdx=278, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=196, trainIdx=220, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=197, trainIdx=74, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=198, trainIdx=93, imgIdx=0, distance=34.0]
 Good Matchs DMatch [queryIdx=199, trainIdx=81, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=200, trainIdx=93, imgIdx=0, distance=45.0]
 Good Matchs DMatch [queryIdx=201, trainIdx=90, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=202, trainIdx=93, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=203, trainIdx=94, imgIdx=0, distance=42.0]
 Good Matchs DMatch [queryIdx=204, trainIdx=93, imgIdx=0, distance=35.0]
 Good Matchs DMatch [queryIdx=205, trainIdx=94, imgIdx=0, distance=44.0]
 Good Matchs DMatch [queryIdx=206, trainIdx=90, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=207, trainIdx=179, imgIdx=0, distance=54.0]
 Good Matchs DMatch [queryIdx=208, trainIdx=92, imgIdx=0, distance=48.0]
 Good Matchs DMatch [queryIdx=209, trainIdx=91, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=210, trainIdx=119, imgIdx=0, distance=77.0]
 Good Matchs DMatch [queryIdx=211, trainIdx=227, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=212, trainIdx=186, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=213, trainIdx=96, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=214, trainIdx=77, imgIdx=0, distance=52.0]
 Good Matchs DMatch [queryIdx=215, trainIdx=372, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=216, trainIdx=334, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=217, trainIdx=278, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=218, trainIdx=325, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=219, trainIdx=188, imgIdx=0, distance=60.0]
 Good Matchs DMatch [queryIdx=220, trainIdx=340, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=221, trainIdx=72, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=222, trainIdx=278, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=223, trainIdx=221, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=224, trainIdx=339, imgIdx=0, distance=74.0]
 Good Matchs DMatch [queryIdx=225, trainIdx=155, imgIdx=0, distance=66.0]
 Good Matchs DMatch [queryIdx=226, trainIdx=278, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=227, trainIdx=165, imgIdx=0, distance=78.0]
 Good Matchs DMatch [queryIdx=228, trainIdx=279, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=229, trainIdx=355, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=231, trainIdx=69, imgIdx=0, distance=80.0]
 Good Matchs DMatch [queryIdx=232, trainIdx=278, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=233, trainIdx=65, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=234, trainIdx=93, imgIdx=0, distance=79.0]
 Good Matchs DMatch [queryIdx=235, trainIdx=203, imgIdx=0, distance=78.0]
 Good Matchs DMatch [queryIdx=236, trainIdx=159, imgIdx=0, distance=70.0]
 Good Matchs DMatch [queryIdx=237, trainIdx=93, imgIdx=0, distance=45.0]
 Good Matchs DMatch [queryIdx=238, trainIdx=172, imgIdx=0, distance=58.0]
 Good Matchs DMatch [queryIdx=239, trainIdx=374, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=240, trainIdx=278, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=241, trainIdx=223, imgIdx=0, distance=55.0]
 Good Matchs DMatch [queryIdx=242, trainIdx=365, imgIdx=0, distance=58.0]
 Good Matchs DMatch [queryIdx=243, trainIdx=91, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=244, trainIdx=238, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=245, trainIdx=299, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=246, trainIdx=289, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=247, trainIdx=93, imgIdx=0, distance=41.0]
 Good Matchs DMatch [queryIdx=249, trainIdx=5, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=250, trainIdx=93, imgIdx=0, distance=53.0]
 Good Matchs DMatch [queryIdx=251, trainIdx=93, imgIdx=0, distance=34.0]
 Good Matchs DMatch [queryIdx=252, trainIdx=97, imgIdx=0, distance=34.0]
 Good Matchs DMatch [queryIdx=253, trainIdx=93, imgIdx=0, distance=37.0]
 Good Matchs DMatch [queryIdx=254, trainIdx=174, imgIdx=0, distance=55.0]
 Good Matchs DMatch [queryIdx=255, trainIdx=91, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=256, trainIdx=81, imgIdx=0, distance=59.0]
 Good Matchs DMatch [queryIdx=257, trainIdx=92, imgIdx=0, distance=57.0]
 Good Matchs DMatch [queryIdx=258, trainIdx=212, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=259, trainIdx=119, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=260, trainIdx=228, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=261, trainIdx=119, imgIdx=0, distance=68.0]
 Good Matchs DMatch [queryIdx=263, trainIdx=266, imgIdx=0, distance=74.0]
 Good Matchs DMatch [queryIdx=264, trainIdx=319, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=265, trainIdx=157, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=266, trainIdx=365, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=267, trainIdx=341, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=268, trainIdx=303, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=269, trainIdx=313, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=271, trainIdx=350, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=272, trainIdx=313, imgIdx=0, distance=68.0]
 Good Matchs DMatch [queryIdx=278, trainIdx=267, imgIdx=0, distance=69.0]
 Good Matchs DMatch [queryIdx=280, trainIdx=223, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=281, trainIdx=267, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=283, trainIdx=334, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=284, trainIdx=313, imgIdx=0, distance=63.0]
 Good Matchs DMatch [queryIdx=285, trainIdx=78, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=286, trainIdx=312, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=287, trainIdx=271, imgIdx=0, distance=68.0]
 Good Matchs DMatch [queryIdx=288, trainIdx=170, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=289, trainIdx=278, imgIdx=0, distance=64.0]
 Good Matchs DMatch [queryIdx=290, trainIdx=282, imgIdx=0, distance=70.0]
 Good Matchs DMatch [queryIdx=291, trainIdx=91, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=292, trainIdx=334, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=293, trainIdx=80, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=294, trainIdx=92, imgIdx=0, distance=47.0]
 Good Matchs DMatch [queryIdx=295, trainIdx=301, imgIdx=0, distance=44.0]
 Good Matchs DMatch [queryIdx=297, trainIdx=220, imgIdx=0, distance=78.0]
 Good Matchs DMatch [queryIdx=298, trainIdx=374, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=300, trainIdx=329, imgIdx=0, distance=74.0]
 Good Matchs DMatch [queryIdx=302, trainIdx=285, imgIdx=0, distance=77.0]
 Good Matchs DMatch [queryIdx=305, trainIdx=271, imgIdx=0, distance=80.0]
 Good Matchs DMatch [queryIdx=307, trainIdx=350, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=308, trainIdx=320, imgIdx=0, distance=71.0]
 Good Matchs DMatch [queryIdx=309, trainIdx=163, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=310, trainIdx=170, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=311, trainIdx=357, imgIdx=0, distance=65.0]
 Good Matchs DMatch [queryIdx=312, trainIdx=320, imgIdx=0, distance=62.0]
 Good Matchs DMatch [queryIdx=314, trainIdx=342, imgIdx=0, distance=75.0]
 Good Matchs DMatch [queryIdx=315, trainIdx=162, imgIdx=0, distance=72.0]
 Good Matchs DMatch [queryIdx=316, trainIdx=239, imgIdx=0, distance=74.0]
 Good Matchs DMatch [queryIdx=317, trainIdx=171, imgIdx=0, distance=56.0]
 Good Matchs DMatch [queryIdx=318, trainIdx=244, imgIdx=0, distance=61.0]
 Good Matchs DMatch [queryIdx=319, trainIdx=369, imgIdx=0, distance=77.0]
 Good Matchs DMatch [queryIdx=320, trainIdx=346, imgIdx=0, distance=67.0]
 Good Matchs DMatch [queryIdx=322, trainIdx=158, imgIdx=0, distance=78.0]
 Good Matchs DMatch [queryIdx=325, trainIdx=92, imgIdx=0, distance=73.0]
 Good Matchs DMatch [queryIdx=326, trainIdx=236, imgIdx=0, distance=76.0]
 Good Matchs DMatch [queryIdx=327, trainIdx=162, imgIdx=0, distance=70.0]

对于相同的图像以及不同的图像,我得到的匹配数量是相同的,我真的很困惑吗?你能解释一下如何使用 OpenCV 比较两张图像并判断它们是否相似吗?

这是我想要达到的目标

4

2 回答 2

5

由于您使用的是 BRUTEFORCE 匹配器,因此您将始终从火车(包含查询的图像)中的查询(模板)中获得所有关键点描述符的最佳匹配。即:BRUTEFORCE 匹配器将始终找到 100% 匹配(列车描述符中所有查询关键点描述符的最佳等效关键点)。

这意味着您需要将匹配项过滤为正确匹配项(inliers)和不正确匹配项(outliers)匹配项。

你可以通过两种方式做到这一点

1.距离计算

使用 Andrey Smorodov 提到的距离。您可以使用此方法(但这并不总是提供正确的结果)

List<DMatch> matchesList = matches.toList();
Double max_dist = 0.0;
Double min_dist = 100.0;

for (int i = 0; i < matchesList.size(); i++) {
    Double dist = (double) matchesList.get(i).distance;
    if (dist < min_dist)
        min_dist = dist;
    if (dist > max_dist)
    max_dist = dist;
}

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
for (int i = 0; i < matchesList.size(); i++)  {  
    if (matchesList.get(i).distance <= (3 * min_dist)) // change the limit as you desire
    good_matches.addLast(matchesList.get(i));
}

2.确定掩码

您可以使用 findHomography 来获取可以让您清楚地找到内点和异常点的遮罩(因为它考虑了相机姿势透视,所以它几乎是正确的)

        LinkedList<Point> objList = new LinkedList<Point>();
        LinkedList<Point> sceneList = new LinkedList<Point>();

        List<KeyPoint> keypoints_RefList = keypointsRef.toList();
        List<KeyPoint> keypoints_List = keypoints.toList();

        for (int i = 0; i < good_matches.size(); i++) {
            objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
            sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
        }                 

        MatOfPoint2f obj = new MatOfPoint2f();
        MatOfPoint2f scene = new MatOfPoint2f();

        obj.fromList(objList);  
        scene.fromList(sceneList);  

        Mat mask = new Mat();
        Mat hg = Calib3d.findHomography(obj, scene, 8, 10,  mask);

现在掩码是 findHomography 中的一个可选输出,它是每个匹配的 1 或 0 值的数组。对应匹配的掩码值为 1,如果它是一个内部值,0 如果它是一个异常值。

您可以将此作为标准来决定您是否有几乎 90% 的掩码为 1,那么您可以让输出为真。

我用它来识别来自android java相机框架的特定对象并得到这些日志结果

08-22 01:08:38.929: I/OCVSample::Activity(25799): Keypoints Size: 1x477  KeypointsRef Size : 1x165
08-22 01:08:39.049: I/OCVSample::Activity(25799): descriptor Size: 32x477  descriptorRef Size : 32x165
08-22 01:08:39.129: I/OCVSample::Activity(25799): Matches Size: 1x165
08-22 01:08:39.129: I/OCVSample::Activity(25799): matchesList Size: 165
08-22 01:08:39.139: I/OCVSample::Activity(25799): Max dist : 460.44110107421875   Min dist : 100.0
08-22 01:08:39.139: I/OCVSample::Activity(25799): good matches size: 19
08-22 01:08:39.139: I/OCVSample::Activity(25799): obj size : 1x165
08-22 01:08:39.139: I/OCVSample::Activity(25799): scene size : 1x165
08-22 01:08:40.239: I/OCVSample::Activity(25799): Homography  mask size : 1x165
08-22 01:08:40.239: I/OCVSample::Activity(25799): Homography  mask : [1; 1; 1; 1; 1; 1; 1; 0; 1; 0; 1; 1; 1; 1; 0; 1; 1; 1; 0; 1; 1; 1; 1; 0; 1; 0; 1; 1; 1; 1; 1; 1; 0; 0; 1; 1; 1; 1; 0; 1; 0; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 1; 0; 1; 1; 0; 1; 0; 1; 0; 0; 1; 0; 1; 1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 0; 0; 1; 1; 0; 1; 1; 1; 1; 0; 1; 0; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 0; 0; 0; 1; 0; 1; 1; 1; 1; 0; 0; 1; 1; 1; 1; 1; 0; 1; 0; 1; 1; 0; 1; 0; 1; 1; 1; 1; 1; 1; 0; 1; 1; 0; 1; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 1; 0; 1; 1; 0; 0; 1; 1]

3.另一种更简单的方法是比较这两个图像的直方图,您可以使用compHist(); 此处显示的 openCV 功能, 也可参考 openCV 文档。

比较直方图的各种方法给出了从 0 到 1 或更高值的输出范围,该输出取决于直方图之间的相似性。在某些方法中小心 1 是 100% 正匹配,在某些其他方法中为 0。“对于卡方方法,低分表示比高分更好的匹配。完美匹配为 0,完全不匹配是无限的(取决于直方图的大小)。”

剩余:-两个完全不同的图像可以具有完全相同的直方图值。

提示:

1.现在关于 knnMatch 只需使用 matcher.knnMatch(); 以及用于输出的适当数据类型。

2.同样在

 matcher.match(query, train, matches);

查询 => 模板的关键点描述符,例如。一个球和

train => 包含相同球的图像的关键点描述符。查询描述符的数量小于火车描述符的数量,确保你做对了。

现在祝你好运。

于 2013-08-21T20:41:02.233 回答
2

Matcher 仅搜索关键点图的最近匹配。对于测量差异,您需要使用距离的(平均)总和(或其他度量)。

于 2013-08-19T07:17:00.463 回答