Example #1
0
 private static int CompareBorderPoints(BorderPoint a, BorderPoint b)
 {
     if (a.point.Y == b.point.Y)
     {
         return(a.point.X.CompareTo(b.point.X));
     }
     else if (a.point.Y < b.point.Y)
     {
         return(-1);
     }
     else
     {
         return(1);
     }
 }
Example #2
0
        private List <BorderPoint> GetBorderPoints(Bitmap sourceImage, Color c)
        {
            List <BorderPoint> border = new List <BorderPoint>();
            BorderPoint        cur    = FindStartPoint(sourceImage);

            border.Add(cur);
            BorderPoint start       = cur;
            BorderPoint next        = new BorderPoint(-1, -1, -1);
            Color       borderColor = sourceImage.GetPixel(cur.point.X, cur.point.Y);
            int         lookTo      = -1;

            do
            {
                lookTo = cur.flag - 2;
                if (lookTo == -1)
                {
                    lookTo = 7;
                }
                else if (lookTo == -2)
                {
                    lookTo = 6;
                }
                int t = lookTo;
                do
                {
                    next.point = cur.point;
                    switch (lookTo)
                    {
                    case 0: next.point.X++; next.flag = 0; break;

                    case 1: next.point.X++; next.point.Y--; next.flag = 1; break;

                    case 2: next.point.Y--; next.flag = 2; break;

                    case 3: next.point.X--; next.point.Y--; next.flag = 3; break;

                    case 4: next.point.X--; next.flag = 4; break;

                    case 5: next.point.X--; next.point.Y++; next.flag = 5; break;

                    case 6: next.point.Y++; next.flag = 6; break;

                    case 7: next.point.X++; next.point.Y++; next.flag = 7; break;
                    }
                    if (next.point == start.point)
                    {
                        break;
                    }
                    if (next.point.X < 0 || next.point.X > pictureBox1.Width || next.point.Y > pictureBox1.Height || next.point.Y < 0)
                    {
                        MessageBox.Show("Границы не найдены");
                        return(null);
                    }
                    if (sourceImage.GetPixel(next.point.X, next.point.Y) == borderColor)
                    {
                        border.Add(next);
                        if (cur.flag == 2 && next.flag == 6 || cur.flag == 6 && next.flag == 2)
                        {
                            border.Add(next);
                        }
                        else if (cur.flag == 1 && next.flag == 7 || cur.flag == 5 && next.flag == 3)
                        {
                            border.Add(next);
                        }
                        else if (cur.flag == 4 && next.flag == 0 || cur.flag == 4 && next.flag == 4)
                        {
                            border.Add(next);
                        }
                        cur  = next;
                        next = new BorderPoint(-1, -1, -1);
                        break;
                    }
                    lookTo = (lookTo + 1) % 8;
                } while (lookTo != t);
            } while (next.point != start.point);

            if (c != Color.Empty)
            {
                foreach (var x in border)
                {
                    sourceImage.SetPixel(x.point.X, x.point.Y, c);
                }
                return(null);
            }
            else
            {
                border.Sort(CompareBorderPoints);


                return(border);
            }
        }
Example #3
0
        //Функция формирования списка граничных точек
        private List <BorderPoint> GetBorderPoints(Bitmap sourceImage, Color c)
        {
            List <BorderPoint> border = new List <BorderPoint>();
            BorderPoint        cur    = FindStartPoint(sourceImage);

            border.Add(cur);
            BorderPoint start       = cur;
            BorderPoint next        = new BorderPoint(-1, -1, -1);
            Color       borderColor = sourceImage.GetPixel(cur.point.X, cur.point.Y);

            //Будем идти против часовой стрелке и ходить изнутри области
            int lookTo = -1;

            do
            {
                lookTo = cur.preDir - 2;
                //Отнимаем в случае границ (По модулю 8)
                if (lookTo == -1)
                {
                    lookTo = 7;
                }
                else if (lookTo == -2)
                {
                    lookTo = 6;
                }
                int t = lookTo;
                do
                {
                    next.point = cur.point;
                    switch (lookTo)
                    {
                    case 0: next.point.X++; next.preDir = 0; break;

                    case 1: next.point.X++; next.point.Y--; next.preDir = 1; break;

                    case 2: next.point.Y--; next.preDir = 2; break;

                    case 3: next.point.X--; next.point.Y--; next.preDir = 3; break;

                    case 4: next.point.X--; next.preDir = 4; break;

                    case 5: next.point.X--; next.point.Y++; next.preDir = 5; break;

                    case 6: next.point.Y++; next.preDir = 6; break;

                    case 7: next.point.X++; next.point.Y++; next.preDir = 7; break;
                    }
                    //Если не нашли - останавливаемся
                    if (next.point == start.point)
                    {
                        break;
                    }
                    if (sourceImage.GetPixel(next.point.X, next.point.Y) == borderColor)
                    {
                        //Кладем в список
                        border.Add(next);
                        if (cur.preDir == 2 && next.preDir == 6 || cur.preDir == 6 && next.preDir == 2)
                        {
                            border.Add(next);
                        }
                        else if (cur.preDir == 1 && next.preDir == 7 || cur.preDir == 5 && next.preDir == 3)
                        {
                            border.Add(next);
                        }
                        else if (cur.preDir == 4 && next.preDir == 0 || cur.preDir == 4 && next.preDir == 4)
                        {
                            border.Add(next);
                        }
                        cur  = next;
                        next = new BorderPoint(-1, -1, -1);
                        break;
                    }
                    lookTo = (lookTo + 1) % 8;
                } while (lookTo != t);
            } while (next.point != start.point);

            if (c != Color.Empty)
            {
                foreach (var x in border)
                {
                    sourceImage.SetPixel(x.point.X, x.point.Y, c);
                }
                return(null);
            }
            else
            {
                border.Sort(CompareBorderPoints);

                return(border);
            }
        }