-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
429 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Reflection; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Configs; | ||
|
||
namespace Aardvark.OpenCV.Tests | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var job = Job.ShortRun.WithToolchain(BenchmarkDotNet.Toolchains.InProcess.Emit.InProcessEmitToolchain.Instance); | ||
var cfg = ManualConfig.Create(DefaultConfig.Instance).WithOptions(ConfigOptions.DisableOptimizationsValidator).AddJob(job); | ||
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args, cfg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Aardvark.Base; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Aardvark.OpenCV.Tests | ||
{ | ||
public class Scaling | ||
{ | ||
[Params(128, 1024, 2048, 4096)] | ||
public int Size; | ||
|
||
[Params(ImageInterpolation.Linear, ImageInterpolation.Cubic)] | ||
public ImageInterpolation Interpolation; | ||
|
||
private PixImage<float> _image; | ||
|
||
private readonly V2d _scaleFactor = new (0.234, 0.894); | ||
|
||
[GlobalSetup] | ||
public void Init() | ||
{ | ||
Aardvark.Base.Aardvark.Init(); | ||
var rnd = new RandomSystem(0); | ||
_image = new PixImage<float>(Col.Format.RGBA, new V2i(Size, Size)); | ||
_image.Volume.SetByIndex((_) => rnd.UniformFloat()); | ||
} | ||
|
||
[Benchmark(Description = "Aardvark (Tensors)", Baseline = true)] | ||
public PixImage<float> AardvarkTensors() | ||
{ | ||
var volume = Aardvark.Base.TensorExtensions.Scaled(_image.Volume, _scaleFactor, Interpolation); | ||
return new PixImage<float>(_image.Format, volume); | ||
} | ||
|
||
[Benchmark] | ||
public PixImage<float> OpenCV() | ||
=> TensorExtensions.ScaledOpenCV(_image, _scaleFactor, Interpolation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Aardvark.Base; | ||
using Aardvark.OpenCV; | ||
using NUnit.Framework; | ||
|
||
namespace Aardvark.OpenCV.Tests | ||
{ | ||
[TestFixture] | ||
internal class TensorExtensionsTests | ||
{ | ||
[OneTimeSetUp] | ||
public void Init() | ||
{ | ||
Aardvark.Base.Aardvark.Init(); | ||
} | ||
|
||
[Test] | ||
public void Scaled() | ||
{ | ||
var pi = PixImage.Load(@"D:\Users\Martin\Pictures\bg\2kYW3Tz.jpg").AsPixImage<byte>(); | ||
pi = pi.ScaledOpenCV(V2d.Half, ImageInterpolation.Linear); | ||
pi.Save(@"D:\Users\Martin\Desktop\bla.png"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
group Test | ||
|
||
Aardvark.PixImage.ImageSharp | ||
|
||
NUnit | ||
NUnit3TestAdapter | ||
Microsoft.NET.Test.Sdk | ||
Microsoft.NET.Test.Sdk | ||
|
||
BenchmarkDotNet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Aardvark.Base; | ||
using OpenCvSharp; | ||
using CvMat = OpenCvSharp.Mat; | ||
|
||
namespace Aardvark.OpenCV | ||
{ | ||
public static class TensorExtensions | ||
{ | ||
[OnAardvarkInit] | ||
public static void Init() | ||
{ | ||
} | ||
|
||
private static readonly Dictionary<Type, Func<int, MatType>> matTypes = new() | ||
{ | ||
{ typeof(byte), MatType.CV_8UC }, | ||
{ typeof(sbyte), MatType.CV_8SC }, | ||
{ typeof(short), MatType.CV_16UC }, | ||
{ typeof(ushort), MatType.CV_16SC }, | ||
{ typeof(int), MatType.CV_32SC }, | ||
{ typeof(float), MatType.CV_32FC }, | ||
{ typeof(double), MatType.CV_64FC }, | ||
}; | ||
|
||
private static MatType ToMatType(this Type type, int channels) | ||
{ | ||
if (matTypes.TryGetValue(type, out var toMatType)) return toMatType(channels); | ||
else throw new NotSupportedException(); | ||
} | ||
|
||
private static readonly Dictionary<ImageInterpolation, InterpolationFlags> interpolationFlags = new() | ||
{ | ||
{ ImageInterpolation.Near, InterpolationFlags.Nearest }, | ||
{ ImageInterpolation.Linear, InterpolationFlags.Linear }, | ||
{ ImageInterpolation.Cubic, InterpolationFlags.Cubic }, | ||
{ ImageInterpolation.Lanczos, InterpolationFlags.Lanczos4 }, | ||
}; | ||
|
||
private static InterpolationFlags ToInterpolationFlags(this ImageInterpolation interpolation) | ||
{ | ||
if (interpolationFlags.TryGetValue(interpolation, out InterpolationFlags flags)) return flags; | ||
else throw new NotSupportedException($"Interpolation {interpolation} is not supported."); | ||
} | ||
|
||
public static Volume<T> ScaledOpenCV<T>(this Volume<T> src, V2d scaleFactor, ImageInterpolation interpolation) | ||
{ | ||
if (!src.HasImageLayout()) | ||
{ | ||
throw new ArgumentException($"Volume must be in image layout (Origin = {src.Origin}, First = {src.First}, Delta = {src.Delta})."); | ||
} | ||
|
||
var dstSize = new V3l((V2l)(V2d.Half + scaleFactor * (V2d)src.Size.XY), src.Size.Z); | ||
var dst = dstSize.CreateImageVolume<T>(); | ||
|
||
var matType = typeof(T).ToMatType((int)src.SZ); | ||
|
||
var mSrc = CvMat.FromPixelData((int)src.SY, (int)src.SX, matType, src.Array); | ||
var mDst = CvMat.FromPixelData((int)dst.SY, (int)dst.SX, matType, dst.Array); | ||
Cv2.Resize(mSrc, mDst, new Size((int)dst.SX, (int)dst.SY), interpolation: interpolation.ToInterpolationFlags()); | ||
|
||
return dst; | ||
} | ||
|
||
public static PixImage<T> ScaledOpenCV<T>(this PixImage<T> src, V2d scaleFactor, ImageInterpolation interpolation) | ||
=> new (src.Format, src.Volume.ScaledOpenCV(scaleFactor, interpolation)); | ||
} | ||
} |