Пример #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));
                        }
                    }
                }
            }
        }
Пример #2
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));
        }