Ask Your Question

drawMatches bug

asked 2013-04-21 10:26:41 -0500

yes123 gravatar image

I think there is a bug in drawMatches.cpp

OpenCV/modules/features2d/src/draw.cpp line: 208

There are two assert:

int i1 = matches1to2[m].queryIdx;
int i2 = matches1to2[m].trainIdx;
CV_Assert(i1 >= 0 && i1 < static_cast<int>(keypoints1.size()));
CV_Assert(i2 >= 0 && i2 < static_cast<int>(keypoints2.size()));

The problem is that keypoints1 refers to the train image, while i1 referers to the queryIdx.

I believe it should be inverted:

int i1 = matches1to2[m].trainIdx;
int i2 = matches1to2[m].queryIdx;

I believe this is a commont mistake in OpenCV because almost every samples use the wrong order to call DescriptorMatcher::match in fact there is always this error in the documentation:

obj.push_back( kp_object[ good_matches[i].queryIdx ].pt );  //> It should be trainIdx
scene.push_back( kp_image[ good_matches[i].trainIdx ].pt );
edit retag flag offensive close merge delete


Hi! I have the same problem. Im making a real time object detector and for unknown reason, the trainIdx gets higher values than the train keypoints vector size (randomly), throwing an OpenCv Exception... I could not find any explanation about this...

aratzs gravatar imagearatzs ( 2013-06-07 02:59:23 -0500 )edit

@yes123, could you please explain more

kzbr93 gravatar imagekzbr93 ( 2017-12-07 17:47:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-07 12:24:01 -0500

yes123 gravatar image

I solved this by switching the train and query images in the call to drawMatches (use query first and train on the right)

edit flag offensive delete link more

Question Tools


Asked: 2013-04-21 10:26:41 -0500

Seen: 3,506 times

Last updated: Apr 21 '13