Esempio n. 1
0
        private void FolderReadThread()
        {
            DisableAllControls();
            List <Color> rgbColorList = new List <Color>();

            SetProgressBar(0);

            for (int i = 0; i < folderReadFilenames.Length; ++i)
            {
                Debug.WriteLine(folderReadFilenames[i]);
                Bitmap bmp = null;
                try { bmp = new Bitmap(folderReadFilenames[i]); }
                catch (Exception) { Debug.WriteLine("Could not open " + folderReadFilenames[i]); }

                if (bmp != null)
                {
                    Color centerColor = BitmapTool.GetCenterColor(bmp);
                    Debug.WriteLine("Color is " + centerColor.R + " " + centerColor.G + " " + centerColor.B);
                    rgbColorList.Add(centerColor);
                    bmp.Dispose();
                }

                SetProgressBar((int)((i + 1f) / folderReadFilenames.Length * 100));
            }

            SetProgressBar(100);

            if (rgbColorList.Count == 0)
            {
                MessageBox.Show("No images found in selected folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                SetPreviewColorList(rgbColorList);
            }
            EnableAllControls();
        }
Esempio n. 2
0
        public static void GetColorClusters(Bitmap bmp, ref LABColor[] colors, ref int[] clusterCount, int numClusters, Action progressCallback)
        {
            Bitmap lowResBmp = BitmapTool.GetLowerResBitmap(bmp, LOW_RES_SIZE);

            if (lowResBmp != null)
            {
                bmp = lowResBmp;
            }

            LABColor[] labPixelData = GetLABPixelData(bmp);

            int numCentroids = numClusters;

            LABColor[] centroids = PickKMeansCentroids(numCentroids, labPixelData);

            int[] labPixelClosestCentroid = new int[labPixelData.Length];
            int[] centroidClusterCount    = new int[centroids.Length];

            //initialize closest centroids to -1 (no centroid)
            for (int p = 0; p < labPixelClosestCentroid.Length; ++p)
            {
                labPixelClosestCentroid[p] = -1;
            }

            bool clustersChanged = false;

            do
            {
                Debug.WriteLine("Calculate clusters");
                clustersChanged = CalculateClusterChanges(labPixelData, centroids, labPixelClosestCentroid, centroidClusterCount);
                RecalculateCentroids(labPixelData, centroids, labPixelClosestCentroid, centroidClusterCount);
                progressCallback();
            } while (clustersChanged);

            for (int c = 0; c < numCentroids; ++c)
            {
                Debug.WriteLine(centroids[c] + " clustersize:" + centroidClusterCount[c]);
            }

            float[] LfAvg = new float[numClusters];
            for (int p = 0; p < labPixelData.Length; ++p)
            {
                LfAvg[labPixelClosestCentroid[p]] += labPixelData[p].Lf;
            }

            for (int c = 0; c < numCentroids; ++c)
            {
                if (centroidClusterCount[c] > 0)
                {
                    LfAvg[c] /= centroidClusterCount[c];
                }
                centroids[c].Lf = LfAvg[c];
            }

            colors       = centroids;
            clusterCount = centroidClusterCount;

            //cleanup
            if (lowResBmp != null)
            {
                lowResBmp.Dispose();
            }
        }