예제 #1
0
        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);
        }