Esempio n. 1
0
        private static ClusterIndices4X4 CreateClusterIndexBlock(RawBlock4X4Rgba32 raw, out int outputNumClusters,
                                                                 int numClusters = 3)
        {
            ClusterIndices4X4 indexBlock = new ClusterIndices4X4();

            var indices = LinearClustering.ClusterPixels(raw.AsSpan, 4, 4,
                                                         numClusters, 1, 10, false);

            var output = indexBlock.AsSpan;

            for (int i = 0; i < output.Length; i++)
            {
                output[i] = indices[i];
            }

            int nClusters = indexBlock.NumClusters;

            if (nClusters < numClusters)
            {
                indexBlock = indexBlock.Reduce(out nClusters);
            }

            outputNumClusters = nClusters;
            return(indexBlock);
        }
Esempio n. 2
0
        public void Clusterize()
        {
            using var testImage = ImageLoader.testBlur1.Clone();
            var pixels      = testImage.GetPixelSpan();
            int numClusters = (testImage.Width / 32) * (testImage.Height / 32);

            var clusters = LinearClustering.ClusterPixels(pixels, testImage.Width, testImage.Height, numClusters, 10, 10);

            ColorYCbCr[] pixC   = new ColorYCbCr[numClusters];
            int[]        counts = new int[numClusters];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixC[clusters[i]] += new ColorYCbCr(pixels[i]);
                counts[clusters[i]]++;
            }
            for (int i = 0; i < numClusters; i++)
            {
                pixC[i] /= counts[i];
            }
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = pixC[clusters[i]].ToRgba32();
            }

            using var fs = File.OpenWrite("test_cluster.png");
            testImage.SaveAsPng(fs);
        }