public void ConvertTest2() { // Create a matrix representation // of a 4x4 image with a inner 2x2 // square drawn in the middle double[,] pixels = { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 }, }; // Create the converter to convert the matrix to a image var conv = new MatrixToBitmapSource(); // Declare a bitmap source and store the pixels on it BitmapSource image; conv.Convert(pixels, out image); var conv2 = new MatrixToImage(); Bitmap image2; conv2.Convert(pixels, out image2); Assert.AreEqual(pixels, image.ToMatrix(0)); Assert.AreEqual(pixels, image2.ToMatrix(0)); Assert.AreEqual(PixelFormats.Gray32Float, image.Format); Assert.AreEqual(System.Drawing.Imaging.PixelFormat.Format8bppIndexed, image2.PixelFormat); }
public void ConvertTest2() { // Load a test image BitmapSource sourceImage = TestTools.GetImage("image1.bmp"); // Make sure values are binary sourceImage = new Threshold().Apply(sourceImage); // Create the converters var imageToMatrix = new BitmapSourceToMatrix(); var matrixToImage = new MatrixToBitmapSource(); // Convert to matrix double[,] matrix; // initialization is not needed imageToMatrix.Convert(sourceImage, out matrix); // Revert to image BitmapSource resultImage; // initialization is not needed matrixToImage.Convert(matrix, out resultImage); Assert.AreEqual(PixelFormats.Gray32Float, resultImage.Format); UnmanagedImage img1 = sourceImage.ToUnmanagedImage(); UnmanagedImage img2 = resultImage.ToUnmanagedImage(); List <IntPoint> p1 = img1.CollectActivePixels(); List <IntPoint> p2 = img2.CollectActivePixels(); bool equals = new HashSet <IntPoint>(p1).SetEquals(p2); Assert.IsTrue(equals); }
public void ConvertTest1() { var target = new MatrixToBitmapSource(); double[,] pixels = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 1 { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, // 2 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 3 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 4 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 5 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 6 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 7 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 8 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 9 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 10 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 11new Bitmap(Properties { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 12 { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, // 13 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 14 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 15 }; BitmapSource imageActual = pixels.ToBitmapSource(); double[,] actual = imageActual.ToMatrix(0); BitmapSource imageSourceExpected = TestTools.GetImage("image1.bmp"); Bitmap imageExpected = imageSourceExpected.ToBitmap(); new Threshold().ApplyInPlace(imageExpected); new Invert().ApplyInPlace(imageExpected); double[,] expected = imageExpected.ToMatrix(0); for (int i = 0; i < pixels.GetLength(0); i++) { for (int j = 0; j < pixels.GetLength(1); j++) { Assert.AreEqual(actual[i, j], expected[i, j]); } } }
public void ConvertTest() { var target = new MatrixToBitmapSource(); byte[,] input = { { 0, 0, 0 }, { 0, 128, 0 }, { 0, 0, 128 }, }; UnmanagedImage bitmap = input.ToBitmapSource().ToUnmanagedImage(); var pixels = bitmap.CollectActivePixels(); Assert.AreEqual(2, pixels.Count); Assert.IsTrue(pixels.Contains(new IntPoint(1, 1))); Assert.IsTrue(pixels.Contains(new IntPoint(2, 2))); }