Пример #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"));
        }
Пример #2
0
        public void FillRuleTest()
        {
            int scale = 1;

            var bmp = new MemoryBitmap <Pixel.BGR24>(16 * scale, 8 * scale);

            for (int y = 0; y < bmp.Height; ++y)
            {
                for (int x = 0; x < bmp.Width; ++x)
                {
                    var z = ((x / scale) & 1) ^ ((y / scale) & 1);
                    if (z == 1)
                    {
                        bmp.SetPixel(x, y, Pixel.GetColor <Pixel.BGR24>(System.Drawing.Color.DarkGray));
                    }
                }
            }

            var dc = bmp.CreateDrawingContext();

            foreach (var tri in _Triangle.GetFillRuleTriangles())
            {
                dc.DrawPolygon(System.Drawing.Color.Red, tri.A * scale, tri.B * scale, tri.C * scale);
            }

            bmp.Save(new AttachmentInfo("result.png"));
        }
Пример #3
0
        public void DrawBitmapFont()
        {
            TestContext.CurrentContext.AttachFolderBrowserShortcut();

            // test glyph splitter

            var spriteFont = Graphics.Bitmaps.Fonts.XnaSpriteFont.Load("Resources\\SegoeUiMono16.png");

            Assert.AreEqual(224, spriteFont.Glyphs.Count);

            // for(int i=0; i < glyphs.Length; ++i) { glyphs[i].Save(new AttachmentInfo($"glyph {i}.png")); }

            // create font and raw some text:

            var font = MemoryBitmap <Pixel.BGRA32> .Load("Resources\\SegoeUiMono16.png").ToBitmapFont();

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

            var xform = Matrix3x2.CreateScale(2) * Matrix3x2.CreateRotation(0.2f) * Matrix3x2.CreateTranslation(5, 5);

            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", -1, (font, System.Drawing.Color.White));

            xform *= Matrix3x2.CreateTranslation(0, 40);
            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", 20, (font, System.Drawing.Color.White));

            xform *= Matrix3x2.CreateTranslation(0, 40);
            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", 20, (Fonts.HersheyFont.Simplex, System.Drawing.Color.White));

            xform *= Matrix3x2.CreateTranslation(0, 40);
            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", 20, System.Drawing.Color.Red);

            dst.Save(new AttachmentInfo("text.png"));
        }
Пример #4
0
        // [TestCase("Resources\\yukikaze.jpg")]
        public void TestAnime2Sketch(string imagePath)
        {
            var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default);
            MemoryBitmap <Pixel.Luminance8> dstImage = default;

            using (var filter = new Anime2SketchFilter())
            {
                filter.Filter(srcImage, ref dstImage);
            }

            dstImage.Save(new AttachmentInfo(imagePath));
        }
Пример #5
0
        private static void _DrawTransformedBitmapWithMulAdd <TPixel>(float mul, float add)
            where TPixel : unmanaged
        {
            var src = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From("shannon.jpg"));

            var dst = new MemoryBitmap <TPixel>(256, 256);

            var xform = System.Numerics.Matrix3x2.CreateScale(dst.Width / (float)src.Width, dst.Height / (float)src.Height);

            dst.AsSpanBitmap().SetPixels <Pixel.BGR24>(xform, src, true, (mul, add));

            dst.Save(AttachmentInfo.From($"result-{typeof(TPixel).Name}.jpg"));
        }
Пример #6
0
        public void TestRealESRGAN(string imagePath)
        {
            // https://onnxruntime.ai/docs/tutorials/resnet50_csharp.html

            var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default);

            using var modelFactory = new MultiresModels(OnnxModel.FromFile);
            modelFactory.Register(16, 16, "Models\\realesrgan_16x16.onnx");
            modelFactory.Register(32, 32, "Models\\realesrgan_32x32.onnx");
            modelFactory.Register(64, 64, "Models\\realesrgan_64x64.onnx");
            modelFactory.Register(128, 128, "Models\\realesrgan_128x128.onnx");
            modelFactory.Register(256, 256, "Models\\realesrgan_256x256.onnx");
            modelFactory.Register(320, 240, "Models\\realesrgan_240x320.onnx");
            modelFactory.Register(320, 320, "Models\\realesrgan_320x320.onnx");
            modelFactory.Register(640, 480, "Models\\realesrgan_480x640.onnx");

            var imagePreprocessor = new ImageProcessor <Pixel.BGR96F>();

            var model        = modelFactory.UseModel(srcImage.Width, srcImage.Height);
            var modelOptions = (model as IServiceProvider).GetService(typeof(Microsoft.ML.OnnxRuntime.SessionOptions)) as Microsoft.ML.OnnxRuntime.SessionOptions;

            modelOptions.GraphOptimizationLevel = Microsoft.ML.OnnxRuntime.GraphOptimizationLevel.ORT_DISABLE_ALL;

            using (var session = model.CreateSession())
            {
                var workingTensor = session.GetInputTensor <float>(0)
                                    .AsSpanTensor4()
                                    .GetSubTensor(0)
                                    .SetImage(srcImage, imagePreprocessor);

                // run

                session.Inference();

                // get results

                var result = session.GetOutputTensor <float>(0)
                             .AsSpanTensor4()
                             .GetSubTensor(0);

                MemoryBitmap <Pixel.BGR24> resultBitmap = default;

                result
                .AsTensorBitmap(Tensors.Imaging.ColorEncoding.BGR)
                .CopyTo(ref resultBitmap);

                resultBitmap.Save(new AttachmentInfo(imagePath));
            }
        }
Пример #7
0
        public void DrawLinesTest()
        {
            var bmp = new MemoryBitmap <Pixel.BGR24>(512, 512);

            var dc = bmp.CreateDrawingContext();

            dc.DrawLine((2, 2), (30, 10), 1, System.Drawing.Color.Red);
            dc.DrawLine((2, 2), (30, 50), 1, System.Drawing.Color.Blue);

            dc.DrawLine((40, 60), (80, 160), 9, LineStyle.Yellow.With(LineCapStyle.Round).WithOutline(System.Drawing.Color.Green, 1));

            dc.DrawLine((180, 260), (800, 300), 9, LineStyle.Yellow.With(LineCapStyle.Round).WithOutline(System.Drawing.Color.Green, 1));

            bmp.Save(new AttachmentInfo("result.png"));
        }
Пример #8
0
        public void DrawingTest()
        {
            var bmp = new MemoryBitmap <Pixel.BGR24>(512, 512);

            var cat   = MemoryBitmap.Load("Resources\\cat.png", Codecs.GDICodec.Default);
            var asset = new ImageSource(cat, (0, 0), (32, 35), (15, 15));

            var dc = bmp.CreateDrawingContext();

            dc.DrawConsoleFont((10, 10), "Hello World 0123456789-+/*", System.Drawing.Color.White);
            dc.DrawConsoleFont((10, 40), "abcdefghijklmnopqrstuvwxyz", System.Drawing.Color.White);
            dc.DrawConsoleFont((10, 70), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", System.Drawing.Color.White);

            dc.DrawTextLine((10, 200), "Abc123", 15, FontStyle.White);

            dc.DrawTextLine(Matrix3x2.CreateRotation(1, new Vector2(10, 350)), "Abc123", 15, FontStyle.White.With(3));

            dc.DrawEllipse((200, 200), 50, 50, (System.Drawing.Color.Red, System.Drawing.Color.Blue, 3));

            dc.DrawImage(Matrix3x2.CreateScale(3) * Matrix3x2.CreateRotation(1) * Matrix3x2.CreateTranslation(70, 150), asset);

            bmp.Save(new AttachmentInfo("result.png"));
        }
Пример #9
0
 public void SaveBitmapToFile(string path, System.Drawing.Imaging.ImageFormat format)
 {
     MemoryBitmap.Save(path, format);
 }