I want to write a MATLAB program for simple object recognition using bag of features. In short, I want to first extract the features from an image, create a visual library using those features, then cluster up the features belonging to one part together, hence creating different parts. now use these parts for matching. i have the basic idea but i don't know much math behind this, also i don't know how i can go about implementing it. can anyone help me with the matlab code for this?

  • $\begingroup$ could u provide images ? $\endgroup$
    – vini
    Commented Apr 8, 2012 at 7:00
  • $\begingroup$ Might help: dsp.stackexchange.com/questions/1433/… $\endgroup$ Commented Apr 9, 2012 at 8:10
  • $\begingroup$ The brief explanation you gave - encompasses the entire science and art of Pattern Recognition. It can't be answered unless you be specific. Also - "How to do X in Matlab" is not the right etiquette here. $\endgroup$ Commented Apr 17, 2012 at 8:53
  • $\begingroup$ There is now support for bag of features in the Computer Vision System Toolbox: mathworks.com/help/vision/ug/… I wish I could make this an answer. $\endgroup$
    – Dima
    Commented Jan 13, 2015 at 18:12

2 Answers 2


I'm not sure stackexchange is the platform to just give you the code, what I reccomend you to start with is the feature extraction, download vl_sift package for matlab and get the features from each of your images.

Next step,is quantize the features: You can get hundred of features for each image, you need to decide how many are enough for your task and then cluster them, and these are your final features. (You need to quantize them because 2 features - even if describe the same object, might be slightly different. You want two very close features to be represented by the same descriptor).

Now, after you have feaures, you need to represent your image by its features, for bag of words the common representation is usually histograms.

Then you can compare 2 images representations to decide if they are the same. This stage can be done in many ways, an easy option is an SVM classifier, download lib_svm package, it is very easy to use. Please note that SVM can decide between 2 classes, if your task is "recognition" then you might need to use multi class SVM.

Again, start with vl_sift, that will give you a good sense of what you need to do.



The, for now free available computer vision book, by Richard Szeliski contains an excellent introduction to this algorithm. Page 612.


I answered a similar question here on DSP with some example code.


Not the answer you're looking for? Browse other questions tagged or ask your own question.