Esempio n. 1
0
        private static MyImage drawEdges(MyImage image, Center[] centers, MyBitplane labels, Color edgeColor)
        {
            MyBitplane edges    = new MyBitplane(image.Width, image.Height);
            MyImage    newImage = new MyImage(image.Width, image.Height, image.NumCh);

            for (int y = 0; y < image.Height; ++y)
            {
                for (int x = 0; x < image.Width; ++x)
                {
                    if (y == 0 || x == 0)
                    {
                        newImage.Bitplane[2].SetPixel(x, y, (int)Math.Round(image.Bitplane[2].GetPixel(x, y)));
                        newImage.Bitplane[1].SetPixel(x, y, (int)Math.Round(image.Bitplane[1].GetPixel(x, y)));
                        newImage.Bitplane[0].SetPixel(x, y, (int)Math.Round(image.Bitplane[0].GetPixel(x, y)));
                        continue;
                    }
                    double p  = labels.GetPixel(x, y);
                    double p1 = labels.GetPixel(x, y - 1);
                    double p2 = labels.GetPixel(x - 1, y - 1);
                    double p3 = labels.GetPixel(x - 1, y);

                    bool edge = false;

                    if (((p1 != p) && (edges.GetPixel(x, y - 1) != 1)) || ((p2 != p) && (edges.GetPixel(x - 1, y - 1) != 1)) || ((p3 != p) && (edges.GetPixel(x - 1, y) != 1)))
                    {
                        edge = true;
                        edges.SetPixel(x, y, 1.0);
                    }

                    if (edge)
                    {
                        newImage.Bitplane[2].SetPixel(x, y, edgeColor.R);
                        newImage.Bitplane[1].SetPixel(x, y, edgeColor.G);
                        newImage.Bitplane[0].SetPixel(x, y, edgeColor.B);
                    }
                    else
                    {
                        newImage.Bitplane[2].SetPixel(x, y, (int)Math.Round(image.Bitplane[2].GetPixel(x, y)));
                        newImage.Bitplane[1].SetPixel(x, y, (int)Math.Round(image.Bitplane[1].GetPixel(x, y)));
                        newImage.Bitplane[0].SetPixel(x, y, (int)Math.Round(image.Bitplane[0].GetPixel(x, y)));
                    }
                }
            }

            return(newImage);
        }
Esempio n. 2
0
        public static Bitmap[] SLIC(String filename, double numberOfCenters, double m, Color edgeColor)
        {
            MyImage image;

            Bitmap[] processedImages = new Bitmap[2];

            using (Bitmap bmp = new Bitmap(filename))
            {
                image = new MyImage(bmp);

                // Convert RGB TO LAB
                image.RGBtoLAB();

                // Create centers
                double     S       = Math.Sqrt((image.Width * image.Height) / numberOfCenters);
                Center[]   centers = createCenters(image, numberOfCenters, S);
                MyBitplane labels  = new MyBitplane(image.Width, image.Height);
                labels.setAllTo(-1);

                for (int iteration = 0; iteration < 10; iteration++)
                {
                    MyBitplane lenghts = new MyBitplane(image.Width, image.Height);
                    lenghts.max();

                    int i = 0;
                    foreach (Center center in centers)
                    {
                        for (int k = (int)Math.Round(center.X - S); k < (int)Math.Round(center.X + S); k++)
                        {
                            for (int l = (int)Math.Round(center.Y - S); l < (int)Math.Round(center.Y + S); l++)
                            {
                                if (k >= 0 && k < image.Width && l >= 0 && l < image.Height)
                                {
                                    double L = image.Bitplane[2].GetPixel(k, l);
                                    double A = image.Bitplane[1].GetPixel(k, l);
                                    double B = image.Bitplane[0].GetPixel(k, l);

                                    double Dc     = Math.Sqrt(Math.Pow(L - center.L, 2) + Math.Pow(A - center.A, 2) + Math.Pow(B - center.B, 2));
                                    double Ds     = Math.Sqrt(Math.Pow(l - center.Y, 2) + Math.Pow(k - center.X, 2));
                                    double lenght = Math.Sqrt(Math.Pow(Dc, 2) + Math.Pow(Ds / 2, 2) * Math.Pow(m, 2));

                                    if (lenght < lenghts.GetPixel(k, l))
                                    {
                                        lenghts.SetPixel(k, l, lenght);
                                        labels.SetPixel(k, l, i);
                                    }
                                }
                            }
                        }
                        i++;
                    }
                    centers = calculateNewCenters(image, centers, labels);
                }

                image = drawAverage(image, centers, labels);
                image.LABtoRGB();
                processedImages[0] = image.GetBitmap(); // Segmented

                image = drawEdges(image, centers, labels, edgeColor);
                processedImages[1] = image.GetBitmap(); // Segmented with Edge
            }

            return(processedImages);
        }