Example #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"));
        }
Example #2
0
        public async Task CreateBitmap()
        {
            using var bmp = new BitmapInfo(256, 256, Pixel.RGBA32.Format)
                            .ToAndroidFactory()
                            .CreateCompatibleBitmap();

            MemoryBitmap <Pixel.RGBA32> dst = default;

            bmp.CopyTo(ref dst);

            dst.AsSpanBitmap().AsTypeless().SetPixels(System.Drawing.Color.Red);
            dst.AsSpanBitmap().AsTypeless().Slice(new BitmapBounds(5, 5, 255 - 10, 255 - 10)).SetPixels(new Random());
            dst.AsSpanBitmap().AsTypeless().CopyTo(bmp);

            TestContext.WriteLine($"{dst.Width} {dst.Height}");

            await TestContext.CurrentContext.AttachToCurrentTest("test1.png", bmp);
        }
        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"));
        }
        public static void Inference <TResult>(this IInferenceContext <PointerBitmap, TResult> context, TResult result, SpanBitmap image, RECT?imageRect = null)
            where TResult : class
        {
            if (image.PixelFormat == Pixel.BGR24.Format)
            {
                context.Inference(result, image.OfType <Pixel.BGR24>(), imageRect);
                return;
            }

            // convert image to BGR24
            MemoryBitmap tmp = default;

            image.CopyTo(ref tmp, Pixel.BGR24.Format);

            tmp.AsSpanBitmap().PinReadablePointer
            (
                ptrBmp => context.Inference(result, ptrBmp, imageRect)
            );
        }
        private bool _TryGetImageSource(ImageSource src, out SpanBitmap bitmap)
        {
            if (src.Source is SpanBitmap.ISource typeless)
            {
                bitmap = typeless.AsSpanBitmap().AsReadOnly(); if (bitmap.IsEmpty)
                {
                    return(false);
                }
                src.WithSourceSize(bitmap.Width, bitmap.Height);
                return(true);
            }

            if (src.Source is MemoryBitmap.IDisposableSource isrc)
            {
                bitmap = isrc.AsMemoryBitmap().AsSpanBitmap().AsReadOnly(); if (bitmap.IsEmpty)
                {
                    return(false);
                }
                src.WithSourceSize(bitmap.Width, bitmap.Height);
                return(true);
            }

            if (src.Source is InterlockedBitmap ilock)
            {
                ilock.DequeueLastOrDefault(innerBmp => innerBmp.AsSpanBitmap().CopyTo(ref _TempBitmap));

                if (_TempBitmap.IsEmpty)
                {
                    bitmap = default; return(false);
                }

                bitmap = _TempBitmap.AsSpanBitmap().AsReadOnly();
                src.WithSourceSize(bitmap.Width, bitmap.Height);
                return(true);
            }

            bitmap = default;
            return(false);
        }
 protected override SpanBitmap <TPixel> GetRenderTarget() => _Target.AsSpanBitmap();
Example #7
0
 public static Adapters.OpenCvSharp4SpanAdapter WithOpenCv <TPixel>(this MemoryBitmap <TPixel> bmp)
     where TPixel : unmanaged
 {
     return(new Adapters.OpenCvSharp4SpanAdapter(bmp.AsSpanBitmap()));
 }