Exemplo n.º 1
0
        public void WarpAffineTransform()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.jpg");

            var src = MemoryBitmap.Load(filePath, Codecs.OpenCvCodec.Default);
            var dst = new MemoryBitmap(512, 512, src.Info.PixelFormat);

            var xform = System.Numerics.Matrix3x2.CreateScale(1.3f, 1.3f) * System.Numerics.Matrix3x2.CreateRotation(0.25f);

            xform.Translation = new System.Numerics.Vector2(5, 40);

            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"OpenCV {t}")))
            {
                using var bm = PerformanceBenchmark.Run(result => TestContext.WriteLine(result.TotalMilliseconds));

                OpenCvSharp4Toolkit.WarpAffine(src, dst, xform);
            }
            dst.Save(new AttachmentInfo("result.opencv.jpg"));

            dst.AsSpanBitmap().WritableBytes.Fill(0);
            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"Soft {t}")))
            {
                using var bm = PerformanceBenchmark.Run(result => TestContext.WriteLine(result.TotalMilliseconds));

                dst.AsSpanBitmap().SetPixels(xform, src);
            }
            dst.Save(new AttachmentInfo("result.soft.jpg"));
        }
Exemplo n.º 2
0
        public void TestTransform(bool useBilinear)
        {
            var src = LoadShannonImage().OfType <Pixel.BGR24>();

            src.Save(new AttachmentInfo("input.png"));

            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\cat.png");
            var cat00    = MemoryBitmap
                           .Load(filePath, Codecs.GDICodec.Default)
                           .OfType <Pixel.BGRA32>();


            filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\QRCode.png");
            var qrcode = MemoryBitmap
                         .Load(filePath, Codecs.GDICodec.Default)
                         .OfType <Pixel.BGRA32>();

            var x = Matrix3x2.CreateScale(0.75f, 0.75f) * Matrix3x2.CreateRotation(0.25f);

            x.Translation = new Vector2(20, 20);
            Matrix3x2.Invert(x, out var xx);

            var dst = new MemoryBitmap <Pixel.BGR24>(512, 512);

            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"Transform {(int)t.TotalMilliseconds}ms")))
            {
                dst.AsSpanBitmap().SetPixels(xx, src.AsSpanBitmap(), useBilinear);
            }

            for (float r = 0.1f; r < 1; r += 0.2f)
            {
                // 1st API

                var xform = Matrix3x2.CreateRotation(r) * Matrix3x2.CreateTranslation(50, 15);
                xform = Matrix3x2.CreateTranslation(-50, -50) * xform * Matrix3x2.CreateTranslation(50, 50);
                xform = xform * Matrix3x2.CreateScale(3, 3);

                dst.AsSpanBitmap().SetPixels(xform, cat00.AsSpanBitmap(), useBilinear, r);
                DrawBounds(dst, cat00.Bounds, xform, Colors.Red);

                // 2nd API

                xform *= Matrix3x2.CreateTranslation(0, 150);

                dst.AsSpanBitmap().SetPixels(xform, cat00.AsSpanBitmap(), useBilinear, r);
                DrawBounds(dst, cat00.Bounds, xform, Colors.Red);
            }

            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(3), cat00.AsSpanBitmap(), useBilinear);
            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(-.6f) * Matrix3x2.CreateTranslation(40, 200), cat00.AsSpanBitmap(), useBilinear);
            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(.3f) * Matrix3x2.CreateRotation(1) * Matrix3x2.CreateTranslation(150, 300), qrcode.AsSpanBitmap(), useBilinear);

            dst.Save(new AttachmentInfo("transformed.png"));
        }
Exemplo n.º 3
0
        public void TestFlipPerformance(int w, int h, bool multiThread, bool hflip, bool vflip)
        {
            var bmp = new MemoryBitmap <Pixel.RGB24>(w, h).AsSpanBitmap();

            var mirrorEffect = new Processing.BitmapMirror(hflip, vflip, multiThread);

            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"{w}x{h} HFlip:{hflip} VFlip:{vflip} {Math.Round(t.TotalMilliseconds / 1000)}ms")))
            {
                for (int r = 0; r < 1000; ++r)
                {
                    bmp.ApplyEffect(mirrorEffect);
                }
            }
        }
Exemplo n.º 4
0
        public void LoadJpegPerformanceTest(string filePath)
        {
            TestContext.WriteLine(filePath);
            TestContext.WriteLine("");

            void _writeToTest(string hdr, TimeSpan t)
            {
                TestContext.WriteLine($"{hdr} {t.TotalMilliseconds}ms");
            }

            void _doNothing(string hdr, TimeSpan t)
            {
            }

            Action <string, TimeSpan> _action1 = _writeToTest;
            Action <string, TimeSpan> _action2 = _writeToTest;

            for (int i = 0; i < 3; ++i)
            {
                _action1 = _writeToTest;
                if (i < 2)
                {
                    _action1 = _doNothing;
                }

                _action2 = _doNothing;
                if (i == 0)
                {
                    _action2 = _writeToTest;
                }

                using (var perf = PerformanceBenchmark.Run(result => _action1("OpenCV", result)))
                {
                    var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), OpenCvCodec.Default);
                    _action2($"OpenCV  OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero);
                }

                using (var perf = PerformanceBenchmark.Run(result => _action1("Skia", result)))
                {
                    var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), SkiaCodec.Default);
                    _action2($"Skia  OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero);
                }

                using (var perf = PerformanceBenchmark.Run(result => _action1("WPF", result)))
                {
                    var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), WPFCodec.Default);
                    _action2($"WPF  OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero);
                }

                using (var perf = PerformanceBenchmark.Run(result => _action1("ImageSharp", result)))
                {
                    var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), ImageSharpCodec.Default);
                    _action2($"ImageSharp  OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero);
                }

                using (var perf = PerformanceBenchmark.Run(result => _action1("GDI", result)))
                {
                    var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), GDICodec.Default);
                    _action2($"GDI  OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero);
                }

                using (var perf = PerformanceBenchmark.Run(result => _action1("STB", result)))
                {
                    var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), STBCodec.Default);
                    _action2($"STB  OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero);
                }

                TestContext.WriteLine("");
            }
        }