请问C# 如何使用Emgucv 的canny去进行区域截取,类似于魔术棒。
或者不使用Emgucv,直接用漫洪填充算法实现。我在网上看到这个算法,但是这个算法的返回值会将其他非填充区域的图形改为黑色。如何让非填充区域不变呢。本人初学C#,不甚了解。
void FloodFill(ImageRgb24 img, Point location, Rgb24 backColor, Rgb24 fillColor)
{
int width = img.Width;
int height = img.Height;
if (location.X < 0 || location.X >= width || location.Y < 0 || location.Y >= height) return; if (backColor == fillColor) return;
if (img[location.Y, location.X] != backColor) return; Stack<Point> points = new Stack<Point>();
points.Push(location); int ww = width -1;
int hh = height -1; while (points.Count > 0)
{
Point p = points.Pop();
img[p.Y, p.X] = fillColor;
if (p.X > 0 && img[p.Y, p.X - 1] == backColor)
{
img[p.Y, p.X - 1] = fillColor;
points.Push(new Point(p.X - 1, p.Y));
} if (p.X < ww && img[p.Y, p.X + 1] == backColor)
{
img[p.Y, p.X + 1] = fillColor;
points.Push(new Point(p.X + 1, p.Y));
} if (p.Y > 0 && img[p.Y - 1, p.X] == backColor)
{
img[p.Y - 1, p.X] = fillColor;
points.Push(new Point(p.X, p.Y - 1));
} if (p.Y < hh && img[p.Y + 1, p.X] == backColor)
{
img[p.Y + 1, p.X] = fillColor;
points.Push(new Point(p.X, p.Y + 1));
}
}
}
或者不使用Emgucv,直接用漫洪填充算法实现。我在网上看到这个算法,但是这个算法的返回值会将其他非填充区域的图形改为黑色。如何让非填充区域不变呢。本人初学C#,不甚了解。
void FloodFill(ImageRgb24 img, Point location, Rgb24 backColor, Rgb24 fillColor)
{
int width = img.Width;
int height = img.Height;
if (location.X < 0 || location.X >= width || location.Y < 0 || location.Y >= height) return; if (backColor == fillColor) return;
if (img[location.Y, location.X] != backColor) return; Stack<Point> points = new Stack<Point>();
points.Push(location); int ww = width -1;
int hh = height -1; while (points.Count > 0)
{
Point p = points.Pop();
img[p.Y, p.X] = fillColor;
if (p.X > 0 && img[p.Y, p.X - 1] == backColor)
{
img[p.Y, p.X - 1] = fillColor;
points.Push(new Point(p.X - 1, p.Y));
} if (p.X < ww && img[p.Y, p.X + 1] == backColor)
{
img[p.Y, p.X + 1] = fillColor;
points.Push(new Point(p.X + 1, p.Y));
} if (p.Y > 0 && img[p.Y - 1, p.X] == backColor)
{
img[p.Y - 1, p.X] = fillColor;
points.Push(new Point(p.X, p.Y - 1));
} if (p.Y < hh && img[p.Y + 1, p.X] == backColor)
{
img[p.Y + 1, p.X] = fillColor;
points.Push(new Point(p.X, p.Y + 1));
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货