public static Rectangle AdjustedToImageEdges(this Rectangle originRect, Rectangle imageRect) { var rect = new Rectangle(originRect.UpperLeft, originRect.LowerRight); if (!imageRect.IsInside(rect.UpperLeft)) { if (rect.UpperLeft.X < imageRect.UpperLeft.X) { rect.UpperLeft.X = imageRect.UpperLeft.X; } if (rect.UpperLeft.Y < imageRect.UpperLeft.Y) { rect.UpperLeft.Y = imageRect.UpperLeft.Y; } } if (!imageRect.IsInside(rect.LowerRight)) { if (rect.LowerRight.X > imageRect.LowerRight.X) { rect.LowerRight.X = imageRect.LowerRight.X; } if (rect.LowerRight.Y > imageRect.LowerRight.Y) { rect.LowerRight.Y = imageRect.LowerRight.Y; } } return rect; }
/// <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); }