/// <summary> /// Adds the data from <paramref name="image"/> to each tree in the forest. /// </summary> /// <param name="forest">The forest used for the computation</param> /// <param name="image">Image to learn from</param> /// <param name="mode">Mode to use when sampling the image background</param> public static void Fill <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, LabeledImage <T> image, BackgroundSampleMode mode) { for (int i = 0; i < forest.TreeCount; i++) { forest[i].Fill(image, mode); } forest.RefreshMetadata(); }
/// <summary> /// Classifies each pixel in the image and returns the leaf nodes which they end up in. /// </summary> /// <param name="forest">The forest used for the computation</param> /// <param name="image">Image to classify</param> /// <returns>A leaf image</returns> public static LeafImage <T> ComputeLeafImage <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, IMultichannelImage <T> image) { LeafImage <T> leafImage = new LeafImage <T>(image.Rows, image.Columns, forest.TreeCount); leafImage.ID = image.ID; for (int i = 0; i < forest.TreeCount; i++) { forest[i].FillLeafImage(leafImage, image); } return(leafImage); }
/// <summary> /// Classifies every pixel in the provided image, returning a full distribution over all labels. /// </summary> /// <param name="forest">The forest used for the computation</param> /// <param name="image">Image to classify</param> /// <returns>The classified image</returns> public static DistributionImage ClassifySoft <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, LabeledImage <T> image) { DistributionImage dist = new DistributionImage(image.Image.Rows, image.Image.Columns, forest.LabelCount); dist.ID = image.ID; for (int t = 0; t < forest.TreeCount; t++) { forest[t].ClassifySoft(image, dist); } dist.DivideThrough(forest.TreeCount); dist.Normalize(); return(dist); }
/// <summary> /// Computes a histogram for all trees from the provided image. /// </summary> /// <param name="forest">The forest used for the computation</param> /// <param name="image">Image to classify</param> /// <returns>The histogram</returns> public static TreeHistogram ComputeHistogram <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, IMultichannelImage <T> image) { int rows = image.Rows; int columns = image.Columns; List <ImageDataPoint <T> > points = new List <ImageDataPoint <T> >(); for (short r = 0; r < rows; r++) { for (short c = 0; c < columns; c++) { points.Add(new ImageDataPoint <T>(image, r, c, -1)); } } return(forest.ComputeHistogram(points)); }
/// <summary> /// Get the node information for all points from all trees. /// </summary> /// <typeparam name="T">The type of the data point</typeparam> /// <typeparam name="D">The underlying feature type of the data point</typeparam> /// <param name="forest">The forest to query</param> /// <param name="points">The points to get info for</param> /// <returns>The desired info</returns> public static INodeInfo <T, D>[][] GetNodeInfo <T, D>(this DecisionForest <T, D> forest, List <T> points) where T : IDataPoint <D> { List <Task <INodeInfo <T, D>[]> > tasks = new List <Task <INodeInfo <T, D>[]> >(); TaskFactory <INodeInfo <T, D>[]> factory = new TaskFactory <INodeInfo <T, D>[]>(); for (int t = 0; t < forest.TreeCount; t++) { tasks.Add(factory.StartNew(arg => { DecisionTree <T, D> tree = (DecisionTree <T, D>)arg; INodeInfo <T, D>[] info = new INodeInfo <T, D> [points.Count]; DecisionTree <T, D> .assignLabels(tree._root, points, info, Enumerable.Range(0, points.Count).ToList()); return(info); }, forest[t])); } Task.WaitAll(tasks.ToArray()); return(tasks.Select(o => o.Result).ToArray()); }
/// <summary> /// Classifies every pixel in the provided image with the maximum likelihood label. /// </summary> /// <param name="forest">The forest used for the computation</param> /// <param name="image">Image to classify</param> /// <returns>The classified image</returns> public static LabelImage Classify <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, IMultichannelImage <T> image) { return(forest.ClassifySoft(image).ToLabelImage()); }
/// <summary> /// Computes a histogram for all trees from the provided image. /// </summary> /// <param name="forest">The forest used for the computation</param> /// <param name="image">Image to classify</param> /// <returns>The histogram</returns> public static TreeHistogram ComputeHistogram <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, LabeledImage <T> image) { return(forest.ComputeHistogram(image.CreateAllDataPoints(BackgroundSampleMode.Full))); }