/// <summary> /// Extracts a random number of points, such that the probability of a point being extracted is equal /// to <paramref name="percentage"/> /// </summary> /// <typeparam name="T">Underlying type of the image</typeparam> /// <param name="image">The image</param> /// <param name="percentage">The probability that a point will be extracted</param> /// <param name="border">Border around the edge of the image to exclude</param> /// <returns>A list of image points</returns> public static IEnumerable <ImageDataPoint <T> > ExtractPoints <T>(this IMultichannelImage <T> image, double percentage, short border = 0) { int rows = image.Rows; int columns = image.Columns; int total = (int)((rows - 2 * border) * (columns - 2 * border) * percentage); if (percentage <= .1) { for (int i = 0; i < total; i++) { short row = (short)ThreadsafeRandom.Next(border, rows - border); short column = (short)ThreadsafeRandom.Next(border, rows - border); yield return(new ImageDataPoint <T>(image, row, column, 0)); } } else { for (short r = border; r < rows - border; r++) { for (short c = border; c < columns - border; c++) { if (ThreadsafeRandom.NextDouble() < percentage) { yield return(new ImageDataPoint <T>(image, r, c, 0)); } } } } }
private static List <T> permute <T>(List <T> current, List <T> remaining, int size) { while (current.Count < size && remaining.Count > 0) { int index = ThreadsafeRandom.Next(remaining.Count); current.Add(remaining[index]); remaining.RemoveAt(index); } return(current); }
/// <summary> /// Returns a random rectangle within the rectangle provided. /// </summary> /// <param name="minRow">Minimum row</param> /// <param name="minColumn">Minimum column</param> /// <param name="maxRow">Maximum bound for a rectangle</param> /// <param name="maxColumn">Maximum bound for a column</param> /// <returns></returns> public static Rectangle Random(int minRow, int minColumn, int maxRow, int maxColumn) { Rectangle rect = new Rectangle(); rect.R = ThreadsafeRandom.Next(minRow, maxRow); rect.C = ThreadsafeRandom.Next(minColumn, maxColumn); rect.Rows = ThreadsafeRandom.Next(maxRow - rect.R) + 1; rect.Columns = ThreadsafeRandom.Next(maxColumn - rect.C) + 1; return(rect); }
/// <summary> /// Creates a scale-space sample given the provided sample size. /// </summary> /// <param name="method">Method to use when sampling scale-space</param> /// <param name="imageHeight">Height of the source image</param> /// <param name="imageWidth">Width of the source image</param> /// <param name="levels">Number of levels in each octave of the pyramid</param> /// <returns>The sample parameters</returns> public static ScaleSpaceSample CreateSample(int imageWidth, int imageHeight, int levels, ScaleSpaceSampleMethod method) { int totalCount = 0; List <Rectangle> sizes = new List <Rectangle>(); Rectangle currentSize = new Rectangle { Columns = imageWidth, Rows = imageHeight }; while (currentSize.Width >= MIN_SIZE && currentSize.Height >= MIN_SIZE) { sizes.Add(currentSize); currentSize.Width /= 2; currentSize.Height /= 2; } int octaves = sizes.Count; int[] octaveCounts = new int[octaves]; int[] octaveWidths = new int[octaves]; int[] octaveHeights = new int[octaves]; for (int o = 0; o < octaves; o++) { int w, h, c; Rectangle p = sizes[o]; octaveWidths[o] = w = p.Width; octaveHeights[o] = h = p.Height; if (w < 0 || h < 0) { octaveCounts[o] = c = 0; } else { octaveCounts[o] = c = w * h; } totalCount += c; } int row, column, octave, level, sample, columns; row = column = octave = level = sample = columns = -1; switch (method) { case ScaleSpaceSampleMethod.Uniform: sample = ThreadsafeRandom.Next(totalCount); octave = 0; while (sample > octaveCounts[octave]) { sample -= octaveCounts[octave]; octave++; } columns = octaveWidths[octave]; row = sample / columns; column = sample % columns; level = ThreadsafeRandom.Next(levels); break; case ScaleSpaceSampleMethod.ScaleWeighted: octave = ThreadsafeRandom.Next(octaves); while (octaveCounts[octave] == 0) { octave = ThreadsafeRandom.Next(octaves); } level = ThreadsafeRandom.Next(levels); columns = octaveWidths[octave]; sample = ThreadsafeRandom.Next(octaveCounts[octave]); row = sample / columns; column = sample % columns; break; } return(new ScaleSpaceSample(octave, level, row, column)); }