public void CreatesNewFromMatrixString() { const string SampleMatrixString = "010\r\n101\r\n110"; var expected = new BinaryMatrix( new byte[, ] { { 0, 1, 0 }, { 1, 0, 1 }, { 1, 1, 0 } }); var result = BinaryMatrix.FromMatrixString(SampleMatrixString); result.Should().Be(expected); }
public void DisplaysContentAsMatrixString() { const string Expected = "010\r\n101\r\n110"; var sampleMatrix = new BinaryMatrix( new byte[, ] { { 0, 1, 0 }, { 1, 0, 1 }, { 1, 1, 0 } }); var result = sampleMatrix.ToString(); result.Should().Be(Expected); }
/// <summary> /// Applies a smooth filter on <paramref name="binaryImage"/> to remove noise and create /// more distinguishable lines. /// </summary> public static BinaryImage Smoothing(BinaryImage binaryImage) { // Defines the size of the matrix used to remove the noises in a black and white image. // NOTE: the size should be at least 3 and an odd number const int MatrixSize = 7; // Needed for edge cases of the image border. const int MatrixRadius = MatrixSize / 2; var img = binaryImage.Clone(); Parallel.For( MatrixRadius, img.Size.Height - MatrixRadius, m => { for (int n = MatrixRadius; n < img.Size.Width - MatrixRadius; n++) { // 1 1 0 // 1 x 0 -> x is the average of all values around. // 0 0 0 var matrixColors = new BinaryMatrix(new Size(MatrixSize, MatrixSize)); for (var matrixM = 0; matrixM < MatrixSize; matrixM++) { for (var matrixN = 0; matrixN < MatrixSize; matrixN++) { int imageM = m - MatrixRadius + matrixM; int imageN = n - MatrixRadius + matrixN; matrixColors[matrixM, matrixN] = img[imageM, imageN]; } } float average = matrixColors.GetAverageValue(); img[m, n] = average >= 0.5f ? BinaryImage.Black : BinaryImage.White; } }); return(img); }
public void MatchesMatrixToStructuringElement() { var structuringElement = new StructuringElement( new[, ] { { 0, 0, 0 }, { -1, -1, -1 }, { 1, 1, 1 } }); var matchingMatrix = new BinaryMatrix( new byte[, ] { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 1 } }); bool result = Thinner.MatchMatrixToStructuringElement( matchingMatrix, structuringElement); result.Should().BeTrue(); }