Edge detection on C#

假装没事ソ 提交于 2019-12-10 20:40:03

问题


I have a black and white image like this (color overlays are mine, and can be removed):

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?


回答1:


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!




回答2:


There is a solution on C++ http://outliner.codeplex.com/ But it will not be an easy task to convert it to C#, the algorithm is quite complex.



来源:https://stackoverflow.com/questions/4899987/edge-detection-on-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!