szakeri
szakeri

Reputation: 159

OpenCV's Brute Force Matcher crashes on second iteration

I am working on an OpenCV project where I am attempting to use a combination of the ORB feature detector, the BRISK feature descriptor, and the Brute Force Matcher to detect, match and track features through a video sequence provided by my webcam.

At the moment, everything is working fine for single images. I can highlight an area on the screen, extract it as an ROI, detect its features and match them back to the first frame of my video. However, my problem arises when I try to compute this process on a video sequence.

camera >> cameraFrame;
cv::cvtColor(cameraFrame, greyFrame, cv::COLOR_BGR2GRAY);

BriskMatching::briskMatcher.findFrameFeatures(greyFrame, mask);
BriskMatching::briskMatcher.computeFrameDescriptors(greyFrame);

        if(BriskMatching::briskMatcher.getFirstFrame())
        {
            BriskMatching::briskMatcher.findImgFeatures(imgToMatch_1C);
            BriskMatching::briskMatcher.setFirstFrame(false);
            clearMask.copyTo(mask);
            BriskMatching::briskMatcher.computeImgDescriptors(imgToMatch_1C);
        }
    }

    BriskMatching::briskMatcher.match();
    BriskMatching::briskMatcher.mMatches.clear(); 
    cv::drawMatches(imgToMatch_1C, BriskMatching::briskMatcher.mImgORBFeatures, greyFrame, BriskMatching::briskMatcher.mFrameORBFeatures, BriskMatching::briskMatcher.mMatches, matchesImg, cv::Scalar(255, 255, 255));
    cv::imshow("Matches", matchesImg);

The program crashes at run-time when I call BriskMatching::briskMatcher.match();, but only after the first iteration of the program loop. Below is the code contained in the briskMatcher.match() function...

void BriskMatching::match()
{
    mBfMatcher.match(mImgDescriptors,mFrameDescriptors, mMatches);
}

This in combination with the memory exception error I get leads me to believe that there is a problem with one of the three containers the match function is trying to use, primarily mMatches which is an std::vector<cv::KeyPoint> I try to clear the vector by calling mMatches.clear(); before it is used again, and although the vector is cleared I still get the crash at runtime.

Does anyone have any insight or suggestions on what may be causing my crash? I've been messing with it for some time now, and it's starting to get quite frustrating.

Upvotes: 4

Views: 732

Answers (1)

Isra-MiAn
Isra-MiAn

Reputation: 71

I tried to do the same with you, I have a program with a lot of detector, extractors and matching algorithm and I had the same problem as you.

In my case I decided to use ORB detector and SIFT extractor method if I would use the Knn-Matcher or FLANN-Matcher and it works OK, but when I implement the BFMatcher with this method I couldn't do it. Finally I decided to use ORB extractor with BFMatcher and it works.

My code is like this:

 else if(botonORBisPressed){
         OrbFeatureDetector detector;
         for (int i=0; i<2; i++) {
             detector.detect(gray_image[i], keypoints[i]);
         }

         //Extractor method depends on Matcher.
         if (botonBFPulsado == true) {
             Ptr <DescriptorExtractor> extractor = DescriptorExtractor::create("ORB");
             if(!extractor)
             {
                 cout << "Error creating feature descriptor" << endl;
                 getchar();
             }

             for (int i=0; i<2; i++) {
                 (* extractor).compute( gray_image[i], keypoints[i], descriptors[i] );
             }
         }

         if ((botonFlannisPressed == true) || (botonKnnisPressed == true)) {
             //Use SIFT algorithm to do the matching
             SiftDescriptorExtractor extractor;
             for (int i=0 ; i<2 ; i++) {
                 extractor.compute(gray_image[i], keypoints[i],descriptors[i]);
             }
         }
     }

If you want to use the extractor-SIFT method I recommend you use other matcher algorithm such I did, however if you prefer you can do the same with me (depends on the matching method use one or other extractor) and you will compare the results.

Upvotes: 1

Related Questions