Saturday, March 3, 2012

SURF Source code

When you install openCV, openCV samples folder also gets installed. The samples will be present in your InstallationDirectory/samples/C. For me it's present in OpenCV-2.1.0/samples/c.This folder contains the sample codes of many good openCV programmes that can be used for a wide variety of purposes. One more thing is that, it also contains the compiled object files along with the source code for each programme. The programme , we will be looking is find_obj.cpp and the compiled code will be with the name find_obj. This programme uses SURF to do an object detection.

I have modified the code to make it work for recognition. I did my summer project at IIT Kharagpur, and there we performed the experiments on face recognition with SURF. For the first time in face recognition history, we worked with color FERET database. We wrote a paper, describing our approach and writing the results of our work. The Research paper was sent to the proceedings of an international conference. Till that gets published, I cannot share my work. But it's so simple and intuitive, when you look at the code I mentioned. Best of luck with your work.

Update (29/06/12) :
I will release the modified source code on or before 8th July

Thursday, March 1, 2012

SURF - Algorithm for matching

Many people talk about SURF and using it for recognition. But no one actually tells you, how it is used or what might be the algorithm for doing it. This series of posts, will detail you of using it practically. If you followed my previous posts, understanding this would be a lot more easier. You can skip the next paragraph. For noobs they have to read the next paragraph.

This post is made, assuming that you have openCV installed in your computer. If you haven't, go to my previous post for Installing openCV. To practice some openCV programming go to the assignments . If you don't know anything of openCV and are a complete absolute beginner, then go to the Easy navigation and start reading all the posts in the serial order. Most of them are practicals though to get your hands dirty. Also, if you have an open source OS, then this post will be more straight forward; although it isn't necessary. I know that many people use it in matlab. The code can be easily ported if you are well versed with C.

Let me start with giving a quick overview of SURF, so that understanding certain parts of the code we are going to write, will be easier to you. SURF stands for Speeded up Robust Features. In a given image, SURF tries to find the interest points - The points where the variance is maximum. It then constructs a 64-variable vector around it to extract the features (A 128-variable one is also possible, but it would consume more time for matching). In this way, there will be as many descriptors in an image as there are interest points. Now, when we use SURF for matching, we follow the below steps for every probe image (The image we need to match against)

1. Take a descriptor of the probe image
2. Compare this with all the descriptors of the gallery image (One of the set of possible matching images)
3. Find the nearest descriptor which is the one with the lowest distance to our descriptor compared to all the other descriptors
4. The nearest descriptor distance is stored
5. Take a descrptor other than the one's already taken from probe image and go to step-2
6. Now all the nearest descriptor distances are added, and the sum is divided with the total number of probe descriptors. This gives us the average distance
7. This average distance, along with the name of the gallery image we just matched, can be outputted to a file
8. For every gallery image, go to step-1
9. When all the gallery images get over, sort the distances in the outputted file and the one with the lowest distance is the best match for our probe image

There is already a function in openCV called cvExtractSURF to extract the SURF features of images. But there is no function to directly compare two images using SURF and give their distance. In the next post, we will be talking about doing this