Пример #1
0
            /// <summary>
            /// Creates a new CentroidPoint and fills its values with a deep copy of the values of an existing CentroidPoint.
            /// The ID is not copied from the existing CentroidPoint.
            /// </summary>
            /// <returns>The newly created CentroidPoint.</returns>
            public static CentroidPoint CentroidOfValues(CentroidPoint toCopy)
            {
                CentroidPoint res = new CentroidPoint(toCopy.Dim);

                res.CopyValuesFrom(toCopy);
                return(res);
            }
Пример #2
0
        /// <summary>
        /// Initialize empty clusters with centroid values based on their distances from each other.
        /// </summary>
        /// <param name="clusterCount">Number of clusters to initialize.</param>
        protected void InitEmptyKmeansClusters(uint clusterCount)
        {
            _clusters = new Dictionary <uint, Cluster>();
            CentroidPoint firstcentroid = new CentroidPoint(PointDim);

            firstcentroid.FillWithRandomValues();                        //first center is random
            _clusters.Add(firstcentroid.ID, new Cluster(firstcentroid));

            for (uint i = 1; i < clusterCount; i++)
            {
                CentroidPoint centroid = new CentroidPoint(PointDim);
                Point         farthest = new Point(PointDim);
                float         maxDist  = 0.0f;
                foreach (ImagePoint point in _points)
                {
                    float dist = 0.0f;
                    foreach (var cluster in _clusters)
                    {
                        dist += Point.SquaredEuclDist(cluster.Value.Centroid, point);
                    }

                    if (dist > maxDist)
                    {
                        maxDist  = dist;
                        farthest = point;
                    }
                }
                centroid.CopyValuesFrom(farthest);
                _clusters.Add(centroid.ID, new Cluster(centroid));
            }
        }
Пример #3
0
        /// <summary>
        /// Initialize empty clusters with centroid values randomly selected from the pixels of the loaded input image.
        /// </summary>
        /// <param name="clusterCount">Number of clusters to initialize.</param>
        protected void InitEmptyClustersFromRandomImagePoints(uint clusterCount)
        {
            _clusters = new Dictionary <uint, Cluster>();
            List <Point> randomPoints = PickRandomImagePoints(_random, (int)clusterCount);

            for (uint i = 0; i < clusterCount; i++)
            {
                CentroidPoint centroid = new CentroidPoint(PointDim);
                centroid.CopyValuesFrom(randomPoints[(int)i]);
                _clusters.Add(centroid.ID, new Cluster(centroid));
            }
        }