Ask Your Question
0

SurfDescriptorExtractor is undefined- Error- OpenCV

asked 2019-03-27 04:39:53 -0500

Rakshita gravatar image

Hi, I am trying to build my video stabilization code with OpenCV 3.4.2 using Visual Studio 15 on Windows. But facing the error SurfDescriptorExtractor is undefined.Kindly suggest a solution. My code is:-

VideoCapture cap("test.avi");

Mat currImg, colorImg, outImg, grayImg, backupColorImg;
int winSize = 20;
int maxCorners = 200;

double qualityLevel = 0.01;
double minDistance = 5.0;
int blockSize = 3;

int frameW, frameH;

cap.read(colorImg);
cvtColor(colorImg, grayImg, CV_BGR2GRAY);
currImg = grayImg.clone();

outImg = colorImg.clone();
int fps = 25;
frameW = grayImg.cols;
frameH = grayImg.rows;

Mat ref;
cap.read(ref);
cvtColor(ref, ref, CV_BGR2GRAY);

SurfDescriptorExtractor extractor;

int minHessian = 400;
vector<KeyPoint> keyPointRef;
SurfFeatureDetector detector(minHessian);
Mat descriptorRef;


detector.detect(ref(Range(0, ref.rows), Range::all()), keyPointRef);
extractor.compute(ref, keyPointRef, descriptorRef);

VideoWriter writeVideo("result.avi", 0, fps, cvSize(frameW, frameH), false);

namedWindow("Stabilize", 0);
namedWindow("GoodMatches", 0);

while (1)
{
    bool bScuccess = cap.read(colorImg);

    if (!bScuccess)
    {
        cout << "Cannot read the frame form video file";
        break;
    }

    cvtColor(colorImg, grayImg, CV_BGR2GRAY);
    currImg = grayImg.clone();
    backupColorImg = colorImg.clone();

    vector<KeyPoint> keyPointCurr;
    Mat descriptorCurr;

    detector.detect(currImg, keyPointCurr);
    extractor.compute(currImg, keyPointCurr, descriptorCurr);

    FlannBasedMatcher matcher;
    vector<DMatch> matches;
    matcher.match(descriptorCurr, descriptorRef, matches);

    double maxDist = 0, minDist = 100;
    for (int i = 0; i < descriptorCurr.rows; i++)
    {
        double dist = matches[i].distance;

        if (dist < minDist) minDist = dist;
        if (dist > maxDist) maxDist = dist;
    }

    vector<DMatch>good_matches;
    for (int i = 0; i < descriptorCurr.rows; i++)
    {
        //if (matches[i].distance <= max(2*minDist, 0.02))
        if (matches[i].distance < 0.2)
        {
            good_matches.push_back(matches[i]);
        }
    }

    vector<Point2f>curPoint;
    vector<Point2f>refPoint;

    for (int i = 0; i < good_matches.size(); i++)
    {
        curPoint.push_back(keyPointCurr[good_matches[i].queryIdx].pt);
        refPoint.push_back(keyPointRef[good_matches[i].trainIdx].pt);
    }

    Mat imgMatches;
    drawMatches(currImg, keyPointCurr, ref, keyPointRef, good_matches, imgMatches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    imshow("GoodMatches", imgMatches);

    //Mat transformMatrix = estimateGlobalMotionRobust(curPoint,refPoint, 3);

    Mat transformMatrix = estimateRigidTransform(curPoint, refPoint, false); // false = rigid transform, no scaling/shearing

    cout << transformMatrix << endl;
    warpAffine(colorImg, outImg, transformMatrix, Size(frameW, frameH));
    //warpPerspective(colorImg,outImg,transformMatrix,Size(frameW,frameH));

    writeVideo.write(outImg);
    imshow("Input", colorImg);
    imshow("Stabilize", outImg);

    if (waitKey(20) == 27)
    {
        cout << "ESC key is pressed by user" << endl;
        break;
    }

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-03-27 04:44:39 -0500

berak gravatar image

your code is from outdated opencv2.4, you can't use the part with the feature description / matching, api has changed.

please look at current tutorials for this.

then, you can only use SIFT or SURF if your cv2 was build with the opencv_contrib modules (xfeatures2d) and IF OPENCV_ENABLE_NONFREE=ON while building. (and no, it's OFF in the PIP version)

edit flag offensive delete link more

Comments

sidenote: if you use 8U descriptors, like ORB or BRIEF, you'll also have to change the matching to BFMatcher(NORM_HAMMING) or use an LSH index for flann.

berak gravatar imageberak ( 2019-03-27 04:47:38 -0500 )edit

Hi, thanks for prompt reply. I have tried building opencv including contrib modules and NONFREE enabled but still the error remain same.

Rakshita gravatar imageRakshita ( 2019-03-27 06:21:14 -0500 )edit

please read the answer again, you also have to change your code.

surf = cv2.xfeatures2d.SURF_create()

etc.

berak gravatar imageberak ( 2019-03-27 07:30:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-27 04:39:53 -0500

Seen: 109 times

Last updated: Mar 27 '19