예제 #1
0
        static Edge GoHorizontal(int row, int column, Gradients gradients, Arr edgeMap, Edges edges, int rows, int columns, int minLength)
        {
            var left  = GoLeft(row, column);
            var right = GoRight(row, column);

            if (left.Count >= minLength)
            {
                if (right.Count >= minLength)
                {
                    left.AddLastFrom(right);
                }
                return(left);
            }
            else if (right.Count >= minLength)
            {
                return(right);
            }
            else
            {
                return(new Edge());
            }

            Edge GoLeft(int r, int c)
            {
                var    edge = new Edge();
                double upGradient, straightGradient, downGradient, direction, edgel;

                do
                {
                    edgeMap.SetReal(r, c, 255);
                    edge.AddLast(new Point(c, r));

                    --c; // left
                    if (c == 0)
                    {
                        return(edge);
                    }

                    upGradient       = gradients.GradientMap.GetReal(r - 1, c);
                    straightGradient = gradients.GradientMap.GetReal(r, c);
                    downGradient     = gradients.GradientMap.GetReal(r + 1, c);

                    if (upGradient > straightGradient && upGradient > downGradient)
                    {
                        --r; // up
                        if (r == 0)
                        {
                            return(edge);
                        }
                    }
                    else if (downGradient > straightGradient && downGradient > upGradient)
                    {
                        ++r; // down
                        if (r == r - 1)
                        {
                            return(edge);
                        }
                    }
                    else if (straightGradient == 0)
                    {
                        return(edge);
                    }

                    edgel = edgeMap.GetReal(r, c);
                    if (edgel != 0)
                    {
                        return(edge);
                    }

                    direction = gradients.DirectionMap.GetReal(r, c);
                }while(direction == Gradients.HorizontalValue);

                // go vertical
                var vertical = GoVertical(r, c, gradients, edgeMap, edges, rows, columns, minLength);

                if (!vertical.IsEmpty)
                {
                    var verticalHead = vertical.First.Value;
                    if (verticalHead.X == c && verticalHead.Y == r)
                    {
                        edge.AddLastFrom(vertical);
                    }
                    else
                    {
                        var verticalTail = vertical.Last.Value;
                        if (verticalTail.X == c && verticalTail.Y == r)
                        {
                            edge.AddLastFrom(vertical, true);
                        }
                        else
                        {
                            edges.Add(vertical);
                        }
                    }
                }

                if (edge.Count >= minLength)
                {
                    return(edge);
                }

                return(new Edge());
            }

            Edge GoRight(int r, int c)
            {
                var    edge = new Edge();
                double upGradient, straightGradient, downGradient, direction, edgel;

                do
                {
                    edgeMap.SetReal(r, c, 255);

                    ++c; // right
                    if (c == columns - 1)
                    {
                        return(edge);
                    }

                    upGradient       = gradients.GradientMap.GetReal(r - 1, c);
                    straightGradient = gradients.GradientMap.GetReal(r, c);
                    downGradient     = gradients.GradientMap.GetReal(r + 1, c);

                    if (upGradient > straightGradient && upGradient > downGradient)
                    {
                        --r; // up
                        if (r == 0)
                        {
                            return(edge);
                        }
                    }
                    else if (downGradient > straightGradient && downGradient > upGradient)
                    {
                        ++r; // down
                        if (r == rows - 1)
                        {
                            return(edge);
                        }
                    }
                    else if (straightGradient == 0)
                    {
                        return(edge);
                    }

                    edgel = edgeMap.GetReal(r, c);
                    if (edgel != 0)
                    {
                        return(edge);
                    }

                    direction = gradients.DirectionMap.GetReal(r, c);
                }while(direction == Gradients.HorizontalValue);

                // go vertical
                var vertical = GoVertical(r, c, gradients, edgeMap, edges, rows, columns, minLength);

                if (!vertical.IsEmpty)
                {
                    var verticalHead = vertical.First.Value;
                    if (verticalHead.X == c && verticalHead.Y == r)
                    {
                        edge.AddLastFrom(vertical);
                    }
                    else
                    {
                        var verticalTail = vertical.Last.Value;
                        if (verticalTail.X == c && verticalTail.Y == r)
                        {
                            edge.AddLastFrom(vertical, true);
                        }
                        else
                        {
                            edges.Add(vertical);
                        }
                    }
                }

                if (edge.Count >= minLength)
                {
                    return(edge);
                }

                return(new Edge());
            }
        }