Ejemplo n.º 1
0
        private static HashSet <Frame> my_cluster = new HashSet <Frame>(); //use hashset to coreat a set of cllection of cluster
        public void Init(string file, Int32 Distance, Int32 Offset)        //inite the cluster
        {
            List <Point <int> > Centroids     = new List <Point <int> >();
            MyBitmap            orginal_image = new MyBitmap(file);//load orginal picture

            this.Generate(ref Centroids, orginal_image, Distance, Offset);
            Point <int> Mean      = this.calculate_mean(orginal_image, Centroids);//get mean pixcel for list
            Frame       new_frame = new Frame(orginal_image, Centroids, Mean);

            my_cluster.Add(new_frame);
        }
Ejemplo n.º 2
0
        //------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------
        public Point <int> calculate_mean(MyBitmap Image, List <Point <int> > Centroids)
        {
            //this function allocate and construct mean pixcel for each centroild list:
            double mean_x = 0;
            double mean_Y = 0;
            double total  = (double)Centroids.Count();

            for (Int32 i = 0; i < Centroids.Count(); i++)
            {
                mean_x = mean_x + Centroids[i].x / total;
                mean_Y = mean_Y + Centroids[i].y / total;
            }
            Int32 x = Convert.ToInt32(mean_x);
            Int32 y = Convert.ToInt32(mean_Y);

            Image.LockBits();//locked image
            Color color = Image.GetPixel(x, y);

            Image.UnlockBits();
            return(new Point <int>(x, y, color));
        }
Ejemplo n.º 3
0
        public void Generate(ref List <Point <int> > Centroids, MyBitmap Image, Int32 distance_limit, Int32 color_limit)
        {
            int width  = Image.Width;
            int height = Image.Height;

            Image.LockBits();                                                                                   //Locks a Bitmap into system memory.
            for (Int32 i = 0; i < (width * height); i++)                                                        //generate a specific amount of super-pixels
            {
                Int32       x_random = num_rand.Next(0, width);                                                 // random generate x coord for random super pixcel
                Int32       y_random = num_rand.Next(0, Image.Height);                                          // random generate y coord for random super pixcel
                Point <int> point    = new Point <int>(x_random, y_random, Image.GetPixel(x_random, y_random)); //construct point on x and y.
                //check if the point is right for color and distance, loop through centroids and check
                if (!this.is_right_color(Centroids, point, color_limit) && !this.is_right_distance(Centroids, point, distance_limit))
                {
                    if (!Centroids.Contains(point))
                    {
                        Centroids.Add(point);
                    }
                }
            }
            //now unlock the image:
            Image.UnlockBits();
        }
Ejemplo n.º 4
0
        public void Compute(string name, string Output)
        {
            //generate super_pixcel with original pic:
            master_cluster.Init(name, distance_limit, color_limit);
            // create segmented bitmap:
            int      master_width  = master_cluster[0].frame.Width;
            int      master_height = master_cluster[0].frame.Height;
            MyBitmap final_pic     = new MyBitmap(master_width, master_height);

            //----------------------------------------------------------------------------------------------
            //----------------------------------------------------------------------------------------------

            Int32 number      = 0;
            int   master_size = master_cluster.Count();

            // Iterate throught the array of clusters until we've process all clusters being generated
            for (Int32 i = 0; i < master_size; i++)
            {
                //for each cluster: get centroid and frame saperatly:
                List <Point <int> > Centroids = master_cluster[i].Centroids.ToList();
                MyBitmap            myFrame   = new MyBitmap(master_cluster[i].frame.my_bitmap);
                //------------------------------------------------------------------------------------------
                //myFrame.Save("Segmented Image" + number + ".png");
                //------------------------------------------------------------------------------------------
                //lock bits to start modify:
                myFrame.LockBits();
                //deal with frame first:
                for (Int32 j = 0; j < Centroids.Count(); j++)
                {
                    // Obtain the value of Width and Height of the image for the current cluster
                    Int32 current_w = myFrame.Width;
                    Int32 current_h = myFrame.Height;

                    // new bitmap for new cluster:
                    MyBitmap modefilyable = new MyBitmap(current_w, current_h);
                    modefilyable.LockBits();
                    for (Int32 x = 0; x < current_w; x++)
                    {
                        for (Int32 y = 0; y < current_h; y++)
                        {
                            // For each pixel in this matrix, compute the value of color offset of the current centroid super-pixel
                            Point <int> first       = new Point <int>(x, y, myFrame.GetPixel(x, y));
                            Point <int> second      = new Point <int>(Centroids[j].x, Centroids[j].y, Centroids[j].color);
                            double      color_depth = Euclidian_distance(first, second);
                            if (color_depth <= 50)
                            {
                                modefilyable.SetPixel(x, y, Centroids[j].color);
                            }
                            else
                            {
                                modefilyable.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                            }
                        }
                    }

                    modefilyable.UnlockBits();
                    List <Point <int> > list = new List <Point <int> >();
                    list.Add(Centroids[0]);
                    // calculate mean for new cluster:
                    Point <int> new_avg = master_cluster.calculate_mean(modefilyable, list);
                    if (new_avg.x != master_cluster[i].Center.x && new_avg.y != master_cluster[i].Center.y)
                    {
                        master_cluster.Add(modefilyable, list, new_avg);
                    }
                    //increase seg pic by 1:
                    number++;
                }//j second for loop stop here

                myFrame.UnlockBits();
            }
            //----------------------------------------------------------------------------------------------
            //----------------------------------------------------------------------------------------------
            final_pic.LockBits();

            for (Int32 q = 0; q < master_cluster.Count(); q++)
            {
                MyBitmap seg = new MyBitmap(master_cluster[q].frame.my_bitmap);
                int      w   = seg.Width;
                int      h   = seg.Height;
                seg.LockBits();
                seg.Save("resulting" + q + ".png");
                for (Int32 r = 0; r < w; r++)
                {
                    for (Int32 c = 0; c < h; c++)
                    {
                        if (seg.GetPixel(r, c) != Color.FromArgb(255, 255, 255))
                        {
                            final_pic.SetPixel(r, c, seg.GetPixel(r, c));
                        }
                    }
                }

                seg.UnlockBits();
            }
            //----------------------------------------------------------------------------------------------
            //----------------------------------------------------------------------------------------------
            final_pic.UnlockBits();
            //final_pic.Save(Output);
            //save the segmented Image into folder:
            // Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
            string        sourcePath   = Directory.GetCurrentDirectory();
            DirectoryInfo my_directory = new DirectoryInfo("Segmented Image");
            string        targetPath   = sourcePath + "/Segmented Image";

            //create directory:
            if (my_directory.Exists == false)
            {
                Directory.CreateDirectory("Segmented Image");
            }
            //copy file:
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath, "resulting*");

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    string fileName = System.IO.Path.GetFileName(s);
                    string destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }


            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath, "resulting*");

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    File.Delete(s);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

            // // Copy each file into it's new directory.
            // foreach (FileInfo fi in source.GetFiles()){
            //     GetFiles(String)
            //     Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
            //     fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            // }
        }
Ejemplo n.º 5
0
 //------------------------------------------------------------------------------------------
 //------------------------------------------------------------------------------------------
 public void Add(MyBitmap Image, List <Point <int> > Centroids, Point <int> center)
 {
     my_cluster.Add(new Frame(Image, Centroids, center));
 }
Ejemplo n.º 6
0
        private List <Point <int> > Centroids_list = null; //define a list centroid pixel

        // fram constructor
        public Frame(MyBitmap bitmap_frame, List <Point <int> > centroids_list, Point <int> center)
        {
            this.frame     = bitmap_frame;
            this.Centroids = centroids_list;
            this.Center    = center;
        }