Beispiel #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);
            }
Beispiel #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));
            }
        }
Beispiel #3
0
 /// <summary>
 /// Initialize empty clusters with centroid values randomly generated from their domain.
 /// </summary>
 /// <param name="clusterCount">Number of clusters to initialize.</param>
 protected void InitEmptyClusters(uint clusterCount)
 {
     _clusters = new Dictionary <uint, Cluster>();
     for (uint i = 0; i < clusterCount; i++)
     {
         CentroidPoint centroid = new CentroidPoint(PointDim);
         centroid.FillWithRandomValues();
         _clusters.Add(centroid.ID, new Cluster(centroid));
     }
 }
Beispiel #4
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));
            }
        }
            public void SetCentroidToWeightedAverageOf(Cluster cl1, Cluster cl2)
            {
                CentroidPoint ctr1    = cl1.Centroid;
                int           weight1 = cl1.PointCount;
                CentroidPoint ctr2    = cl2.Centroid;
                int           weight2 = cl2.PointCount;

                if (weight1 + weight2 != 0) // otherwise the centroid can remain unchanged
                {
                    Centroid.SetWeightedAverageOf(ctr1, weight1, ctr2, weight2);
                }
            }
Beispiel #6
0
        protected CentroidPoint ClosestCentroidTo(Point point)
        {
            System.Diagnostics.Debug.Assert(ClusterNum > 0);

            CentroidPoint res     = new CentroidPoint(PointDim);
            float         minDist = float.MaxValue;

            foreach (Cluster cluster in _clusters.Values)
            {
                CentroidPoint centroid = cluster.Centroid;
                float         dist     = Point.SquaredEuclDist(centroid, point);
                if (dist < minDist)
                {
                    minDist = dist;
                    res     = centroid;
                }
            }
            return(res);
        }
Beispiel #7
0
        private void AddPointToClosestCluster(ImagePoint point)
        {
            CentroidPoint closestCentroid = ClosestCentroidTo(point);

            _clusters[closestCentroid.ID].AddPoint(point);
        }
 public Cluster(CentroidPoint centroid)
 {
     Points   = new HashSet <ImagePoint>();
     Centroid = centroid;
 }