Ask Your Question
2

throwing exception in matching function

asked 2014-01-10 00:25:58 -0500

Aruna Vijay gravatar image

"OpenCV Error: Assertion failed ((globalDescIdx>=0) && (globalDescIdx < size())) in getLocalIdx, file opencv-2.4.7/modules/features2d/src/matchers.cpp, line 163 terminate called after throwing an instance of 'cv::Exception'"

I read that it happens because of the frame being empty so i make the code as below

    if(!**image.empty**())  // image from camera 
{ 
    detector.detect( image, kp_image );
    extractor.compute( image, kp_image, des_image );
            **if(!des_image.empty())**
    matcher.knnMatch(des_object, des_image, matches, 2);

    for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
    {
        if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
        {
            good_matches.push_back(matches[i][0]);
        }
    }

        if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
        {
            good_matches.push_back(matches[i][0]);
        }
    }

    //Draw only "good" matches
    //      drawMatches( object, kp_object, image, kp_image, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

    if (good_matches.size() >= 4)
    {
        for( int i = 0; i < good_matches.size(); i++ )
        {
            //Get the keypoints from the good matches
            obj.push_back( kp_object[ good_matches[i].queryIdx ].pt );
            scene.push_back( kp_image[ good_matches[i].trainIdx ].pt );
        }

        H = findHomography( obj, scene, CV_RANSAC );

        perspectiveTransform( obj_corners, scene_corners, H);

        cout << "X and Y  " <<  scene_corners[2].x << "  " <<scene_corners[2].y<< "\n";

     //Draw lines between the corners (the mapped object in the scene image )
      line( image, scene_corners[0] , scene_corners[1] , Scalar(0, 255, 0), 4 );
          line( image, scene_corners[1] , scene_corners[2] , Scalar( 0, 255, 0), 4 );  // vertical left top to bottom 

      line(image, scene_corners[2] , scene_corners[3] , Scalar( 0, 255, 0), 4 );



      line( image, scene_corners[3] , scene_corners[0] , Scalar( 0, 255, 0), 4 );
    }
    imshow( "good matches", image );

    key = waitKey(1);
}
   Still it throws the exception
edit retag flag offensive close merge delete

Comments

Hello did you find a solution? I'm afraid I'm experiencing the same problems.

ianni67 gravatar imageianni67 ( 2014-03-26 08:35:03 -0500 )edit

Hi, I'm think I'm having the same problems too. Any solutions?

JCrasher gravatar imageJCrasher ( 2015-02-05 10:33:01 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-06-02 11:10:18 -0500

sumitsk gravatar image

I got stuck with this too and it took me almost 3-4 hours to figure this out. When you apply knn match , make sure that number of features in both test and query image is greater than or equal tonumber of nearest neighbours in knn match.
say for example, we have this code:

Mat img1,img2,desc1,desc2;
vector<KeyPoint> kpt1,kpt2;

FAST(img1,kpt1,30,true) ;
FAST(img2,kpt1,30,true) ;

SurfDescriptorExtractor sfdesc1,sfdesc2;
sfdesc1.compute(img1,kpt1,desc1);
sfdesc2.compute(img2,kpt2,desc2);

FlannBasedMatcher matcher;
vector< vector<DMatch> > matches1,matches2;
matcher.knnMatch(desc1,desc2,matches1,2);

this code will return exception as in the post, just modify the code as show below:

if(kpt1.size()>=2 && kpt2.size()>=2) 
matcher.knnMatch(desc1,desc2,matches1,2);

this method worked for me ..!!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-01-10 00:25:58 -0500

Seen: 1,723 times

Last updated: Jun 02 '15