public static GrayscaleFloatImage Hysteresis(ImageEdges edges, float tMin) { var magnitude = edges.Image; GrayscaleFloatImage dest = new GrayscaleFloatImage(magnitude.Width, magnitude.Height); float maxBrightness = 256; var queue = new Queue <Point>(edges.Points); while (queue.Count > 0) { var currentPoint = queue.Dequeue(); var x = currentPoint.X; var y = currentPoint.Y; dest[x, y] = maxBrightness; var xList = Enumerable.Range(x - 1, 3).ToList(); var yList = Enumerable.Range(y - 1, 3).ToList(); var points = xList.SelectMany(i => yList.Select(j => new Point() { X = i, Y = j })) .Where(p => p.X >= 0 && p.X <= magnitude.Width - 1 && p.Y >= 0 && p.Y <= magnitude.Height - 1).ToList(); foreach (var point in points) { if (dest[point.X, point.Y] == 0 && magnitude[point.X, point.Y] >= tMin) { queue.Enqueue(point); } } } return(dest); }
public static ImageEdges NonMaximumSuppression(GrayscaleFloatImage magnitude, GrayscaleFloatImage directions, float tMax) { ImageEdges dest = new ImageEdges(); dest.Image = new GrayscaleFloatImage(magnitude.Width, magnitude.Height); dest.Points = new List <Point>(); List <float> allvals = new List <float>(); for (int y = 0; y < magnitude.Height; y++) { for (int x = 0; x < magnitude.Width; x++) { allvals.Add(magnitude[x, y]); int direction = (int)directions[x, y]; var pointList = new List <Point>(); var xList = Enumerable.Range(x - 1, 3).ToList(); var yList = Enumerable.Range(y - 1, 3).ToList(); if (direction == 0) { pointList = xList.Where(n => n >= 0 && n <= magnitude.Width - 1) .Select(n => new Point() { X = n, Y = y }) .ToList(); } else if (direction == 90) { pointList = yList.Where(n => n >= 0 && n <= magnitude.Height - 1) .Select(n => new Point() { X = x, Y = n }) .ToList(); } else { if (direction == 45) { xList.Reverse(); } pointList = xList.Zip(yList, (first, second) => new Point() { X = first, Y = second }) .Where(p => p.X >= 0 && p.X <= magnitude.Width - 1 && p.Y >= 0 && p.Y <= magnitude.Height - 1) .ToList(); } var values = new List <float>(); foreach (var point in pointList) { values.Add(magnitude[point.X, point.Y]); } if (values.Max() > magnitude[x, y]) { dest.Image[x, y] = 0; } else { dest.Image[x, y] = magnitude[x, y]; if (dest.Image[x, y] >= tMax) { dest.Points.Add(new Point() { X = x, Y = y }); } } } } var a = allvals.Max(); var b = allvals.Min(); return(dest); }