4.875

4.9 | 8 ratings Rate this file 116 Downloads (last 30 days) File Size: 129.45 KB File ID: #21512
image thumbnail

kd-tree for matlab

by Andrea Tagliasacchi

 

22 Sep 2008 (Updated 01 Sep 2010)

A kd-tree mex lib which allows for nearest neighbor, k-nearest neighbor, range and ball queries

| Watch this File

File Information
Description

kdtree provides a minimalistic implementation of kd-tree. The implementation can be used either inside MATLAB by means of MEX calls, or as a standalone tool, directly from a C/C++ program. The image on the website has been creaed with "fulltest.m"

This implementation offers the following functionalities:
- kdtree_build: k-d tree construction O( n log^2(n) )
- kdtree_delete: frees memory allocated by kdtree
- kdtree_nearest_neighbor: nearest neighbor query (for one or more points)
- kdtree_k_nearest_neighbors: kNN for a single query point
- kdtree_range_query: rectangular range query
- kdtree_ball_query: queries samples withing distance delta from a point

For more information, please refer to the following page:
https://sites.google.com/site/andreatagliasacchi/software/matlabkd-treelibrary

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
k-D tree
This submission has inspired the following:
Kdtree implementation in matlab

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Requires a working MEX compiler. Runs on all platforms.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (20)
30 Sep 2008 Gary Tam

Thank you! Work out of the box.

03 Dec 2008 Luigi Giaccari

The most complete Kd-tree on Matlab exchange.
 Thank you Andrea

21 Jan 2009 KIgor Karic

For some reason kdtree_nearest_neighbor takes extremely long time to finish. Are others having the same problem or am I the only one?

03 Feb 2009 Andrea Tagliasacchi

Thanks Kigor, I found a simple bug in the kdtree_nearest_neighbor data conversion. The query was executed MxN times instead of M times; given the query matrix Q is of size(M,N).

The updated kdtree_nearest_neighbor.cpp file can be found at
https://latis.cs.sfu.ca/svn/users/ata2/matlibs/kdtree/

The bug will be corrected in the next release

07 Feb 2009 Bing Jian

How to use the result returned by kdtree_build()
which is a double number on my MATLAB7.5.0?
Does your program require at least MATLAB7.6?
Thanks!

30 Mar 2009 Andrea Tagliasacchi

kdtree_build() returns a pointer to the kdtree. That number needs to be fed as first argument to every other query method. I haven't tried it on older versions of Matlab but I haven't used any of the new releases functionality.

17 May 2009 Florian Brucker

Worked perfectly out of the box after compiling in MATLAB as specified by the docs. Thanks a lot!

One little thing: The help for kdtree_k_nearest_neighbors says that the neighbors are returned with increasing distance order. This seems to be incorrect, my experiments tell me they are returned in decreasing distance order (e.g. if the point itself is part of the tree, then it is returned as the last of the neighbors, not as the first).

27 Aug 2009 lin ming

great work!
note: compile with visual c++ 6.0 will cause fatal error on my computer. However, VS2008 works fine.

09 Sep 2009 sun jin

kdtree_build.cpp
D:\kdtree1.2\kdtree\KDTree.h(144) : error C2374: 'dIdx' : redefinition; multiple initialization
        D:\kdtree1.2\kdtree\KDTree.h(137) : see declaration of 'dIdx'
D:\kdtree1.2\kdtree\KDTree.h(225) : error C2371: 'i' : redefinition; different basic types
        D:\kdtree1.2\kdtree\KDTree.h(223) : see declaration of 'i'
 
  D:\MATLAB\R2008A\BIN\MEX.PL: Error: Compile of 'kdtree_build.cpp' failed.
 
??? Error using ==> mex at 207
Unable to complete successfully.

15 Oct 2009 Dominic

Thanks a lot for this contribution - I've been using it a lot recently and it just works perfectly :)

22 Oct 2009 Dominic

On my System (64bit Ubuntu, Matlab R2009a) there seems to be a memory leak with this implementation:

for i = 1:1000
tree = kdtree_build(rand(1000,1));
end

grabs around 140MB of system memory, according to top. That is never released again. Do I have to release the resources myself in some way or might this be a bug?
I really depend on this implementation, so you would do me a great favour by having a look at it...

26 Oct 2009 Dominic

Ok, Shame on me, there is a method

"kdtree_delete"

that needs to be called for releasing the memory again.

10 Feb 2010 Alastair Harrison

This is a really nice implementation. It's worked very well for me and is much faster than other implementations I've tried.

If I accidentally call kdtree_delete twice on the same handle, matlab quits immediately. Would it be possible for the library to maintain an internal list of handles to avoid this problem?

It would also allow implementation of a 'kdtree_delete_all' function, to free up all trees that have been created in a session. That's useful because if a piece of matlab code crashes before it gets to kdtree_delete then you can end up with an orphaned tree.

It would be nice to have a 'kdtree_delete_all' function to free up all trees that have been created. Of course, this would require a list of trees to be maintained within the library.

10 Feb 2010 Andrea Tagliasacchi

The easiest solution would be enclosing the methods in a Matlab class and let Matlab call the "delete" functions automatically.

I might work on it in the future and improve it to include this functionality as well. I suggest you to be more careful with your de-allocations in the meantime :)

15 Apr 2010 Georgios Evangelidis

Very nice and complete work! It worked perfectly. VC Express Ed. 2008 works fine. Thanks a lot!.

20 Apr 2010 Eval Bladimir Bacca Cortes

It works really nice, but I need something and I don't know how to do it efficiently. I need to update an existing kd-tree with new data, but without losing the actual indexes. Is that possible? Please could you help me? Thanks in advance for your time....

21 Apr 2010 Andrea Tagliasacchi

Hello Eval, that's a completely different problem, the underlying structure of this kd-tree is built to exploit the complete balancedness of the data. If you need a dynamic kd-tree then you would need to code it differently. By the way, why kd-tree? They are not exactly the best for this kind of task. Have you ever thought about using quad or octrees? Well, unless you have very high dimensional data...

17 Apr 2011 Duy Nguyen

Hello Mr.Andrea Tagliasacchi, your code is really great but i need some other things. I wonder how i can make the function nearest neighborhood get distances between corresponding pairs. I mean that function's output return not only indexes but also the distances. Thank you in advance for your time

21 Jun 2011 Prateek Rungta

Hi Andrea,
Thanks for the file, great work. Just a FYI: the tree doesn't appear to be correctly created in the event there are nan values in the data-set. In my tests, the points need to be at least 50-60% non-nan for the tree to work as expected.

10 Jan 2012 Jose

Great work! I'm using it in my thesis project and it works well. But I don't know how I can delete nodes in the tree meanwhile keep the tree balanced.

Please login to add a comment or rating.
Updates
24 Sep 2008

The makefile was implying an underlying .mexmaci initialization which is valid only for mac/OSX environments. The mexext program is now used to determine the extension of the build products according to your architecture.

12 Nov 2008

Corrected few bugs in parameter passing
Greatly enhanced the preprocessing speed (tree construction) 10x
Improved examples and makefile structure.
More info in "CHANGES"

08 Dec 2008

Updates some interface bugs and the bugs on distances (second argument) returned by the query functions.

01 Sep 2010

Redirected documentation to my personal page

Tag Activity for this File
Tag Applied By Date/Time
kdtree Andrea Tagliasacchi 22 Oct 2008 10:20:36
kd tree Andrea Tagliasacchi 22 Oct 2008 10:20:36
utilities Andrea Tagliasacchi 22 Oct 2008 10:20:36
miscellaneous Andrea Tagliasacchi 22 Oct 2008 10:20:36
utilities Cristina McIntire 12 Dec 2008 15:33:06
mathematics Andrea Tagliasacchi 16 Dec 2008 10:58:06
data structure Andrea Tagliasacchi 22 May 2009 14:42:17
spatial data structure Andrea Tagliasacchi 22 May 2009 14:42:24
kdtree_buildcpp sun jin 09 Sep 2009 02:42:17
kdtree_buildcpp Hyeokjune Jeon 11 Dec 2009 00:48:54
kd tree Hyeokjune Jeon 11 Dec 2009 00:48:57
range query Hyeokjune Jeon 11 Dec 2009 00:49:27

Contact us at files@mathworks.com