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; }
private double ComputeDataTerm(ImgGradient gradient, Coordinate<double> normal) { var ux = gradient.YMagnitude; var uy = -gradient.XMagnitude; return Math.Abs(ux*normal.Y - uy*normal.X); }