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")); }
public void ZXingFindQRCode(string filePath, string expected) { // http://www.cvsandbox.com/ filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, filePath); var image = MemoryBitmap.Load(filePath, STBCodec.Default); // detect code: var result = new ZXingCode(); using var detector = new ZXingCode.Detector(); detector.Inference(result, image, new System.Drawing.Rectangle(20, 20, 1000, 1000)); var code = result.Results[0]; if (string.IsNullOrWhiteSpace(expected)) { Assert.Null(code); return; } Assert.AreEqual(expected, code.Text); // report result: TestContext.WriteLine($"Code found: {code?.Text}"); image .CreateDrawingContext() .DrawAsset(System.Numerics.Matrix3x2.Identity, result); image.Save(new AttachmentInfo("result.png")); }
public void SaveMjpeg() { var f1 = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.jpg"); var f2 = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon-blurred.jpg"); var ff1 = MemoryBitmap.Load(f1, GDICodec.Default); var ff2 = MemoryBitmap.Load(f2, GDICodec.Default); IEnumerable <MemoryBitmap> _getFrames() { for (int i = 0; i < 10; ++i) { yield return(ff1); yield return(ff1); yield return(ff1); yield return(ff1); yield return(ff2); yield return(ff2); yield return(ff2); yield return(ff2); } } AttachmentInfo .From("video.avi") .WriteAVI(_getFrames()); }
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")); }
public void LoadImage(string filePath) { TestContext.CurrentContext.AttachFolderBrowserShortcut(); var codecs = new IBitmapDecoder[] { OpenCvCodec.Default, GDICodec.Default, WPFCodec.Default, ImageSharpCodec.Default, STBCodec.Default, SkiaCodec.Default }; foreach (var decoder in codecs.OfType <IBitmapDecoder>()) { var sw = System.Diagnostics.Stopwatch.StartNew(); var bitmap = MemoryBitmap.Load(ResourceInfo.From(filePath), decoder); sw.Stop(); TestContext.WriteLine($"Loading {System.IO.Path.GetFileName(filePath)} with {decoder} tool {sw.ElapsedMilliseconds}"); foreach (var encoder in codecs.OfType <IBitmapEncoder>()) { // System.Diagnostics.Debug.Assert(!(decoder is WPFCodec && encoder is SkiaCodec)); var fname = $"{decoder.GetType().Name}-To-{encoder.GetType().Name}.png"; bitmap.Save(new AttachmentInfo(fname), encoder); fname = $"{decoder.GetType().Name}-To-{encoder.GetType().Name}.jpg"; bitmap.Save(new AttachmentInfo(fname), encoder); fname = $"{decoder.GetType().Name}-To-{encoder.GetType().Name}-Gray.jpg"; bitmap .AsSpanBitmap() .ToMemoryBitmap(Pixel.Luminance8.Format) .Save(AttachmentInfo.From(fname), encoder); } } }
public void LoadWithMultiCodec(string filePath) { var img = MemoryBitmap.Load(ResourceInfo.From(filePath), Codecs.STBCodec.Default, Codecs.OpenCvCodec.Default, Codecs.ImageSharpCodec.Default, Codecs.GDICodec.Default, Codecs.SkiaCodec.Default); Assert.NotNull(img); Assert.AreEqual(512, img.Width); Assert.AreEqual(512, img.Height); }
public void LoadImage(string filePath) { filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, filePath); var bitmap = MemoryBitmap.Load(filePath, Codecs.ImageSharpCodec.Default); bitmap.Save(new AttachmentInfo("Result.png")); }
public void LoadImageWithDefaultCodec(string filePath) { var sw = System.Diagnostics.Stopwatch.StartNew(); var bitmap = MemoryBitmap.Load(ResourceInfo.From(filePath)); Assert.IsFalse(bitmap.IsEmpty); TestContext.WriteLine(bitmap.Info); sw.Stop(); }
public void LoadWebpToImageSharp() { // load a bitmap with SkiaSharp, which is known to support WEBP. var mem = MemoryBitmap.Load(ResourceInfo.From("shannon.webp"), Codecs.SkiaCodec.Default); using (var proxy = mem.UsingImageSharp()) { mem.Save(AttachmentInfo.From("ImageSharp.png")); } }
public void Laplacian1() { var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.webp"); // Use SkiaSharp to load a WEBP image: var bmp = MemoryBitmap <Pixel.BGR24> .Load(filePath, Codecs.SkiaCodec.Default); bmp.AsSpanBitmap().Apply <Pixel.BGR24, Pixel.RGBA128F>(Laplacian); bmp.Save(new AttachmentInfo("laplacian.jpg")); }
public void TestResnet50(string imagePath, string expectedResult) { // https://onnxruntime.ai/docs/tutorials/resnet50_csharp.html var srcImage = MemoryBitmap.Load(imagePath); var model = OnnxModel.FromFile("Models\\resnet50-v2-7.onnx"); // image normalization // https://github.com/onnx/models/tree/master/vision/classification/resnet#preprocessing var modelXform = MultiplyAdd .CreateMul(0.229f, 0.224f, 0.225f) .ConcatAdd(0.485f, 0.456f, 0.406f) // .GetTransposedZYXW() .GetInverse(); var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>(modelXform); 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) .AsSpanTensor2() .GetSubTensor(0); result.ApplySoftMax(); var scores = result.ToArray(); var pairs = Labels.resnet50_v2_7_Labels .Zip(scores, (Label, Score) => (Label, Score)) .OrderByDescending(item => item.Score) .ToList(); foreach (var pair in pairs) { TestContext.WriteLine($"{pair.Label} = {pair.Score}"); } Assert.AreEqual(expectedResult, pairs[0].Label); } }
public void LoadAndSaveInteropFormat() { TestContext.CurrentContext.AttachFolderBrowserShortcut(); var img1 = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From("shannon.jpg")); var tmpPath = AttachmentInfo.From("shannon.interopbmp").WriteObject(f => img1.Save(f)); var img2 = MemoryBitmap <Pixel.BGR24> .Load(tmpPath.FullName); img2.Save(AttachmentInfo.From("shannon.png")); }
public void DrawToPlanesTest() { var src = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From("shannon.jpg")); var dst = new SpanPlanesXYZ <float>(256, 256); var xform = System.Numerics.Matrix3x2.CreateScale(dst.Width / (float)src.Width, dst.Height / (float)src.Height); dst.SetPixels <Pixel.BGR24>(xform, src, true); dst.Save(AttachmentInfo.From("result.png")); }
// [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)); }
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 void LoadWithConversion(string filePath) { var bgr = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From(filePath), STBCodec.Default, OpenCvCodec.Default, ImageSharpCodec.Default, GDICodec.Default, SkiaCodec.Default); Assert.NotNull(bgr); var rgb = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From(filePath), STBCodec.Default, OpenCvCodec.Default, ImageSharpCodec.Default, GDICodec.Default, SkiaCodec.Default); Assert.NotNull(rgb); Assert.AreEqual(512, bgr.Width); Assert.AreEqual(512, bgr.Height); }
public void TestFullStackFaceDetector(string imagePath) { var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default); using (var filter = new FullStackFaceInfo.Detector()) { var result = filter.Process(srcImage).ToArray(); for (int i = 0; i < result.Length; i++) { result[0].AlignedImage.Save(new AttachmentInfo($"detected image {i}.jpg")); } } }
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)); } }
public void TestYuNet(string imagePath) { // https://github.com/opencv/opencv_zoo/tree/master/models/face_detection_yunet // https://github.com/ShiqiYu/libfacedetection // https://github.com/Kazuhito00/YuNet-ONNX-TFLite-Sample/blob/main/yunet/yunet_onnx.py var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default); var pmodel = OnnxModel.FromFile("Models\\face_detection_yunet_2021sep.onnx"); var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>(); using (var session = pmodel.CreateSession()) { var workingTensor = session.UseInputTensor <float>(0, 1, 3, srcImage.Height, srcImage.Width) .VerifyName(n => n == "input") .AsSpanTensor4() .GetSubTensor(0) .SetImage(srcImage, imagePreprocessor); const int COUNT = 15040; var loc = session.UseOutputTensor <float>(0, COUNT, 14).VerifyName(n => n == "loc"); var conf = session.UseOutputTensor <float>(1, COUNT, 2).VerifyName(n => n == "conf"); var iou = session.UseOutputTensor <float>(2, COUNT, 1).VerifyName(n => n == "iou"); // run session.Inference(); // https://github.com/ShiqiYu/libfacedetection/blob/master/src/facedetectcnn.cpp#L731 var r_loc = loc.AsSpanTensor2().UpCast <YuNetEntry>().Span.ToArray(); var r_conf = conf.AsSpanTensor2().UpCast <System.Numerics.Vector2>().Span.ToArray(); var r_iou = iou.AsSpanTensor2().UpCast <float>().Span; // softmax1vector2class(mbox_conf); // clamp1vector(mbox_iou); // SpanTensor.ApplySoftMax(r_conf); var sorted = r_conf .Select(item => item.Y) .Zip(r_loc) .OrderByDescending(item => item.First) .ToArray(); var prior_variance = new [] { 0.1f, 0.1f, 0.2f, 0.2f }; } }
public void TestMcnnFace(string imagePath) { // https://github.com/linxiaohui/mtcnn-opencv/tree/main/mtcnn_cv2 var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default); var pmodel = OnnxModel.FromFile("Models\\MCNN\\pnet.onnx"); var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>(); using (var session = pmodel.CreateSession()) { var workingTensor = session.UseInputTensor <float>(0, 1, srcImage.Height, srcImage.Width, 3) .VerifyName(n => n == "input_1") .AsSpanTensor4() .GetSubTensor(0) .SetImage(srcImage, imagePreprocessor); var conv2d = session.UseOutputTensor <float>(0, 1, 251, 251, 4).VerifyName(n => n == "conv2d_4"); var softmax = session.UseOutputTensor <float>(1, 1, 251, 251, 2).VerifyName(n => n == "softmax"); // run session.Inference(); // get results: var score = softmax .AsSpanTensor4() .GetSubTensor(0) .UpCast <System.Numerics.Vector2>() .Span .ToArray(); var cv2d = conv2d .AsSpanTensor4() .GetSubTensor(0) .UpCast <System.Numerics.Vector4>() .Span .ToArray(); var pairs = score .Select(item => item.Y) .Zip(cv2d.Select(xyxy => xyxy * new System.Numerics.Vector4(srcImage.Height, srcImage.Width, srcImage.Height, srcImage.Width))) .OrderByDescending(item => item.First) .ToArray(); } }
public void TestArcFace(string imagePath) { // https://github.com/onnx/models/tree/master/vision/body_analysis/arcface // https://github.com/openvinotoolkit/open_model_zoo/tree/2021.4.1/models/public/mtcnn var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default); var srcImagePreprocessor = new ImageProcessor <Pixel.RGB96F>(); var model = OnnxModel.FromFile("Models\\arcfaceresnet100-8.onnx"); using (var session = model.CreateSession()) { var workingTensor = session.GetInputTensor <float>(0) .AsSpanTensor4() .GetSubTensor(0) .SetImage(srcImage, srcImagePreprocessor); // run session.Inference(); // get results var result = session .GetOutputTensor <float>(0) .AsSpanTensor2() .GetSubTensor(0) .ToArray(); var resultv4 = System.Runtime.InteropServices.MemoryMarshal.Cast <float, System.Numerics.Vector4>(result); for (int i = 0; i < resultv4.Length; ++i) { TestContext.WriteLine(resultv4[i]); } } }
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")); }
public void ResizeImage() { var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.jpg"); var bitmap = MemoryBitmap .Load(filePath, Codecs.SkiaCodec.Default) .AsSpanBitmap(); bitmap .WithGDI() .ToResizedMemoryBitmap(32, 32) .Save(new AttachmentInfo("GDI_Resized.png")); bitmap .WithOpenCv() .ToResizedMemoryBitmap(32, 32, OpenCvSharp.InterpolationFlags.Lanczos4) .Save(new AttachmentInfo("OpenCV_Resized.png")); bitmap .WithImageSharp() .ToResizedMemoryBitmap(32, 32) .Save(new AttachmentInfo("ImageSharp_Resized.png")); }
public void TryDetectAruco4x4(string filePath) { filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources", "Aruco", filePath); var bitmap = MemoryBitmap.Load(filePath, Codecs.OpenCvCodec.Default); var arucoContext = new Vision.Backends.MarkersContext(); using (var arucoEstimator = new Vision.Backends.MarkersContext.ArucoEstimator()) { arucoEstimator.SetCameraCalibrationDefault(); arucoEstimator.Inference(arucoContext, bitmap); } foreach (var item in arucoContext.Markers) { TestContext.WriteLine($"{item.Id} {item.A} {item.B} {item.C} {item.D}"); } bitmap .CreateDrawingContext() .DrawAsset(System.Numerics.Matrix3x2.Identity, arucoContext); bitmap.Save(new AttachmentInfo("Result.png")); }
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(""); } }