Ask Your Question
0

multiple object detenction 2d camera findHomography

asked 2014-10-11 06:54:33 -0600

gfx gravatar image

updated 2014-10-11 06:55:11 -0600

      if(imagess)
  {


      //Initialise the Wrapping Class for Surf()
      //FastFeatureDetector detector (600);
     // MserFeatureDetector detector (1000);

      OrbFeatureDetector detector (5000);
       // SurfFeatureDetector detector(60);
      //SiftFeatureDetector detector(2000);

      //detect : first param: Image, second param: vector (output)

        //vector<KeyPoint> keypoints1,keypoints2;

        detector.detect(riscontro,keypoints1);
        detector.detect(imagess,keypoints2);

      //Initialise wrapping class for descriptors computing using SURF() class.
        //FREAK.DescriptorExtractor extractor;
        OrbDescriptorExtractor extractor;
        //BriefDescriptorExtractor extractor;
        //SurfDescriptorExtractor extractor;
        //SiftFeatureDetector extractor;

      //Compute: Input:image, keypoints Output:descriptors


        extractor.compute(riscontro,keypoints1,descriptors1);
        extractor.compute(imagess,keypoints2,descriptors2);

        if (!descriptors1.empty() & !descriptors2.empty())
        {

      //Initialise BruteForceMatcher: For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each on (=brute)
        //cv::BFMatcher matcher(cv::NORM_L2, true);
          //FlannBasedMatcher matcher(FLANN_DISTANCE_CHECK);
        BFMatcher matcher(NORM_HAMMING2, true);//FlannBasedMatcher matcher; //BFMatcher matcher(NORM_L2);
        //std::vector<std::vector< DMatch > > matches;
        std::vector< DMatch > matches;
       // matcher.knnMatch(descriptors1,descriptors2, matches );
      //match: execute the matcher!
        matcher.match(descriptors1,descriptors2, matches);




       double max_dist = 0; double min_dist = 100;

        //-- Quick calculation of max and min distances between keypoints
        for( int ui = 0; ui < descriptors1.rows; ui++ )
        { double dist = matches[ui].distance;
          if( dist < min_dist ) min_dist = dist;
          if( dist > max_dist ) max_dist = dist;
        }


         std::vector< DMatch > good_matches;
        /* good_matches.reserve(matches.size());


         for (size_t i = 0; i < matches.size(); ++i)
         {
             if (matches[i].size() < 2)
                         continue;

             const DMatch &m1 = matches[i][0];
             const DMatch &m2 = matches[i][1];

             if(m1.distance <= nndrRatio * m2.distance)
             good_matches.push_back(m1);
         }*/


        //std::vector<vector<DMatch> > good_matches;
       //***** std::vector< DMatch > good_matches;
      /*  double RatioT = 0.75;
       //-- ratio Test
       for(int ai=0; ai<matches.size(); ai++)
       {
       if((matches[ai].size()==1)||(abs(matches[ai][0].distance/matches[ai][1].distance) < RatioT))
       {
           good_matches.push_back(matches[ai]);
        }
       }*/


       for( int ai = 0; ai < descriptors1.rows; ai++ )
        { if( matches[ai].distance  < 3*min_dist ) // <= max(2*min_dist, 0.02) )
            { good_matches.push_back(matches[ai]); }
        }


      //Draw the matches with drawMatches

        //findHomography(riscontro, imagess, CV_RANSAC);
       //for(int iw=0;iw<good_matches.size();iw++)
           drawMatches(riscontro,keypoints1,imagess,keypoints2,good_matches,target1, Scalar::all(-1), Scalar::all(-1),
                    vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
        //Size size(640,480);
        //resize(target1,target,size);//resize image

        //-- Localize the object
        std::vector<Point2f> obj;
        std::vector<Point2f> scene;

        for( unsigned int ki = 0; ki < good_matches.size(); ki++ )
        {
          //-- Get the keypoints from the good matches
          obj.push_back( keypoints1[ good_matches[ki].queryIdx ].pt );
          scene.push_back( keypoints2[ good_matches[ki].trainIdx ].pt );
          kglobalpoint.push_back( keypoints2[ good_matches[ki].trainIdx ].pt );
        }

        if (obj.size() >= 4){

        Mat H = findHomography( obj, scene, CV_LMEDS, 5.0 );


        //-- Get the corners from the image_1 ( the object to be "detected" )
        std::vector<Point2f> obj_corners(4);
        obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( riscontro.cols, 0 );
        obj_corners[2] = cvPoint( riscontro.cols, riscontro.rows ); obj_corners[3] = cvPoint( 0, riscontro.rows );
        std::vector<Point2f> scene_corners(4);

        perspectiveTransform( obj_corners, scene_corners, H);



        //-- Draw lines between the corners (the mapped object in the scene - image_2 )
        Point2f offset( (float)riscontro.cols, 0);
        line( target1, scene_corners[0] + offset, scene_corners[1] + offset, Scalar(0, 255, 0), 4 );
        line( target1, scene_corners[1] + offset, scene_corners[2] + offset, Scalar( 0, 255, 0), 4 );
        line( target1, scene_corners[2] + offset, scene_corners[3 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-10-11 08:31:30 -0600

updated 2014-10-29 17:52:03 -0600

The standard feature based approach has problems when there are multiple instances of the same object. One of your problems could be the computation of the good matches. If there are multiple objects, a feature from your template image will fit very well to several features in your image, so your algorithm will assign it rather randomly. That means if you have three instances, your input into the RANSAC will be only a third of the good matched you would have if you see only one instance. And also the ratio of inliers in the input data will be rather bad so that you could need much more tries than before.

edit flag offensive delete link more

Comments

That approach is therefore recommended?

gfx gravatar imagegfx ( 2014-10-11 12:27:49 -0600 )edit

@FooBar I was about to ask for precision here but I asked a question about the topic there if you are interested: http://answers.opencv.org/question/45888/detection-of-multiple-instance-of-object-with-bin/

Doombot gravatar imageDoombot ( 2014-10-29 13:01:30 -0600 )edit

Question Tools

Stats

Asked: 2014-10-11 06:54:33 -0600

Seen: 2,981 times

Last updated: Oct 29 '14