ORB/BruteForce-drawing matches when there are none

asked 2016-05-17 13:53:40 -0500

patri gravatar image

I'm trying to write a program that uses ORB algorithm to detect and compute the keypoints of an image and a video and matches descriptor vectors using BruteForce matcher. The issue I am facing is, that every time I run the program on Visual C++, when the object that I'm trying to detect is not visible, my algorithm is drawing all the supposed matching lines between the keypoints detected(it matches all the keypoints). When the object that I'm trying to detect appears in the image I don't face this issue, in fact, I hardly get any mismatches.

This is a brief sequence of the main test:

• convert input image to grayscale

• convert input videos to grayscale

• detect keypoints and extract descriptors from input grayscale image

• detect keypoints and extract descriptors from input grayscale videos

• match descriptors(see below)

BFMatcher matcher(NORM_HAMMING);
vector<DMatch> matches;
matcher.match(descriptors_1, descriptors_2, matches);

double max_dist = 0; double min_dist = 100;

////calcularea distantelor max si min distances intre keypoints
for (int i = 0; i < descriptors_1.rows; i++)
    double dist = matches[i].distance;
    if (dist < min_dist) min_dist = dist;
    if (dist > max_dist) max_dist = dist;

printf("-- Max dist : %f \n", max_dist);
printf("-- Min dist : %f \n", min_dist);

std::vector< DMatch > good_matches;

for (int i = 0; i < descriptors_1.rows; i++)
    if (matches[i].distance <= max(2 * min_dist, 0.02))

////-- Desenarea matches-urilor "bune"
Mat img_matches;
drawMatches(img1, keypoints_1, cadruProcesat, keypoints_2,
    good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
    vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

////-- Afisare matches
imshow("good matches", img_matches);
int gm = 0;
for (int i = 0; i < (int)good_matches.size(); i++)
    printf("-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx);
    gm += 1;

//imshow(windowName2, cadruProcesat);

switch (waitKey(10)) {

case 27:
    //tasta 'esc' a fost apasata(ASCII 27) 
    return 0;

Please help me find the problem.

edit retag flag offensive close merge delete



When object is outside of image what is value of min_dist? I think that will be always true :

if (matches[i].distance <= max(2 * min_dist, 0.02))
LBerger gravatar imageLBerger ( 2016-05-17 15:01:00 -0500 )edit

So I've changed the value of min_dist to 15(not 100) and it seems that it works very well for patterns... but this was done by trial and error. Now I have one question: when I'm trying to recognize my face, the keypoints that were found in the video aren't uniformly distributed, they are mostly where there are high changes in the intensity... Do you have any idea how can I make a uniform distribution of them?

patri gravatar imagepatri ( 2016-05-18 12:32:03 -0500 )edit

Try using this post

LBerger gravatar imageLBerger ( 2016-05-19 14:08:33 -0500 )edit