[OpenCV] SIFT implementation in OpenCV 2.4

#include "opencv2\opencv.hpp"

#include <stdio.h>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>

using namespace std;
using namespace cv;

//
int main(  )
{
        //source image
	char* img1_file = "C:/images/box.png";
	char* img2_file = "C:/images/box.png";

	// image read
	Mat tmp = cv::imread( img1_file, 1 );
	Mat in  = cv::imread( img2_file, 1 );

	/* threshold      = 0.04;
	   edge_threshold = 10.0;
	   magnification  = 3.0;	*/

	// SIFT feature detector and feature extractor
	cv::SiftFeatureDetector detector( 0.05, 5.0 );
	cv::SiftDescriptorExtractor extractor( 3.0 );

	/* In case of SURF, you apply the below two lines
	cv::SurfFeatureDetector detector();
	cv::SurfDescriptorExtractor extractor();
	*/

	// Feature detection
	std::vector keypoints1, keypoints2;
	detector.detect( tmp, keypoints1 );
	detector.detect( in, keypoints2 );

	// Feature display
	Mat feat1,feat2;
	drawKeypoints(tmp,keypoints1,feat1,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	drawKeypoints(in,keypoints2,feat2,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	imwrite( "feat1.bmp", feat1 );
	imwrite( "feat2.bmp", feat2 );
	int key1 = keypoints1.size();
	int key2 = keypoints2.size();
	printf("Keypoint1=%d \nKeypoint2=%d", key1, key2);

	// Feature descriptor computation
	Mat descriptor1,descriptor2;
	extractor.compute( tmp, keypoints1, descriptor1 );
	extractor.compute( in, keypoints2, descriptor2 );
	/*int desc1 = descriptor1.size;
	int desc2 = descriptor2.size;
	printf("Descriptor1=%d \nDescriptor2=%d", desc1, desc2);*/

	// corresponded points
	std::vector matches;

	// L2 distance based matching. Brute Force Matching
	BruteForceMatcher<L2 > matcher;

	// Flann-based matching
	//FlannBasedMatcher matcher;

	// display of corresponding points
	matcher.match( descriptor1, descriptor2, matches );

	// matching result
	Mat result;
	drawMatches( tmp, keypoints1, in, keypoints2, matches, result );

	// output file
	imwrite( "result.bmp", result );

	// display the result
	namedWindow("SIFT", CV_WINDOW_AUTOSIZE );
	imshow("SIFT", result);
	waitKey(0); //press any key to quit

	return 0;
}

Input image:box

Result:

sift_result

12 responses to this post.

  1. […] [OpenCV] SIFT implementation in OpenCV 2.4 […]

    Reply

  2. Posted by mezguich med amine on December 14, 2012 at 10:50 am

    I tried the code in code blocks but it does not work

    C:\Users\hp\Desktop\Test IMPL\Test6\main.cpp:37: error: missing template arguments before ‘keypoints1’

    Reply

    • Posted by Daniel on May 1, 2013 at 12:42 pm

      I had the same error code… problema with std::vector

      Reply

      • Posted by Daniel on May 1, 2013 at 1:07 pm

        Note: I am using Opencv 2.4.5

        Correct code:

        ————————————–

        #include “opencv2\opencv.hpp”

        #include
        #include
        #include
        #include
        #include

        using namespace std;
        using namespace cv;

        //
        int main( )
        {
        //source image
        //modify!!!!
        char* img1_file = “D:\\CodeBlocks\\Fotos\\fachada-do-predio.jpg”;
        char* img2_file = “D:\\CodeBlocks\\Fotos\\fachada-do-predio.jpg”;

        // image read
        Mat tmp = cv::imread( img1_file, 1 );
        Mat in = cv::imread( img2_file, 1 );

        /* threshold = 0.04;
        edge_threshold = 10.0;
        magnification = 3.0; */

        // SIFT feature detector and feature extractor
        cv::SiftFeatureDetector detector( 0.05, 5.0 );
        cv::SiftDescriptorExtractor extractor( 3.0 );

        //In case of SURF, you apply the below two lines
        //cv::SurfFeatureDetector detector();
        //cv::SurfDescriptorExtractor extractor();

        // Feature detection
        std::vector keypoints1, keypoints2;
        detector.detect( tmp, keypoints1 );
        detector.detect( in, keypoints2 );

        // Feature display
        Mat feat1,feat2;
        drawKeypoints(tmp,keypoints1,feat1,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
        drawKeypoints(in,keypoints2,feat2,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

        //modify!!!!
        imwrite( “D:\\CodeBlocks\\Fotos\\template_matching2.jpg”, feat1 ); //modify to use your img
        imwrite( “D:\\CodeBlocks\\Fotos\\template_matching2.jpg”, feat2 ); //modify to use your img

        int key1 = keypoints1.size();
        int key2 = keypoints2.size();
        printf(“Keypoint1=%d \nKeypoint2=%d”, key1, key2);

        // Feature descriptor computation
        Mat descriptor1,descriptor2;
        extractor.compute( tmp, keypoints1, descriptor1 );
        extractor.compute( in, keypoints2, descriptor2 );
        /*int desc1 = descriptor1.size;
        int desc2 = descriptor2.size;
        printf(“Descriptor1=%d \nDescriptor2=%d”, desc1, desc2);*/

        // corresponded points
        std::vector matches;

        // L2 distance based matching. Brute Force Matching
        BruteForceMatcher<L2 > matcher;

        // Flann-based matching
        //FlannBasedMatcher matcher;

        // display of corresponding points
        matcher.match( descriptor1, descriptor2, matches );

        // matching result
        Mat result;
        drawMatches( tmp, keypoints1, in, keypoints2, matches, result );

        // output file
        //modify!!!!
        imwrite( “D:\\CodeBlocks\\Fotos\\result.bmp”, result );

        // display the result
        namedWindow(“SIFT”, CV_WINDOW_AUTOSIZE );
        imshow(“SIFT”, result);
        waitKey(0); //press any key to quit

        return 0;
        }

        ————————————–

      • Posted by Daniel on May 1, 2013 at 1:10 pm

        OPS… Wrong Code…

        Here’s the correct one:

        #include “opencv2\opencv.hpp”

        #include
        #include
        #include
        #include
        #include

        using namespace std;
        using namespace cv;

        //
        int main( )
        {
        //source image
        char* img1_file = “D:\\CodeBlocks\\Fotos\\fachada-do-predio.jpg”;
        char* img2_file = “D:\\CodeBlocks\\Fotos\\fachada-do-predio.jpg”;

        // image read
        Mat tmp = cv::imread( img1_file, 1 );
        Mat in = cv::imread( img2_file, 1 );

        /* threshold = 0.04;
        edge_threshold = 10.0;
        magnification = 3.0; */

        // SIFT feature detector and feature extractor
        cv::SiftFeatureDetector detector( 0.05, 5.0 );
        cv::SiftDescriptorExtractor extractor( 3.0 );

        //In case of SURF, you apply the below two lines
        //cv::SurfFeatureDetector detector();
        //cv::SurfDescriptorExtractor extractor();

        // Feature detection
        std::vector keypoints1, keypoints2;
        detector.detect( tmp, keypoints1 );
        detector.detect( in, keypoints2 );

        // Feature display
        Mat feat1,feat2;
        drawKeypoints(tmp,keypoints1,feat1,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
        drawKeypoints(in,keypoints2,feat2,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
        imwrite( “D:\\CodeBlocks\\Fotos\\template_matching2.jpg”, feat1 );
        imwrite( “D:\\CodeBlocks\\Fotos\\template_matching2.jpg”, feat2 );
        int key1 = keypoints1.size();
        int key2 = keypoints2.size();
        printf(“Keypoint1=%d \nKeypoint2=%d”, key1, key2);

        // Feature descriptor computation
        Mat descriptor1,descriptor2;
        extractor.compute( tmp, keypoints1, descriptor1 );
        extractor.compute( in, keypoints2, descriptor2 );
        /*int desc1 = descriptor1.size;
        int desc2 = descriptor2.size;
        printf(“Descriptor1=%d \nDescriptor2=%d”, desc1, desc2);*/

        // corresponded points
        std::vector matches;

        // L2 distance based matching. Brute Force Matching
        BruteForceMatcher<L2 > matcher;

        // Flann-based matching
        //FlannBasedMatcher matcher;

        // display of corresponding points
        matcher.match( descriptor1, descriptor2, matches );

        // matching result
        Mat result;
        drawMatches( tmp, keypoints1, in, keypoints2, matches, result );

        // output file
        imwrite( “D:\\CodeBlocks\\Fotos\\result.bmp”, result );

        // display the result
        namedWindow(“SIFT”, CV_WINDOW_AUTOSIZE );
        imshow(“SIFT”, result);
        waitKey(0); //press any key to quit

        return 0;
        }

    • Posted by sookang on January 2, 2015 at 6:08 am

      It is also failed

      That’s why there are errors that part b’cuz of vector keypoint1,keypoint2; and vector matches and BruteForceMatcher<L2 > matcher; This is correct code

      Reply

  3. Posted by Bala on March 1, 2013 at 2:04 am

    Where can I find a perfectly working SIFT or SURF code for object detection/identification?
    I need the code to work like shown in http://www.aishack.in/2010/05/sift-scale-invariant-feature-transform/

    Reply

  4. Posted by Zara on August 26, 2013 at 11:16 am

    hello, if i want to use this for a nos. of images than what changes i have to do as in to call .xml file as input
    plz help
    thnk you

    Reply

  5. Posted by Nick on November 22, 2013 at 1:21 am

    First of all, make sure your code is correct before posting it.
    Line 37 should be std::vector keypoints1, keypoints2;
    Line 60 should be std::vector matches;
    Secondly, when I tried your code I got 734 keypoints and not 263 as it shows on your screenshots. Clearly, there are no bad matches to filter out as both images are the same. Technically each keypoint in the first image is matched with its identical keypoint in the second so there are no false positives, true negatives and unmatched keypoints. Could you please clarify what’s going on? I am a bit confused.

    Reply

  6. Posted by r on June 18, 2014 at 4:57 pm

    thanks

    Reply

  7. can i get python code of same . please help

    Reply

Leave a reply to r Cancel reply