Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

throwing exception in matching function

"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