Esempio n. 1
0
        public static void BindRGB(List <K_mean> clusarr, byte[,,] res)//связывание пикселей с кластерами по значениям RGB
        {
            int    k  = clusarr.Count;
            int    H  = res.GetUpperBound(1) + 1;
            int    W  = res.GetUpperBound(2) + 1;
            K_mean cl = new K_mean();

            for (int j = 0; j < k; j++)// очищаем привязанные к кластеру пиксели
            {
                clusarr[j].scores.Clear();
            }
            for (int i = 0; i < H; i++)     // строки
            {
                for (int j = 0; j < W; j++) // столбцы
                {
                    double min = Math.Abs(clusarr[0].current_pixel.R - res[0, i, j])
                                 + Math.Abs(clusarr[0].current_pixel.G - res[1, i, j])
                                 + Math.Abs(clusarr[0].current_pixel.B - res[2, i, j]); // начальное минимальное расстояние
                    cl = clusarr[0];
                    for (int n = 0; n < clusarr.Count; n++)
                    {
                        double tmp = Math.Abs(clusarr[n].current_pixel.R - res[0, i, j])
                                     + Math.Abs(clusarr[n].current_pixel.G - res[1, i, j])
                                     + Math.Abs(clusarr[n].current_pixel.B - res[2, i, j]);
                        if (min > tmp)
                        {
                            min = tmp;
                            cl  = clusarr[n];
                        }
                    }
                    Img_pixel pt = new Img_pixel(i, j, res[0, i, j], res[1, i, j], res[2, i, j]);
                    cl.scores.Add(pt); // привязываем пиксель к кластеру к наиблежайшему кластеру
                }
            }
        }
Esempio n. 2
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();
        }