Esempio n. 1
0
        public void Kmean_segmentation()
        {
            int k = 0;

            this.trackBar1.BeginInvoke((MethodInvoker)(() => k = this.trackBar1.Value));
            t1.Start();
            byte[,,] res;
            if (is_Gray)
            {
                res = ToGray(img1);
            }
            else
            {
                bit2 = new Bitmap(img1);
                res  = BitmapToByteRgb(bit2);
            }
            List <K_mean> clusarr = new List <K_mean>(k);

            for (int i = 0; i < clusarr.Capacity; i++)
            {
                clusarr.Add(new K_mean());
                clusarr[i].scores        = new List <Img_pixel>();
                clusarr[i].current_pixel = new Img_pixel();
                clusarr[i].last_pixel    = new Img_pixel();
            }
            //Start
            K_mean.StartRGB(clusarr, res, Initialize_Random, Mark_color);
            t1.Stop();
            this.label3.BeginInvoke((MethodInvoker)(() => this.label3.Text = "Время выполнения: " + (Convert.ToString(Convert.ToDouble(t1.ElapsedMilliseconds) / 1000)) + " сек."));

            bit2 = RgbToBitmap(res);
            pictureBox2.Image = bit2;
            Graphics g        = this.CreateGraphics();
            Font     drawFont = new Font("Arial", 10);
            int      x1       = 950;
            int      y1       = 400;
            int      space    = 560;

            for (int i = 0; i < k; i++)
            {
                SolidBrush drawBrush = new SolidBrush(Color.FromArgb(clusarr[i].current_pixel.R, clusarr[i].current_pixel.G, clusarr[i].current_pixel.B));
                g.FillRectangle(drawBrush, x1, y1, space / k, 20);
                g.DrawString(Convert.ToString(i + 1), drawFont, new SolidBrush(Color.Black), x1 + space / (2 * k), y1 + 23);
                x1 += space / k;
                if (x1 >= 1500)
                {
                    x1  = 950;
                    y1 += 20;
                }
            }

            t1.Reset();
        }