public void IndexFiles(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null) { BKTree<CEDDTreeNode> ceddtree = new BKTree<CEDDTreeNode>(); double[] ceddDiscriptor = null; int totalFileCount = imageFiles.Length; CEDD cedd = new CEDD(); for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Bitmap bmp = new Bitmap(Image.FromFile(fi.FullName))) { ceddDiscriptor = cedd.Apply(bmp); } CEDDTreeNode ceddTreeNode = new CEDDTreeNode { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, CEDDDiscriptor = ceddDiscriptor }; ceddtree.add(ceddTreeNode); IndexBgWorker.ReportProgress(i); } CEDDRepository<BKTree<CEDDTreeNode>> repo = new CEDDRepository<BKTree<CEDDTreeNode>>(); repo.Save(ceddtree); CacheHelper.Remove("CeddIndexTree"); }
public void BKTree_should_FindBestDistance() { BKTree <TestNode> tree = new BKTree <TestNode>(); TestNode search = new TestNode(new int[] { 118, 223, 316 }); TestNode best = new TestNode(3, new int[] { 120, 220, 320 }); tree.add(new TestNode(1, new int[] { 100, 200, 300 })); tree.add(new TestNode(2, new int[] { 110, 210, 310 })); tree.add(best); tree.add(new TestNode(4, new int[] { 130, 230, 330 })); tree.add(new TestNode(5, new int[] { 140, 240, 340 })); Assert.Equal(9, DistanceMetric.calculateLeeDistance(search.Data, best.Data)); Assert.Equal(9, tree.findBestDistance(search)); }
public void BKTree_should_FindBestDistance() { BKTree<TestNode> tree = new BKTree<TestNode>(); TestNode search = new TestNode(new int[] { 118, 223, 316 }); TestNode best = new TestNode(3, new int[] { 120, 220, 320 }); tree.add(new TestNode(1, new int[] { 100, 200, 300 })); tree.add(new TestNode(2, new int[] { 110, 210, 310 })); tree.add(best); tree.add(new TestNode(4, new int[] { 130, 230, 330 })); tree.add(new TestNode(5, new int[] { 140, 240, 340 })); Assert.Equal(9, DistanceMetric.calculateLeeDistance(search.Data, best.Data)); Assert.Equal(9, tree.findBestDistance(search)); }
public void IndexFiles(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null) { BKTree <CEDDTreeNode> ceddtree = new BKTree <CEDDTreeNode>(); double[] ceddDiscriptor = null; int totalFileCount = imageFiles.Length; CEDD cedd = new CEDD(); for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Bitmap bmp = new Bitmap(Image.FromFile(fi.FullName))) { ceddDiscriptor = cedd.Apply(bmp); } CEDDTreeNode ceddTreeNode = new CEDDTreeNode { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, CEDDDiscriptor = ceddDiscriptor }; ceddtree.add(ceddTreeNode); IndexBgWorker.ReportProgress(i); } CEDDRepository <BKTree <CEDDTreeNode> > repo = new CEDDRepository <BKTree <CEDDTreeNode> >(); repo.Save(ceddtree); CacheHelper.Remove("CeddIndexTree"); }
public void BKTree_should_ThrowUponAddingNullNode() { BKTree <TestNode> tree = new BKTree <TestNode>(); tree.add(new TestNode(1, new int[] { 100, 200, 300 })); tree.add(new TestNode(2, new int[] { 110, 210, 310 })); tree.add(new TestNode(3, new int[] { 130, 230, 330 })); tree.add(new TestNode(4, new int[] { 140, 240, 340 })); Assert.ThrowsDelegate boom = delegate { tree.add(null); }; Assert.Throws <NullReferenceException>(boom); }
/* * To use BKTree: * 1. Create a class dervied from BKTreeNode * 2. Add a member variable of your data to be sorted / retrieved * 3. Override the calculateDistance method to calculate the distance metric * between two nodes for the data to be sorted / retrieved. * 4. Instantiate a BKTree with the type name of the class created in (1). */ static void Main(string[] args) { /* * NOTE: More comprehensive examples of BK-Tree methods in unit tests */ // Exercise static distance metric methods -- just because Console.WriteLine( DistanceMetric.calculateHammingDistance( new byte[] { 0xEF, 0x35, 0x20 }, new byte[] { 0xAD, 0x13, 0x87 })); Console.WriteLine( DistanceMetric.calculateLeeDistance( new int[] { 196, 105, 48 }, new int[] { 201, 12, 51 })); Console.WriteLine( DistanceMetric.calculateLevenshteinDistance( "kitten", "sitting")); // Create BKTree with derived node class from top of file BKTree<ExampleNodeRecord> tree = new BKTree<ExampleNodeRecord>(); // Add some nodes tree.add( new ExampleNodeRecord( 1, new int[] {100,200,300}) ); tree.add( new ExampleNodeRecord( 2, new int[] {110,210,310}) ); tree.add( new ExampleNodeRecord( 3, new int[] {120,220,320}) ); tree.add( new ExampleNodeRecord( 4, new int[] {130,230,330}) ); tree.add( new ExampleNodeRecord( 5, new int[] {140,240,340}) ); // Get best node from our tree with best distance Dictionary<ExampleNodeRecord, Int32> results = tree.findBestNodeWithDistance( new ExampleNodeRecord( new int[] { 103, 215, 303 }) ); // Get best nodes below threshold results = tree.query( new ExampleNodeRecord(new int[] { 103, 215, 303 }), 10 ); // arbitrary threshold // Dictionaries don't print well; so invent your own handy print routine }
/* * To use BKTree: * 1. Create a class dervied from BKTreeNode * 2. Add a member variable of your data to be sorted / retrieved * 3. Override the calculateDistance method to calculate the distance metric * between two nodes for the data to be sorted / retrieved. * 4. Instantiate a BKTree with the type name of the class created in (1). */ static void Main(string[] args) { /* * NOTE: More comprehensive examples of BK-Tree methods in unit tests */ // Exercise static distance metric methods -- just because Console.WriteLine( DistanceMetric.calculateHammingDistance( new byte[] { 0xEF, 0x35, 0x20 }, new byte[] { 0xAD, 0x13, 0x87 })); Console.WriteLine( DistanceMetric.calculateLeeDistance( new int[] { 196, 105, 48 }, new int[] { 201, 12, 51 })); Console.WriteLine( DistanceMetric.calculateLevenshteinDistance( "kitten", "sitting")); // Create BKTree with derived node class from top of file BKTree <ExampleNodeRecord> tree = new BKTree <ExampleNodeRecord>(); // Add some nodes tree.add(new ExampleNodeRecord(1, new int[] { 100, 200, 300 })); tree.add(new ExampleNodeRecord(2, new int[] { 110, 210, 310 })); tree.add(new ExampleNodeRecord(3, new int[] { 120, 220, 320 })); tree.add(new ExampleNodeRecord(4, new int[] { 130, 230, 330 })); tree.add(new ExampleNodeRecord(5, new int[] { 140, 240, 340 })); // Get best node from our tree with best distance Dictionary <ExampleNodeRecord, Int32> results = tree.findBestNodeWithDistance( new ExampleNodeRecord(new int[] { 103, 215, 303 })); // Get best nodes below threshold results = tree.query( new ExampleNodeRecord(new int[] { 103, 215, 303 }), 10); // arbitrary threshold // Dictionaries don't print well; so invent your own handy print routine }
public void BKTree_should_FindBestNode() { BKTree <TestNode> tree = new BKTree <TestNode>(); TestNode search = new TestNode(new int[] { 210, 175, 233 }); TestNode best = new TestNode(2, new int[] { 200, 200, 200 }); tree.add(new TestNode(1, new int[] { 100, 100, 100 })); tree.add(best); tree.add(new TestNode(3, new int[] { 300, 300, 300 })); tree.add(new TestNode(4, new int[] { 400, 400, 400 })); tree.add(new TestNode(5, new int[] { 500, 500, 500 })); TestNode found = tree.findBestNode(search); Assert.Equal(2, found.Id); Assert.Equal(best.Data, found.Data); }
public void BKTree_should_FindBestNodeWithDistance() { BKTree <TestNode> tree = new BKTree <TestNode>(); TestNode search = new TestNode(new int[] { 365, 422, 399 }); TestNode best = new TestNode(4, new int[] { 400, 400, 400 }); tree.add(new TestNode(1, new int[] { 100, 100, 100 })); tree.add(new TestNode(2, new int[] { 200, 200, 200 })); tree.add(new TestNode(3, new int[] { 300, 300, 300 })); tree.add(best); tree.add(new TestNode(5, new int[] { 500, 500, 500 })); Dictionary <TestNode, Int32> result = tree.findBestNodeWithDistance(search); Assert.Equal(1, result.Count); Assert.Equal(58, DistanceMetric.calculateLeeDistance(search.Data, best.Data)); Assert.Equal(58, result.Values.ElementAt(0)); Assert.Equal(4, result.Keys.ElementAt(0).Id); Assert.Equal(best.Data, result.Keys.ElementAt(0).Data); }
public void BKTree_should_QueryBestMatchesBelowGivenThreshold() { BKTree <TestNode> tree = new BKTree <TestNode>(); TestNode search = new TestNode(new int[] { 399, 400, 400 }); TestNode best1 = new TestNode(41, new int[] { 400, 400, 400 }); TestNode best2 = new TestNode(42, new int[] { 403, 403, 403 }); TestNode best3 = new TestNode(43, new int[] { 406, 406, 406 }); tree.add(new TestNode(1, new int[] { 100, 100, 100 })); tree.add(new TestNode(2, new int[] { 200, 200, 200 })); tree.add(new TestNode(3, new int[] { 300, 300, 300 })); tree.add(best1); tree.add(best2); tree.add(new TestNode(5, new int[] { 500, 500, 500 })); Dictionary <TestNode, Int32> results; // Query for match within distance of 1 (best1 is only expected result) results = tree.query(search, 1); Assert.Equal(1, results.Count); Assert.Equal(1, DistanceMetric.calculateLeeDistance(search.Data, best1.Data)); Assert.Equal(1, results.Values.ElementAt(0)); Assert.Equal(41, results.Keys.ElementAt(0).Id); Assert.Equal(best1.Data, results.Keys.ElementAt(0).Data); // Query for match within distance of 10 (best1 & best2 are expected results) tree.add(best3); // exercise adding another node after already queried results = tree.query(search, 10); Assert.Equal(2, results.Count); Assert.Equal(1, DistanceMetric.calculateLeeDistance(search.Data, best1.Data)); Assert.Equal(10, DistanceMetric.calculateLeeDistance(search.Data, best2.Data)); Assert.True(results.Contains(new KeyValuePair <TestNode, int>(best1, 1))); Assert.True(results.Contains(new KeyValuePair <TestNode, int>(best2, 10))); // Query for matches within distance of 20 (best1, best2 & best3 are expected results) results = tree.query(search, 20); Assert.Equal(3, results.Count); Assert.Equal(1, DistanceMetric.calculateLeeDistance(search.Data, best1.Data)); Assert.Equal(10, DistanceMetric.calculateLeeDistance(search.Data, best2.Data)); Assert.Equal(19, DistanceMetric.calculateLeeDistance(search.Data, best3.Data)); Assert.True(results.Contains(new KeyValuePair <TestNode, int>(best1, 1))); Assert.True(results.Contains(new KeyValuePair <TestNode, int>(best2, 10))); Assert.True(results.Contains(new KeyValuePair <TestNode, int>(best3, 19))); }
public void BKTree_should_FindBestNode() { BKTree<TestNode> tree = new BKTree<TestNode>(); TestNode search = new TestNode(new int[] { 210, 175, 233 }); TestNode best = new TestNode(2, new int[] { 200, 200, 200 }); tree.add(new TestNode(1, new int[] { 100, 100, 100 })); tree.add(best); tree.add(new TestNode(3, new int[] { 300, 300, 300 })); tree.add(new TestNode(4, new int[] { 400, 400, 400 })); tree.add(new TestNode(5, new int[] { 500, 500, 500 })); TestNode found = tree.findBestNode(search); Assert.Equal(2, found.Id); Assert.Equal(best.Data, found.Data); }
public void BKTree_should_ThrowUponAddingNullNode() { BKTree<TestNode> tree = new BKTree<TestNode>(); tree.add(new TestNode(1, new int[] { 100, 200, 300 })); tree.add(new TestNode(2, new int[] { 110, 210, 310 })); tree.add(new TestNode(3, new int[] { 130, 230, 330 })); tree.add(new TestNode(4, new int[] { 140, 240, 340 })); Assert.ThrowsDelegate boom = delegate { tree.add(null); }; Assert.Throws<NullReferenceException>(boom); }
public void BKTree_should_QueryBestMatchesBelowGivenThreshold() { BKTree<TestNode> tree = new BKTree<TestNode>(); TestNode search = new TestNode(new int[] { 399, 400, 400 }); TestNode best1 = new TestNode(41, new int[] { 400, 400, 400 }); TestNode best2 = new TestNode(42, new int[] { 403, 403, 403 }); TestNode best3 = new TestNode(43, new int[] { 406, 406, 406 }); tree.add(new TestNode(1, new int[] { 100, 100, 100 })); tree.add(new TestNode(2, new int[] { 200, 200, 200 })); tree.add(new TestNode(3, new int[] { 300, 300, 300 })); tree.add(best1); tree.add(best2); tree.add(new TestNode(5, new int[] { 500, 500, 500 })); Dictionary<TestNode, Int32> results; // Query for match within distance of 1 (best1 is only expected result) results = tree.query(search, 1); Assert.Equal(1, results.Count); Assert.Equal(1, DistanceMetric.calculateLeeDistance(search.Data, best1.Data)); Assert.Equal(1, results.Values.ElementAt(0)); Assert.Equal(41, results.Keys.ElementAt(0).Id); Assert.Equal(best1.Data, results.Keys.ElementAt(0).Data); // Query for match within distance of 10 (best1 & best2 are expected results) tree.add(best3); // exercise adding another node after already queried results = tree.query(search, 10); Assert.Equal(2, results.Count); Assert.Equal(1, DistanceMetric.calculateLeeDistance(search.Data, best1.Data)); Assert.Equal(10, DistanceMetric.calculateLeeDistance(search.Data, best2.Data)); Assert.True(results.Contains(new KeyValuePair<TestNode, int>(best1, 1))); Assert.True(results.Contains(new KeyValuePair<TestNode, int>(best2, 10))); // Query for matches within distance of 20 (best1, best2 & best3 are expected results) results = tree.query(search, 20); Assert.Equal(3, results.Count); Assert.Equal(1, DistanceMetric.calculateLeeDistance(search.Data, best1.Data)); Assert.Equal(10, DistanceMetric.calculateLeeDistance(search.Data, best2.Data)); Assert.Equal(19, DistanceMetric.calculateLeeDistance(search.Data, best3.Data)); Assert.True(results.Contains(new KeyValuePair<TestNode, int>(best1, 1))); Assert.True(results.Contains(new KeyValuePair<TestNode, int>(best2, 10))); Assert.True(results.Contains(new KeyValuePair<TestNode, int>(best3, 19))); }
public void BKTree_should_FindBestNodeWithDistance() { BKTree<TestNode> tree = new BKTree<TestNode>(); TestNode search = new TestNode(new int[] { 365, 422, 399 }); TestNode best = new TestNode(4, new int[] { 400, 400, 400 }); tree.add(new TestNode(1, new int[] { 100, 100, 100 })); tree.add(new TestNode(2, new int[] { 200, 200, 200 })); tree.add(new TestNode(3, new int[] { 300, 300, 300 })); tree.add(best); tree.add(new TestNode(5, new int[] { 500, 500, 500 })); Dictionary<TestNode,Int32> result = tree.findBestNodeWithDistance(search); Assert.Equal(1, result.Count); Assert.Equal(58, DistanceMetric.calculateLeeDistance(search.Data, best.Data)); Assert.Equal(58, result.Values.ElementAt(0)); Assert.Equal(4, result.Keys.ElementAt(0).Id); Assert.Equal(best.Data, result.Keys.ElementAt(0).Data); }