Sift matching C++ source code / using opencv library

Created Date : 2011.10
Language : C/C++
Tool : Microsoft Visual C++ 2008
Library & Utilized : OpenCV 2.3
Reference : SIFT reference
etc. : template Image, WebCam

I made SIFT matching program using OpenCV 2.3.
I was wondering how to know the object pose.
In the internet, there are many source about sift, surf. But most of code introduced about only descripter and matching. There is no code to find object pose.
So I made this code and I should disclose this code.

This code uses openCV functions very useful.
cvExtractSURF, cvFindHomography...

I made matching code to the class. Class file name is MareMatchingClass.h/cpp.
You can use my class in the source very easily.

1. Create Matching class
   CMareMatchingClass MMathing;   

2.Input PatchImg

3.Find PatchImg in the background img
   MMathing.GetObjectRectAndBestH(BackGroundImg, &rect4pt);

4.Drawing the rect(rect4pt).
5.Repeat, go to the 3.

The class is consist of like below process;
1. Extract Feature -> use cvExtractSURF function
2. Find Matching point
3. Select some feature in the mached feature points, randomly.
4. calculate Homography matrix. This is geometry relationship between patch and background image.
5. transform features in the patch image by Homography matrix.
6. compare the transformed features to the background features.
7. evaluate how much is the homography exact.
7. repeat 4~6 and select best H.

<source code>

I think the source code is not best.
There are still shortage the source code.
It would need futher improvemnet.
so I want to discuss with you. Please leave your valueable opinion.
Thank you.
Have a nice day~. ^^

Oh~ english is very difficult.....


  1. Code doesn't works as it works in your video,

    Why is that>?

  2. I cann't know your problem.
    What error did you meet?

  3. Anonymous15/8/12 01:34

    Hi Mare,your above code is very useful.But its not working Properly.I am working in Image mode. I have given 2 images(patch image and background image).The code fails in FindBestH().And if(inCount <= 5) returning 0.It can't draw line on background image.

    Can you please guide me.I can successfully compile the code.

  4. Could you send me your source code and image file?
    I will check what the problem is.
    My e-mail is or

    I guess the problem is lack of input feature or not approporiate input value.
    Thank you.

  5. This comment has been removed by the author.

  6. Thanks for the great tutorial and it is very helpful in my studies but there is a problem the problem is too slow almost running in 1 frame or 2 frames a per second. Is there anyway how to speed up the program?

    Thanks in advance!

    1. I think the source code is not complete.
      I made this code to study principle of SIFT.
      I want to help to others study.

  7. It is possible to use Mat image in the program?

    1. I made iplImage class base.
      If you want to Mat, a lot of parts will have to be modified.
      Thank you.

  8. Anonymous15/9/13 08:45

    i could not find the code hear, has it been removed?

  9. em.. I just checked down is possible or not.
    Click the linked words '< source code >' then page will move to
    And you can download.
    Thank you.

  10. Anonymous4/4/14 05:27

    oh man what is this!!

  11. to test the code which file should i open?
    thank you
    im beginner

  12. main.cpp is main source code.
    but if you use Visual Studio, you can open by project.
    xxx.sin file is project opner file.
    Thank you.


Post a Comment

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

(C, C++) TinyXML , xml read & write

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )