Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Okay, maybe let's first answer the KNN-question. If you have a matrix of feature vectors and corresponding labels, you can train your KNN-classifier as any other classifier with the train() method (http://docs.opencv.org/modules/ml/doc/k_nearest_neighbors.html#cvknearest-train), afterwards, you can use the findNearest() method (http://docs.opencv.org/modules/ml/doc/k_nearest_neighbors.html#cvknearest-find-nearest) for an unknown test-feature to predict the label by specifying how many k-nearest neighbors you want to consider (majority vote).

The response parameter is a one-dimensional matrix (i.e. just one row or one column) which contains the labels for your classes. For your problem, either choose (-1, 1) or (0,1) class labels, how you need to specify it, is explained here: http://docs.opencv.org/modules/ml/doc/statistical_models.html#cvstatmodel-train. For each descriptor one corresponding class label needs to be assigned.

So, and because you don't want to do this for all the features of one image, you don't use local features here, but global ones (otherwise your result will be very inaccurate!), i.e. you can use for example, global histograms features, or the popular GIST-feature (http://people.csail.mit.edu/torralba/code/spatialenvelope/). Alternatively (the state of the art), you can use local features (typically densely sampled) to form a Bag-of-(visual)-Words descriptor (or short: BoW, or BovW), see http://answers.opencv.org/question/8677/image-comparison-with-a-database/#8686 how to build it, OpenCV has also integrated BoW, see the corresponding classes at http://docs.opencv.org/modules/features2d/doc/object_categorization.html?highlight=bow#object-categorization.