Beispiel #1
0
        /// <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));
                        }
                    }
                }
            }
        }
Beispiel #2
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);
 }
Beispiel #3
0
        /// <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);
        }
Beispiel #4
0
        /// <summary>
        /// Samples an index from <paramref name="distribution"/>.
        /// </summary>
        /// <param name="distribution">A collection of values which sum to 1</param>
        /// <returns>The sampled index</returns>
        public static int Sample(this IEnumerable <float> distribution)
        {
            float         sample = ThreadsafeRandom.NextFloat() * distribution.Sum();
            Queue <float> values = new Queue <float>(distribution);
            int           index  = 0;

            while (sample > 0 && values.Count > 0)
            {
                sample -= values.Dequeue();
                index++;
            }
            return(index - 1);
        }
Beispiel #5
0
        /// <summary>
        /// Samples an index from <paramref name="list"/> using <paramref name="selector"/> to transform <typeparamref name="T"/> to a float.
        /// </summary>
        /// <typeparam name="T">Type of the list</typeparam>
        /// <param name="list">The list to sample</param>
        /// <param name="selector">Function used to create a normalized list</param>
        /// <returns>The sampled index</returns>
        public static int Sample <T>(this IEnumerable <T> list, Func <T, float> selector)
        {
            float         sample = ThreadsafeRandom.NextFloat() * list.Sum(selector);
            Queue <float> values = new Queue <float>(from item in list
                                                     select selector(item));
            int index = 0;

            while (sample > 0 && values.Count > 0)
            {
                sample -= values.Dequeue();
                index++;
            }
            return(index - 1);
        }
Beispiel #6
0
        /// <summary>
        /// Randomly samples the Gaussian distribution.
        /// </summary>
        /// <returns>A sample from the Gaussian distribution</returns>
        public float Sample()
        {
            double x1, x2, w, y1, y2;

            do
            {
                x1 = 2.0 * ThreadsafeRandom.NextDouble() - 1.0;
                x2 = 2.0 * ThreadsafeRandom.NextDouble() - 1.0;
                w  = x1 * x1 + x2 * x2;
            } while (w >= 1.0);

            w  = Math.Sqrt((-2.0 * Math.Log(w)) / w);
            y1 = x1 * w;
            y2 = x2 * w;

            return((float)(y1 * _stddev + _mean));
        }
        /// <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));
        }