Draw Rectangle around objects which not matching

asked 2016-10-26 21:45:54 -0600

Karanbeer Kaur gravatar image

I am using below code snippet to compare two images.After compare, I want to draw rectangle around the object which are not matching.

     int retVal = 0;
     int notMactingCount = 0;
     long startTime = System.currentTimeMillis();

    // Load images to compare
    Mat img1 = Imgcodecs.imread(filename1, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    Mat img2 = Imgcodecs.imread(filename2, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    System.out.println(img1);

    // Declare key point of images
    MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
    MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
    Mat descriptors1 = new Mat();
    Mat descriptors2 = new Mat();

    // Definition of ORB key point detector and descriptor extractors
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

    // Detect key points
    detector.detect(img1, keypoints1);
    detector.detect(img2, keypoints2);

    // Extract descriptors
    extractor.compute(img1, keypoints1, descriptors1);
    extractor.compute(img2, keypoints2, descriptors2);

    // Definition of descriptor matcher
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING ); 

    // Match points of two images
    MatOfDMatch matches = new MatOfDMatch();
    System.out.println("Type of Image1= " + descriptors1.type() + ", Type of Image2= " + descriptors2.type());
    System.out.println("Cols of Image1= " + descriptors1.cols() + ", Cols of Image2= " + descriptors2.cols());
    System.out.println("keypoints1 of Image1= " + keypoints1.cols() + ", keypoints2 of Image2= " + keypoints2.cols());


    // Visualize the matches and save the visualization.

    if (descriptors2.cols() == descriptors1.cols()) {
        matcher.match(descriptors1, descriptors2 ,matches);

        // Check matches of key points
        DMatch[] match = matches.toArray();
        double max_dist = 0; double min_dist = 100;

        for (int i = 0; i < descriptors1.rows(); i++) {
            double dist = match[i].distance;
            if( dist < min_dist ) min_dist = dist;
            if( dist > max_dist ) max_dist = dist;
        }
         System.out.println("max_dist=" + max_dist + ", min_dist=" + min_dist);

        // Extract good images (distances are under 10)
        for (int i = 0; i < descriptors1.rows(); i++) {
            if (match[i].distance <= 10) {
                retVal++;
            }else{
                notMactingCount++;
            }
        }
        System.out.println("matching count=" + retVal+" notMactingCount="+notMactingCount);
    }

Please share code snippet to draw rectangle around the object which are not matching or any link reference

edit retag flag offensive close merge delete

Comments

do you understand, that it just matches keypoints between 2 images ?

there are no "objects" at all. (and so no "non-matched" objects)

imho, you misunderstood the use-case of the feature matching, it is not intended for classifying image content, only to find a pose.

berak gravatar imageberak ( 2016-10-27 02:12:08 -0600 )edit

@berak can you please suggest how to compare images, get objects and then draw rectangle for which objects are not matching

Karanbeer Kaur gravatar imageKaranbeer Kaur ( 2016-10-27 06:12:25 -0600 )edit

You need to find the contour of non- maching objects.... you can use the pose of maching object as a mask to filter the image.... the result was background and non-matching object ... if you first use background subtraction and then orb to find object and then the found object as mask the result is only non-matching object .... use findcontour and bounding rect.... the game is done!!

regards Giorgio

gfx gravatar imagegfx ( 2016-10-27 06:13:55 -0600 )edit

@gfx now I have two List of findContours. now how I can use these list to new Image mentioning by drawing rectangle around non-matching object.

Mat img1 = Imgcodecs.imread(filename1, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = Imgcodecs.imread(filename2, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);


ArrayList<MatOfPoint> contoursImg1 = new ArrayList<MatOfPoint>();
ArrayList<MatOfPoint> contoursImg2 = new ArrayList<MatOfPoint>();
Imgproc.findContours(img1, contoursImg1, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.findContours(img2, contoursImg2, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);

System.out.println("contours1=="+contoursImg1.size()+"contours2=="+contoursImg2.size());
Karanbeer Kaur gravatar imageKaranbeer Kaur ( 2016-10-31 22:14:17 -0600 )edit

subtract background for first, for second subtract all contour from your image .... the result is non matching object ... obviusly if background image != non maching ... so the background subtraction maybe a solution or not. Can you print an image? Or similar one?

gfx gravatar imagegfx ( 2017-07-12 08:36:34 -0600 )edit