Search code examples
c#computer-visionedge-detection

Edge detection on C#


I have a black and white image like this (color overlays are mine, and can be removed): enter image description here I need to figure out the edge of the hand shown, how can I do that?

My current algorithm:

        List<Point> edgePoints = new List<Point>();
        for (int x = 0; x < largest.Rectangle.Width && edgePoints.Count == 0; x++) {
            //top
            for (int y = 0; y < largest.Rectangle.Height - 3 && edgePoints.Count == 0; y++) {
                if (colorGrid[x, y].ToArgb() == Color.White.ToArgb() &&
                    colorGrid[x, y + 1].ToArgb() == Color.White.ToArgb() &&
                    colorGrid[x, y + 2].ToArgb() == Color.White.ToArgb() &&
                    colorGrid[x, y + 3].ToArgb() == Color.White.ToArgb()
                    ) {
                    edgePoints.Add(new Point(x, y));
                    //g.DrawLine(new System.Drawing.Pen(Color.Orange), new Point(largest.Rectangle.X + x, largest.Rectangle.Y + y), new Point(largest.Rectangle.X + x, largest.Rectangle.Y + y + 3));
                    break;
                }
            }
            //bottom
            for (int y = largest.Rectangle.Height - 1; y > 3 && edgePoints.Count == 0; y++) {
                if (colorGrid[x, y].ToArgb() == Color.White.ToArgb() &&
                    colorGrid[x, y - 1].ToArgb() == Color.White.ToArgb() &&
                    colorGrid[x, y - 2].ToArgb() == Color.White.ToArgb() &&
                    colorGrid[x, y - 3].ToArgb() == Color.White.ToArgb()
                    ) {
                    edgePoints.Add(new Point(x, y));
                    //g.DrawLine(new System.Drawing.Pen(Color.Orange), new Point(largest.Rectangle.X + x, largest.Rectangle.Y + y), new Point(largest.Rectangle.X + x, largest.Rectangle.Y + y + 3));
                    break;
                }
            }
        }

Results in a fairly well defined outline, but if the and curves in anywhere, that edge is not detected. I.E., if I held my hand sideways, I'd get the edge of the top finger and bottom finger, but that's it.

What can I do to correct this and get a real edge?


Solution

  • Have a look on projects like this: http://code.google.com/p/aforge/ that will help you a lot and you dont have to reinvent the wheel!