C# Image to PathGeometry

Brandon Boone 31 Reputation points
2022-03-10T17:32:38.153+00:00

I am trying to Convert an Image to a PathGeometry. I upload a picture, and then I do canny edge detection and now I want to convert that in to a PathGeometry. My over all plan it to fill it with different colors.

So I tried this :

  public Shape MakeSape(Brush customColor, double[,] image)
     {
     Path graphPath = new Path();
     graphPath.Fill = customColor;
            graphPath.Stroke = Brushes.Black;
            PathFigure pf = new PathFigure();
            pf.StartPoint = new Point(0, 0);
            pf.IsClosed = true;
            int row = image.GetLength(0);
            int column = image.GetLength(1);

            for (int y = 0; y < row; y++)
            {
                for (int x = 0; x < column; x++)
                {
                    if (image[y, x] >= LowThreshold)
                    {
                        pf.Segments.Add(new LineSegment(new Point(x, y), true));
                    }
                }

            }

            PathGeometry pg = new PathGeometry();
            pg.Figures.Add(pf);
            graphPath.Data = pg;

            ImageAsShape = graphPath;
            return graphPath;
        }

And that looked like it should work, but I am just getting all black.

Developer technologies Windows Presentation Foundation
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-03-11T17:33:50.307+00:00

    You can see the source code of Vectorization (not simple...)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.