C# 上的边缘检测
我有一个像这样的黑白图像(颜色叠加是我的,可以删除): 我需要找出所示手的边缘,我该怎么做?
我当前的算法:
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;
}
}
}
产生相当明确的轮廓,但如果 和 曲线在任何地方,则不会检测到该边缘。即,如果我将手横向握住,我会得到上指和下指的边缘,但仅此而已。
我可以做些什么来纠正这个问题并获得真正的优势?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这样的项目:http://code.google.com/p/aforge/ 这会对你有很大帮助,而且你不必重新发明轮子!
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!
C++ 上有一个解决方案 http://outliner.codeplex.com/
但将其转换为C#并不是一件容易的事,算法相当复杂。
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.