/// <summary> /// Fills hole by approximation of its 8-connected non-hole pixels; Does so in a spiral fashion. /// Throws error if hole touches the edges of the image. /// </summary> public void FillHoleApproximateConnected(Hole hole) { if (hole.CoveringRectangle.TopRight.Value == -1 || hole.CoveringRectangle.BottomLeft.Value == -1 || hole.CoveringRectangle.TopLeft.Value == -1 || hole.CoveringRectangle.BottomRight.Value == -1) { throw new Exception("Better-Approximation does not work if the hole touches the edges of the image."); } var spiralTraverser = new SpiralTraverser(hole.CoveringRectangle, _image); foreach (var pix in spiralTraverser) { if (pix.Value == -1) { var array = new Pixel[] { _image.GetDistantElement(pix, 0, -1), _image.GetDistantElement(pix, -1, -1), _image.GetDistantElement(pix, -1, 0), _image.GetDistantElement(pix, -1, 1), _image.GetDistantElement(pix, 0, 1), _image.GetDistantElement(pix, 1, 1), _image.GetDistantElement(pix, 1, 0), _image.GetDistantElement(pix, 1, -1) } .Where(x => x != null && x.Value != -1); var sum = array.Sum(x => x.Value); var count = array.Count(); pix.Value = sum / count; } } _image.IsHoled = false; }