public Rectangle MinDistanceRegion(Rectangle sourceRect, Drawable drawable, SelectionMask selectionMask) { Debug.Assert(sourceRect.Width == sourceRect.Height); var windowSize = sourceRect.Width; var windowDelta = windowSize/2; Rectangle minRegionRect = null; var minDistance = 0.0; var sourceRgn = new PixelRgn(drawable, sourceRect.AdjustedToImageEdges(drawable.Bounds), false, false); var sourceArray = sourceRgn.ToArrayWithoutSelection(selectionMask); var currentCoordinate = new IntCoordinate(0, 0); for (int i = windowDelta; i < drawable.Width - windowDelta; i++) { for (int j = windowDelta; j < drawable.Height - windowDelta; j++) { if (j == sourceRect.Y1 + windowDelta && i == sourceRect.X1 + windowDelta) { continue; } currentCoordinate.X = i; currentCoordinate.Y = j; var currentRgnRect = currentCoordinate.PointCenteredRectangle(windowSize); var dist = RegionDistance(sourceArray, currentRgnRect, drawable, selectionMask); if (dist == null) { continue; } if (dist == 0) { return currentRgnRect; } if ((minDistance > dist || minRegionRect == null)) { minDistance = (double) dist; minRegionRect = currentRgnRect; } } } return minRegionRect; }
public Rectangle MinDistanceRegion(Rectangle sourceRect, ImgGradient gradient, Pixel[,] layerPixels, SelectionMask selectionMask) { Debug.Assert(sourceRect.Width == sourceRect.Height); var windowSize = sourceRect.Width; var windowDelta = windowSize/2; var layerWidth = layerPixels.GetLength(0); var layerHeight = layerPixels.GetLength(1); Rectangle minRegionRect = null; var minDistance = 0.0; var adjustedSourceRect = sourceRect.AdjustedToImageEdges(new Rectangle(new IntCoordinate(0, 0), layerWidth, layerHeight)); var currentCoordinate = new IntCoordinate(0, 0); var orthoGradient = new Coordinate<double>(-gradient.YMagnitude*windowDelta, gradient.XMagnitude*windowDelta); for (int i = windowDelta; i < layerWidth - windowDelta; i++) { for (int j = windowDelta; j < layerHeight - windowDelta; j++) { if (j == sourceRect.Y1 + windowDelta && i == sourceRect.X1 + windowDelta) { continue; } currentCoordinate.X = i; currentCoordinate.Y = j; var currentRgnRect = currentCoordinate.PointCenteredRectangle(windowSize); var dist = RegionDistance(adjustedSourceRect, currentRgnRect, layerPixels, selectionMask); if (dist == null) { continue; } if (dist == 0) { return currentRgnRect; } if ((minDistance > dist || minRegionRect == null)) { minDistance = (double) dist; minRegionRect = currentRgnRect; } } } return minRegionRect; }
/// <summary> /// Calculate new confidance value of pixel at given coordinate /// </summary> /// <param name="coordinate"></param> /// <param name="pxConfidenceTerm"></param> /// <param name="windowSize"></param> private static double NewConfidanceValue(IntCoordinate coordinate, double[,] pxConfidenceTerm, int windowSize) { var imageRect = new Rectangle(new IntCoordinate(0, 0), pxConfidenceTerm.GetLength(0), pxConfidenceTerm.GetLength(1)); //TODO: handle boundary regions properly return RegionHelper.Grid(coordinate.PointCenteredRectangle(windowSize)) .Select(coordPrim => imageRect.IsInside(coordPrim) ? pxConfidenceTerm[coordPrim.X, coordPrim.Y] : 0) .Sum()/(windowSize*windowSize); }