Does openCV SurfFeatureDetector unnecessarily extract descriptors internally? - c++

I just wondered, if using a SurfFeatureDetector to detect keypoints and a SurfDescriptorExtractor to extract the SURF descriptors (see code below as described here) wouldn't extract the descriptors twice.
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints;
detector.detect( img, keypoints ); //detecting keypoints, extracting descriptors without returning them
SurfDescriptorExtractor extractor;
Mat descriptors;
extractor.compute( img, keypoints, descriptors ); // extracting descriptors a second time
The openCV documentation says, those 2 classes are wrappers for the SURF() class.
The SURF::operator() is overloaded, one version taking just a keypoint vector, the other one additionally taking a vector for the descriptors.
What intrigues me... both then call the cvExtractSURF() function, which seems to extract the descriptors, no matter what... (I did not dive too deep into the C code as I find it hard to understand, so maybe I'm wrong)
But this would mean that the SurfFeatureDetector would extract descriptors without returning them. Using the SurfDescriptorExtractor in the next step just does it a second time, which seems very inefficient to me. But am I right?

You can be assured that detector does not actually compute the descriptors. The key statement to look at is line 687 of surf.cpp if( !descriptors ) continue; Features are not computed during detection, the way it should be. This kind of architecture is most likely due to the fact that surf code was "added" to OpenCV after it was designed/developed to work by itself.
As a background: note that detector and feature extractors are different things. You first "detect" points using SurfFeatureDetector where local features are extracted (using SurfDescriptorExtractor). The snippet you have is a good guide.

Related

Feature detectors and descriptor takes significantly longer to compute in the first instance

I am using OpenCV's Feature2D::detectAndCompute() to compute the keypoints and descriptors of images.
The code basically look like this:
Ptr<cv::ORB> orb = cv::ORB::create();
Mat descriptors1, descriptors2...;
vector<KeyPoint> keypoints1, keypoints2...;
orb->detectAndCompute(img1, noArray(), keypoints1, descriptors1); // first instance
orb->detectAndCompute(img2, noArray(), keypoints2, descriptors2); // second instance
// more detectAndCompute...
My problem is that it takes significantly longer to do the first detectAndCompute(~0.25s) than all the detectAndCompute after it(~0.002s each). Why is this happening?
Also, regardless of how small the images are. The first detectAndCompute always takes 0.25s. Is there any way to cut down this time?
Thank you!

OPencv C++ Concatenate/merge/insert two vectors of type Mat

I am trying to concatenate vectors and am running into some issues. Using the insert function I was able to combine two vectors of type std::vector<keyPoint> but run into issues when attempting to do the same process but to vectors of type std::vector<Mat>.
Te error is No matching member function for call to 'begin, 'end'
Code is as follows
std::vector<KeyPoint> kp1, kp2;
std::vector<Mat> desc1, desc2;
std::vector<KeyPoint> keypoints;
std::vector<Mat> descriptors;
//Add keypoints and descriptors found to master list
keypoints.insert(keypoints.end(),kp1.begin(),kp1.end());
keypoints.insert(keypoints.end(),kp2.begin(),kp2.end());
descriptors.insert(descriptors.end(),desc1.begin(),desc1.end());
descriptors.insert(descriptors.end(),desc2.begin(),desc2.end());
Looking for a solution or a work-around.
Thanks for any help in advance.

Feature Detection Opencv

I am a beginner in OpenCV feature detection and matching. I am trying to write one simple method to detect and display the detected feature with an image.Here is the method that I have written:
void FeatureDetection::detectSimpleFeature(Mat image){
Mat gray_image, output_image;
cvtColor(image,gray_image,CV_BGR2GRAY);
// Detect Keypoints using Simple Detector
Ptr<Feature2D> detectors;
vector<KeyPoint> keypoint_1;
detectors->detect(image, keypoint_1);
drawKeypoints(gray_image, keypoint_1, output_image, Scalar::all(-1), DrawMatchesFlags:: DEFAULT);
namedWindow("Keypoints Detection");
imshow("Keypoints Detection", output_image);
waitKey(0);
}
There is no compile time error in this function, but while running, the program is crashing.Can anyone please help?
I am also searching special types of detector like SURF, SIFT etc but could not find in my downloaded and built library. Please suggest !!!

Fiducial markers - OpenCV - Feature Detection & Matching

Would somebody share their knowledge of OpenCV feature detection and extraction of fiducial markers?
I'm attempting to find a fiducial marker (see image below) (self-created ARTag-style using MS Paint) in a scene.
Using Harris corner detection, I can adequately locate the corners of the marker image. Similarly, using Harris corner detection, I can find most of the corners of the marker in the scene. I then use SIFT to extract descriptors for the marker image and the scene image. Then I've tried both BF and FLANN for feature matching. However, both matching algorithms tend to match the wrong corners together.
Is there something that I can do to improve the accuracy? Or are there other detection methods that would be better appropriate for this application?
Portion of code:
GoodFeaturesToTrackDetector harris_detector(6, 0.15, 10, 3, true);
vector<KeyPoint> keypoints1, keypoints2;
harris_detector.detect(im1, keypoints1);
harris_detector.detect(im2, keypoints2);
SiftDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute( im1, keypoints1, descriptors1 );
extractor.compute( im2, keypoints2, descriptors2 );
BFMatcher matcher;
//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors1, descriptors2, matches );
you can try to use ORB detector, which is a fusion of FAST keypoint detector and BRIEF descriptor. it is fast and better than BRIEF descriptor because the later does not compute the orientation.
you can found a example of orb's usage in samples/cpp/tutorial_code/features2D/AKAZE_tracking or enter link description here
or there is a python project which does the similar task as yours fiducial

OpenCV: ‘SiftDescriptorExtractor’ was not declared in this scope

I am new to OpenCV in general and have been trying out a number of examples over the past few days. I've successfully gotten Harris corner detections to work on some test images. My next test was to see if I could match two images based on the harris detections using SIFT.
Here is how I found harris corners:
GoodFeaturesToTrackDetector harris_detector(1000, 0.01, 10, 3, true);
vector<KeyPoint> keypoints1, keypoints2;
harris_detector.detect(img1, keypoints1);
harris_detector.detect(img2, keypoints2);
This works well. With my next goal of matching the features between img1 and img2, I try to use SIFT. However, when I try to declare an extractor for SIFT:
SiftDescriptorExtractor extractor;
I get the following error:
error: ‘SiftDescriptorExtractor’ was not declared in this scope
SiftDescriptorExtractor extractor;
What am I doing wrong?
Thanks in advance.
Make sure you have #include <features2d.hpp>.
In some versions of OpenCV, Sift are in <opencv2/nonfree/features2d.hpp>.