private static bool IsPositionWithinImage(BinaryImage image, MatrixPosition position) { return(position.M >= 0 && position.N >= 0 && position.M < image.Size.Height && position.N < image.Size.Width); }
/// <summary> /// Returns the coordinates of the pixels that lie on the sampling line, which is defined by the /// <paramref name="startingPosition"/> and its <paramref name="angle"/> to the edge of the /// <paramref name="image"/>. The <paramref name="startingPosition"/> will not be returned in the /// result set. /// </summary> /// <remarks> /// <example> /// Here's an example using the angle of 45 degrees. /// <![CDATA[ /// ^ /// | E E = edge of the image /// | \ \ = sampling line /// | \ c = center /// | \ /// | \ /// | \ /// | c /// | /// . /// . /// . ---------------------------> /// ]]> /// </example> /// </remarks> internal static SamplingLine GetSamplingLineForAngle( BinaryImage image, MatrixPosition startingPosition, int angle) { const int MaxIterationLimit = 50000; var samplingLine = new SamplingLine(); MatrixPosition nextPosition; double phi = (angle + 180d) * Math.PI / 180d; int r = 1; do { var deltaN = (int)Math.Round(r * Math.Cos(phi), 0, MidpointRounding.ToZero); var deltaM = (int)Math.Round(r * Math.Sin(phi), 0, MidpointRounding.ToZero); nextPosition = new MatrixPosition(startingPosition.M + deltaM, startingPosition.N + deltaN); if (nextPosition != startingPosition // ReSharper disable once SimplifyLinqExpressionUseAll && !samplingLine.ContainsPosition(nextPosition) && IsPositionWithinImage(image, nextPosition)) { var pointOfInterest = new SamplingPoint(nextPosition, r); samplingLine.Add(pointOfInterest); } r++; } while (IsPositionWithinImage(image, nextPosition) && r < MaxIterationLimit); return(samplingLine); }
public void GetsTheCoordinatesOfAllPixelsOnALineDefinedByAStartingPointAndAngle0( string testcaseName, int angle, MatrixPosition[] expectedResult) { //// [ (0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (0,6) (0,7) (0,8) (0,9) ] //// [ (1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9) ] //// [ (2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9) ] //// [ (3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9) ] //// [ (4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9) ] //// [ (5,0) (5,1) (5,2) (5,3) (5,4) X (5,6) (5,7) (5,8) (5,9) ] x = Starting Point (m,n) //// [ (6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9) ] //// [ (7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9) ] //// [ (8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9) ] //// [ (9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9) ] var inputImage = new BinaryImage(10, 10); var startingPoint = new MatrixPosition(5, 5); var result = SignatureReader.GetSamplingLineForAngle( inputImage, startingPoint, angle); var resultingPositions = result.SamplingPoints.Select(x => x.Position).ToArray(); resultingPositions.Should().ContainInOrder(expectedResult, testcaseName); resultingPositions.Should().HaveCount(expectedResult.Length); }
/// <summary> /// Shrinks the <paramref name="image"/> by half its size and returns the new image as result. /// </summary> internal static BinaryImage ShrinkByHalf(BinaryImage image) { var downSizedImage = new BinaryImage(image.Size.Width / 2, image.Size.Height / 2); var matrixSize = new Size(2, 2); Parallel.For( 0, downSizedImage.Size.Height, downSizedM => { int originalM = downSizedM * 2; for (int downSizedN = 0; downSizedN < downSizedImage.Size.Width; downSizedN++) { int originalN = downSizedN * 2; var positionInImage = new MatrixPosition(originalM, originalN); var neighbourMatrixFromPosition = image.GetNeighborMatrixFromPosition(positionInImage, matrixSize); float averageValue = neighbourMatrixFromPosition.GetAverageValue(); downSizedImage[downSizedM, downSizedN] = averageValue >= 0.75f ? BinaryImage.Black : BinaryImage.White; } }); return(downSizedImage); }
public void GetsNeighbourMatrixFromPosition( BinaryImage binaryImage, MatrixPosition positionInImage, Size sizeOfNeighbourMatrix, BinaryMatrix expectedResult) { var result = binaryImage.GetNeighborMatrixFromPosition(positionInImage, sizeOfNeighbourMatrix); result.Should().Be(expectedResult); }