示例#1
0
        private RgbVector[] CalculateInitialColorCentroids()
        {
            var rand      = new Random();
            var centroids = new RgbVector[K];

            for (int i = 0; i < K; i++)
            {
                centroids[i] = _superPixels[rand.Next(0, _superPixels.Length)].Data.Centroid.Lab.AsColorXyz()
                               .AsColorRgb();
            }

            return(centroids);
        }
示例#2
0
        public void Colors_ShouldRoundtripFromRgbToCielab()
        {
            // Arrange
            var color = Color.Red;

            // Act
            var colorRgb     = new RgbVector(color);
            var colorXyz     = ColorXyz.FromRgb(colorRgb);
            var colorCielab  = ColorCielab.FromRgb(colorRgb);
            var colorXyzBack = colorCielab.AsColorXyz();
            var colorRgbBack = colorXyzBack.AsColorRgb();

            // Assert
            Assert.Fail();
        }
示例#3
0
        /// <summary>
        /// Calculates the initial cluster configuration
        /// All clusters have their centroid set, but no pixels assigned
        /// </summary>
        /// <param name="clusterCount"></param>
        /// <returns></returns>
        private PixelCluster[] CalculateInitialClusters(int clusterCount)
        {
            switch (Function)
            {
            case ClusterRandomFunction.RgbDistance:
                // start as a list, so we don't initialize wierd rgbvector defaults
                var clusters = new List <PixelCluster>();

                for (int i = 0; i < clusterCount; i++)
                {
                    var centroid =
                        CalculateRandomClusterCentroid((from cluster in clusters select cluster.ColorCentroid).ToArray());

                    clusters.Add(new PixelCluster {
                        ColorCentroid = centroid
                    });
                }

                return(clusters.ToArray());

                break;

            case ClusterRandomFunction.HueShift:

                // other algorithm
                var firstCentroid = CalculateRandomClusterCentroid(new RgbVector[] { });
                var centroids     = new RgbVector[clusterCount];
                // rotate centroid color
                centroids[0] = firstCentroid;
                for (int i = 1; i < clusterCount; i++)
                {
                    centroids[i] = centroids[i - 1].AsHsvRotated(clusterCount);
                }

                var clustersHue = from centroid in centroids select new PixelCluster {
                    ColorCentroid = centroid
                };

                return(clustersHue.ToArray());

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#4
0
        private double UpdateColorCentroids()
        {
            var iterator = 0;
            var error    = 0.0;

            // iterate over super pixel pixel collections
            foreach (var pixelCollection in CollectPixels())
            {
                if (pixelCollection.Count == 0)
                {
                    continue;
                }

                var oldCentroid = _colorCentroids[iterator];
                _colorCentroids[iterator] = RgbVector.Mean(pixelCollection.ToArray());

                error += oldCentroid.DistanceToSquared(_colorCentroids[iterator]);

                iterator++;
            }

            return(error);
        }