Does someone know the link of example of SIFT implementation with OpenCV 2.2. regards,
SIFT in OpenCV Note that these were previously only available in the opencv contrib repo, but the patent expired in the year 2020. So they are now included in the main repo.
First, we have to construct a SIFT object and then use the function detectAndCompute to get the keypoints. It will return two values – the keypoints and the descriptors. View the code on Gist. Next, let's try and match the features from image 1 with features from image 2.
Below is a minimal example:
#include <opencv/cv.h> #include <opencv/highgui.h> int main(int argc, const char* argv[]) { const cv::Mat input = cv::imread("input.jpg", 0); //Load as grayscale cv::SiftFeatureDetector detector; std::vector<cv::KeyPoint> keypoints; detector.detect(input, keypoints); // Add results to image and save. cv::Mat output; cv::drawKeypoints(input, keypoints, output); cv::imwrite("sift_result.jpg", output); return 0; }
Tested on OpenCV 2.3
You can obtain the SIFT detector and SIFT-based extractor in several ways. As others have already suggested the more direct methods, I will provide a more "software engineering" approach that may make you code more flexible to changes (i.e. easier to change to other detectors and extractors).
Firstly, if you are looking to obtain the detector using built in parameters the best way is to use OpenCV"s factory methods for creating it. Here's how:
#include <opencv2/core/core.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/highgui/highgui.hpp> #include <vector> using namespace std; using namespace cv; int main(int argc, char *argv[]) { Mat image = imread("TestImage.jpg"); // Create smart pointer for SIFT feature detector. Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SIFT"); vector<KeyPoint> keypoints; // Detect the keypoints featureDetector->detect(image, keypoints); // NOTE: featureDetector is a pointer hence the '->'. //Similarly, we create a smart pointer to the SIFT extractor. Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SIFT"); // Compute the 128 dimension SIFT descriptor at each keypoint. // Each row in "descriptors" correspond to the SIFT descriptor for each keypoint Mat descriptors; featureExtractor->compute(image, keypoints, descriptors); // If you would like to draw the detected keypoint just to check Mat outputImage; Scalar keypointColor = Scalar(255, 0, 0); // Blue keypoints. drawKeypoints(image, keypoints, outputImage, keypointColor, DrawMatchesFlags::DEFAULT); namedWindow("Output"); imshow("Output", outputImage); char c = ' '; while ((c = waitKey(0)) != 'q'); // Keep window there until user presses 'q' to quit. return 0; }
The reason using the factory methods is flexible because now you can change to a different keypoint detector or feature extractor e.g. SURF simply by changing the argument passed to the "create" factory methods like this:
Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF"); Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SURF");
For other possible arguments to pass to create other detectors or extractors see: http://opencv.itseez.com/modules/features2d/doc/common_interfaces_of_feature_detectors.html#featuredetector-create
http://opencv.itseez.com/modules/features2d/doc/common_interfaces_of_descriptor_extractors.html?highlight=descriptorextractor#descriptorextractor-create
Now, using the factory methods means you gain the convenience of not having to guess some suitable parameters to pass to each of the detectors or extractors. This can be convenient for people new to using them. However, if you would like to create your own custom SIFT detector, you can wrap the SiftDetector object created with custom parameters and wrap it into a smart pointer and refer to it using the featureDetector smart pointer variable as above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With