Ejemplo n.º 1
0
        public void TestByRegion(string resourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Rectangle region = new(0, 0, sourceImage.Width / 2, sourceImage.Height / 2);

            // Crop region of image
            using Image croppedImage = ImageCrop.ByRegion(sourceImage, region);

            Assert.IsNotNull(croppedImage);

            // Check size
            Assert.AreEqual(region.Width, croppedImage.Width);
            Assert.AreEqual(region.Height, croppedImage.Height);

            // Get bytes for each image
            byte[] sourceBytes  = ImageBytes.FromImage(sourceImage);
            byte[] croppedBytes = ImageBytes.FromImage(sourceImage);

            // Compare to ensure correct region cropped
            int limit = croppedImage.Width * croppedImage.Height;

            for (int i = 0; i < limit; i++)
            {
                Assert.AreEqual(sourceBytes[i], croppedBytes[i]);
            }
        }
Ejemplo n.º 2
0
        public void TestAsBytes(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            const byte Threshold = 0x7F;

            // Get bytes for image
            byte[] imageBytes = ImageBinary.AsBytes(sourceImage, Threshold);

            // Check we have some bytes
            Assert.IsNotNull(imageBytes);

            // Check all converted to binary (0 or 1)
            Assert.IsTrue(imageBytes.All(b => b < 0x02));

            // Check correct number of bytes after conversion
            // (Same per source image)
            Assert.AreEqual(118287, imageBytes.Length);

            // Count 1's and 0's
            int countZero = imageBytes.Count(b => b == 0x00);
            int countOne  = imageBytes.Count(b => b == 0x01);

            // May be slight variances due to compression
            // but should be similar.
            Assert.IsTrue(countZero > 52000 && countZero < 52100);
            Assert.IsTrue(countOne > 66200 && countOne < 66300);
        }
Ejemplo n.º 3
0
        public void TestCombineAllSame()
        {
            string resourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);
            Assert.IsNotNull(sourceImage);

            using Image sourceImageCopy = TestImage.FromResource(resourcePath);
            Assert.IsNotNull(sourceImageCopy);

            Image[] imagesToCombine = new Image[]
            {
                sourceImage,
                sourceImageCopy
            };

            // Combine
            Bitmap combinedImage = ImageCombine.All(imagesToCombine);

            // Convert for byte comparison
            Bitmap sourceBitmap = ImageFormatting.ToBitmap(sourceImage);

            // Compare images
            Assert.IsTrue(TestImage.Compare(sourceBitmap, combinedImage));
        }
Ejemplo n.º 4
0
        public void TestFromSourceToDestination(string resourcePath)
        {
            // Load source image
            using Bitmap sourceBitmap = TestImage.FromResource(resourcePath) as Bitmap;

            Assert.IsNotNull(sourceBitmap);

            // Create blank bitmap
            using Bitmap copyBitmap = new(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat);

            // Crop region of image
            ImageCopy.FromSourceToDestination(sourceBitmap, copyBitmap);

            // Check size
            Assert.AreEqual(sourceBitmap.Width, copyBitmap.Width);
            Assert.AreEqual(sourceBitmap.Height, copyBitmap.Height);

            // Get bytes for each image
            byte[] sourceBytes = ImageBytes.FromImage(sourceBitmap);
            byte[] copiedBytes = ImageBytes.FromImage(copyBitmap);

            // Compare to ensure correct copy matches
            int limit = System.Math.Min(sourceBytes.Length, copiedBytes.Length);

            for (int i = 0; i < limit; i++)
            {
                Assert.AreEqual(sourceBytes[i], copiedBytes[i]);
            }
        }
Ejemplo n.º 5
0
        public void TestBytesGetMaxValue()
        {
            const string ResourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            using Image image = TestImage.FromResource(ResourcePath);

            byte max = ImageBytes.GetMaxValue(image);

            Assert.AreEqual(0xff, max);
        }
Ejemplo n.º 6
0
        public void TestCombineAllWithNegative()
        {
            string resourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);
            Assert.IsNotNull(sourceImage);

            using Image negativeCopy = ImageColor.ToNegative(sourceImage);
            Assert.IsNotNull(negativeCopy);

            Image[] imagesToCombine = new Image[]
            {
                sourceImage,
                negativeCopy
            };

            Bitmap combinedImage = ImageCombine.All(imagesToCombine);

            // Get bytes for images
            byte[] combinedBytes = ImageBytes.FromImage(combinedImage);

            // Just check first row of bytes has been combined
            for (int i = 0; i < combinedImage.Width; i++)
            {
                Assert.AreEqual(byte.MaxValue, combinedBytes[i]);
            }

            int pixelDepth = 3;
            int stride     = 1056;
            int width      = 1053;
            int height     = combinedImage.Height;

            int limit = combinedBytes.Length - 4;

            // Compare combined bytes excluding stride padding
            for (int y = 0; y < height; y++)
            {
                int offset = y * stride;

                for (int x = 0; x < width; x += pixelDepth)
                {
                    int i = offset + x;

                    if (i < limit)
                    {
                        Assert.AreEqual(byte.MaxValue, combinedBytes[i]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void TestBytesGetMinMaxValue()
        {
            const string ResourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            using Image image = TestImage.FromResource(ResourcePath);

            Tuple <byte, byte> minMax = ImageBytes.GetMinMaxValue(image);

            Assert.AreEqual(0x00, minMax.Item1);
            Assert.AreEqual(0xff, minMax.Item2);
        }
        public void TestApplyThresholdValue(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply threshold
            using Image thresholdImage = ImageThreshold.Apply(sourceImage, 0x40);
            Assert.IsNotNull(thresholdImage);

            byte[] withoutAlphaBytes = RemoveAlphaLayerBytes(ImageBytes.FromImage(thresholdImage), sourceImage.PixelFormat);

            // Check bytes thresholded
            Assert.IsTrue(withoutAlphaBytes.Take(sourceImage.Width).All(b => b == byte.MinValue || b == byte.MaxValue));
        }
Ejemplo n.º 9
0
        public void TestCombineAllMixedColor()
        {
            using Image colorImage = TestImage.FromResource("Freedom35.ImageProcessing.Tests.Resources.clock.bmp");
            Assert.IsNotNull(colorImage);

            using Image bwImage = TestImage.FromResource("Freedom35.ImageProcessing.Tests.Resources.clock-bw.bmp");
            Assert.IsNotNull(bwImage);

            Image[] imagesToCombine = new Image[]
            {
                colorImage,
                bwImage
            };

            Assert.ThrowsException <ArgumentException>(() => ImageCombine.All(imagesToCombine));
        }
Ejemplo n.º 10
0
        public void TestBytesGetAvgValue()
        {
            const string ResourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            using Image image = TestImage.FromResource(ResourcePath);

            byte avg = ImageBytes.GetAverageValue(image);

            // Average for image
            Assert.AreEqual(0x8d, avg);

            avg = ImageBytes.GetAverageValue(image, 0x22, 0x24);

            // Average in range
            Assert.AreEqual(0x23, avg);
        }
Ejemplo n.º 11
0
        public void TestImageResizeAsNewByRatio(string resourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Bitmap bitmap = sourceImage as Bitmap;

            Assert.IsNotNull(bitmap);

            using Image resizedImage = ImageResize.ResizeAsNew(bitmap, 2.0);

            Assert.IsNotNull(resizedImage);
            Assert.AreEqual(bitmap.Width * 2, resizedImage.Width);
            Assert.AreEqual(bitmap.Height * 2, resizedImage.Height);
        }
        public void TestApplyMin(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply min value
            using Image minImage = ImageThreshold.ApplyMin(sourceImage, 0x20);

            Assert.IsNotNull(minImage);

            byte[] withoutAlphaBytes = RemoveAlphaLayerBytes(ImageBytes.FromImage(minImage), sourceImage.PixelFormat);

            // Check all greater than value
            Assert.IsTrue(withoutAlphaBytes.All(b => b >= 0x20));
        }
        public void TestApplyMinMax(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply min/max value
            using Image minMaxImage = ImageThreshold.ApplyMinMax(sourceImage, 0x25, 0xc3);

            Assert.IsNotNull(minMaxImage);

            byte[] withoutAlphaBytes = RemoveAlphaLayerBytes(ImageBytes.FromImage(minMaxImage), sourceImage.PixelFormat);

            // Check all within range
            Assert.IsTrue(withoutAlphaBytes.All(b => b >= 0x25 && b <= 0xc3));
        }
        public void TestOtsuThreshold(string sourceResourcePath, string resultResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply Thresholding
            using Image thresholdImage = ImageThreshold.ApplyOtsuMethod(sourceImage);

            Assert.IsNotNull(thresholdImage);

            // Load correct result image
            using Image resultImage = TestImage.FromResource(resultResourcePath);

            Assert.IsNotNull(resultImage);

            // Compare images
            Assert.IsTrue(TestImage.Compare(thresholdImage, resultImage));
        }
Ejemplo n.º 15
0
        public void TestImageResizeAsNew(string resourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Bitmap bitmap = sourceImage as Bitmap;

            Assert.IsNotNull(bitmap);

            // Change original size
            int width  = bitmap.Width / 2;
            int height = bitmap.Height / 2;

            using Image resizedImage = ImageResize.ResizeAsNew(bitmap, width, height);

            Assert.IsNotNull(resizedImage);
            Assert.AreEqual(width, resizedImage.Width);
            Assert.AreEqual(height, resizedImage.Height);
        }
Ejemplo n.º 16
0
        public void TestCombineAllOne()
        {
            using Image sourceImage = TestImage.FromResource("Freedom35.ImageProcessing.Tests.Resources.clock.bmp");
            Assert.IsNotNull(sourceImage);

            Image[] imagesToCombine = new Image[]
            {
                sourceImage
            };

            // Combine
            Bitmap combinedBitmap = ImageCombine.All(imagesToCombine);

            // Convert for byte comparison
            byte[] sourceBytes   = ImageBytes.FromImage(sourceImage);
            byte[] combinedBytes = ImageBytes.FromImage(combinedBitmap);

            // Should return the same image when only combining one
            for (int i = 0; i < sourceImage.Width; i++)
            {
                Assert.AreEqual(sourceBytes[i], combinedBytes[i]);
            }
        }
Ejemplo n.º 17
0
        public void TestImageResizeOriginal(string resourcePath)
        {
            // Load source image
            Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Bitmap bitmap = sourceImage as Bitmap;

            Assert.IsNotNull(bitmap);

            // Change original size
            int width  = bitmap.Width / 2;
            int height = bitmap.Height / 2;

            ImageResize.ResizeOriginal(ref bitmap, width, height);

            Assert.IsNotNull(bitmap);
            Assert.AreEqual(width, bitmap.Width);
            Assert.AreEqual(height, bitmap.Height);

            bitmap.Dispose();
        }
Ejemplo n.º 18
0
        public void TestImageResizeOriginalByRatio(string resourcePath)
        {
            // Load source image
            Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Bitmap bitmap = sourceImage as Bitmap;

            Assert.IsNotNull(bitmap);

            // Get original size
            int width  = bitmap.Width;
            int height = bitmap.Height;

            ImageResize.ResizeOriginal(ref bitmap, 3.0);

            Assert.IsNotNull(bitmap);
            Assert.AreEqual(width * 3, bitmap.Width);
            Assert.AreEqual(height * 3, bitmap.Height);

            bitmap.Dispose();
        }