Wednesday, February 15, 2012

Opencv knnSearch example

After searching through the web and going through the FLANN api I could not find a decent example that showcases the K nearest neighbor search capability. I found that OpenCV also has a wrapper written around flann so checked if there exists a simple example. To my dismay I could not find one. So, I spent couple of hours and put together the following code. Hope someone looking to use flann with opencv will find this example useful!

The example generates data randomly centered around a mean and a variance of arbitrary dimensions(provided by user, default is 1) and generates query data with similar properties and invokes the single query and batch knnSearch function calls and prints out. Just to let you know its not a class, its just a simple example to help people use knnSearch function in openCV. Enjoy!

code: cvKnn.cpp
Command to compile the code: g++ cvKnn.cpp -L /usr/lib -lopencv_core -lopencv_flann -o cvKnn

11 comments:

Don said...

Thanks, this saved me a lot of time searching!!

Peter Chervenski said...

Thanks!

夯新聞 said...

Thanks a lot!

Daniyar Kurmankhojayev said...

Thanks a lot. Very useful.

stepo said...

Thanks a lot! THe OpenCV docs are useless here. This is exactly what I was looking for ;)

Ricardo Eugenio González Valenzuela said...

Goodmorning. I would also like to download the code, but when I click over the link it appears the following message: "Internal Server Error". I was wondering if you could, please, be able to check it? or could you e-mail me the code (r.gonzalez.valenzuela@gmail.com)?
Thank you very much!

Felipe Bombardelli said...

Thanks Manohar!!!!

Powel Talwar said...

Thanks a lot.

stackoverflow said...

Thanks for the code. I couldn't find a similar example in python. Does OpenCV exposes its flann API in python?

yannick said...

Hi, the link is broken!!
Many thanks

Emma Liu said...

thank you, that's super helpful!