Esempio n. 1
0
        private bool initClustering()
        {
            if (!loaded)
            {
                return(false);
            }

            mClusters.Clear();
            HashSet <ColorPoint> mSet = new HashSet <ColorPoint>();
            Random rand = new Random();

            int cluserCount;

            if (!int.TryParse(txtClusterCount.Text, out cluserCount))
            {
                MessageBox.Show("Number of Clusters should be an integer!");
                return(false);
            }

            for (int i = 0; i < cluserCount; i++)
            {
                ColorPoint p = mColorPoints[rand.Next(0, mColorPoints.Count)];
                while (mSet.Contains(p))
                {
                    p = mColorPoints[rand.Next(0, mColorPoints.Count)];
                }
                mClusters.Add(new Cluster(p.r, p.g, p.b));
            }

            updateClusteringWorkload();

            keepClustering = true;
            return(true);
        }
Esempio n. 2
0
        private void loadImage(string filename)
        {
            if (loaded)
            {
                return;
            }

            Bitmap bmap = new Bitmap(ofd_image.FileName);

            mColorPoints = null;
            mClusters.Clear();
            mColorPointDict.Clear();

            mHeight = bmap.Height;
            mWidth  = bmap.Width;

            pbProgress.Maximum = mWidth * mHeight;
            pbProgress.Value   = 0;

            for (int i = 0; i < mHeight; i++)
            {
                for (int j = 0; j < mWidth; j++)
                {
                    Color             pixel = bmap.GetPixel(j, i);
                    ColorPoint        cp    = new ColorPoint(pixel.R, pixel.G, pixel.B);
                    List <PixelPoint> lpp;
                    if (mColorPointDict.ContainsKey(cp))
                    {
                        lpp = mColorPointDict[cp];
                    }
                    else
                    {
                        lpp = new List <PixelPoint>();
                        mColorPointDict[cp] = lpp;
                    }
                    PixelPoint pp = new PixelPoint(j, i);
                    lpp.Add(pp);
                    pbProgress.Value += 1;
                }
            }

            mColorPoints = mColorPointDict.Keys.ToList();

            btnLoadImage.Enabled = false;
            btnClear.Enabled     = true;
            btnConvert.Enabled   = true;
            loaded         = true;
            keepClustering = true;
            drawBitmap();

            pbProgress.Value = 0;
        }
Esempio n. 3
0
        private Cluster getClosestCluster(ColorPoint p)
        {
            Cluster closest = mClusters[0];
            double  d       = p.distance(closest.getMean());
            double  t;
            Cluster tc;

            for (int i = 1; i < mClusters.Count; i++)
            {
                tc = mClusters[i];
                t  = p.distance(tc.getMean());
                if (t < d)
                {
                    d       = t;
                    closest = tc;
                }
            }
            return(closest);
        }
Esempio n. 4
0
 public void addPoint(ColorPoint p)
 {
     mutexPoints.WaitOne();
     points.Add(p);
     mutexPoints.ReleaseMutex();
 }
Esempio n. 5
0
 public Cluster(int r, int g, int b)
 {
     mean = new ColorPoint(r, g, b);
 }