Esempio n. 1
0
        //---------------------------------------------------

        public UnsafeBitmapEnumerator(UnsafeBitmap fastBitmap)
        {
            fastBitmap.LockBitmap();
            locked          = true;
            this.fastBitmap = fastBitmap;
            x             = -1;
            y             = 0;
            pCurrentPixel = fastBitmap[x, y];
        }
Esempio n. 2
0
        public void Iterate()
        {
            _colourClusterAllocation    = new Hashtable(); //for keeping track of colour<->cluster allocation
            _pixelDataClusterAllocation = new Hashtable();
            _clusterColours             = new Hashtable();

            UnsafeBitmap fastBitmap = new UnsafeBitmap(_image);

            fastBitmap.LockBitmap();
            Point size = fastBitmap.Size;
            BGRA *pPixel;

            for (int y = 0; y < size.Y; y++)
            {
                pPixel = fastBitmap[0, y];
                for (int x = 0; x < size.X; x++)
                {
                    PixelData pd = Colour.GetPixelData(pPixel, _model);
                    AllocateToCluster(pd);

                    //increment the pointer
                    pPixel++;
                }
            }
            fastBitmap.UnlockBitmap();

            CalculateClusterCentroids();

            _processedImage = (Bitmap)_image.Clone();

            //segment the image based on the cluster
            fastBitmap = new UnsafeBitmap(_processedImage);
            fastBitmap.LockBitmap();
            for (int y = 0; y < size.Y; y++)
            {
                pPixel = fastBitmap[0, y];
                for (int x = 0; x < size.X; x++)
                {
                    PixelData pd     = Colour.GetPixelData(pPixel, _model);
                    Color     newClr = (Color)_clusterColours[pd.Name];

                    pPixel->red   = newClr.R;
                    pPixel->green = newClr.G;
                    pPixel->blue  = newClr.B;

                    //increment the pointer
                    pPixel++;
                }
            }

            fastBitmap.UnlockBitmap();

            CheckConvergence();
        }
Esempio n. 3
0
        private void FindTopXColours(int numColours)
        {
            Dictionary <string, ColourCount> colours = new Dictionary <string, ColourCount>();
            UnsafeBitmap fastBitmap = new UnsafeBitmap(_image);

            fastBitmap.LockBitmap();
            Point size = fastBitmap.Size;
            BGRA *pPixel;

            for (int y = 0; y < size.Y; y++)
            {
                pPixel = fastBitmap[0, y];
                for (int x = 0; x < size.X; x++)
                {
                    //get the bin index for the current pixel colour
                    Color clr = Color.FromArgb(pPixel->red, pPixel->green, pPixel->blue);

                    if (colours.ContainsKey(clr.Name))
                    {
                        ((ColourCount)colours[clr.Name]).Count++;
                    }
                    else
                    {
                        colours.Add(clr.Name, new ColourCount(clr, 1));
                    }

                    //increment the pointer
                    pPixel++;
                }
            }

            fastBitmap.UnlockBitmap();

            //instantiate using actual colours found - which might be less than numColours
            if (colours.Count < numColours)
            {
                numColours = colours.Count;
            }

            _topColours = new Color[numColours];

            List <KeyValuePair <string, ColourCount> > summaryList = new List <KeyValuePair <string, ColourCount> >();

            summaryList.AddRange(colours);

            summaryList.Sort(delegate(KeyValuePair <string, ColourCount> kvp1, KeyValuePair <string, ColourCount> kvp2)
                             { return(Comparer <int> .Default.Compare(kvp2.Value.Count, kvp1.Value.Count)); });


            for (int i = 0; i < _topColours.Length; i++)
            {
                _topColours[i] = Color.FromArgb(summaryList[i].Value.Colour.R, summaryList[i].Value.Colour.G, summaryList[i].Value.Colour.B);
            }
        }