/// <summary> /// Creates a list of pixels sampled according to <paramref name="mode"/>. /// </summary> /// <param name="mode">Mode to use when sampling pixels marked with the "background label" as defined by <see cref="LabelImage.BackgroundLabel"/></param> /// <returns>List of sampled pixels</returns> public List <ImageDataPoint <T> > CreateAllDataPoints(BackgroundSampleMode mode) { if (_labels != null) { return(createAllDataPointsLabels(mode)); } else { return(createAllDataPointsRaw()); } }
private unsafe List <ImageDataPoint <T> > createAllDataPointsLabels(BackgroundSampleMode mode) { List <ImageDataPoint <T> > points = new List <ImageDataPoint <T> >(); int rows = _image.Rows; int columns = _image.Columns; LabelSet set = _labels.Labels; fixed(short *labelsSrc = _labels.RawArray) { fixed(bool *validSrc = _valid) { short *labelsPtr = labelsSrc; bool * validPtr = validSrc; for (short r = 0; r < rows; r++) { for (short c = 0; c < columns; c++) { short label = getLabel(*labelsPtr++, set); bool sample = *validPtr++; if (sample && label == LabelImage.BackgroundLabel) { switch (mode) { case BackgroundSampleMode.Ignore: sample = false; break; case BackgroundSampleMode.Half: sample = ThreadsafeRandom.Test(.5); break; } } if (sample) { points.Add(new ImageDataPoint <T>(_image, r, c, label)); } } } } } return(points); }
/// <summary> /// Classifies each point from <paramref name="image"/> and trackes which nodes it visits. /// </summary> /// <param name="tree">The tree used for the computation</param> /// <param name="image">Image to add to the tree</param> /// <param name="mode">Mode to use when sampling the image</param> public static void Fill <T>(this DecisionTree <ImageDataPoint <T>, T[]> tree, LabeledImage <T> image, BackgroundSampleMode mode) { List <ImageDataPoint <T> > points = image.CreateAllDataPoints(mode); tree.Fill(points); }
/// <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(); }