-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace Aardvark.OpenCV.Benchmarks | ||
|
||
open Aardvark.Base | ||
open Aardvark.OpenCV | ||
open BenchmarkDotNet.Attributes | ||
|
||
module ``Image Processing Benchmarks`` = | ||
|
||
type Scaling() = | ||
|
||
[<DefaultValue; Params(128, 1024, 2048, 4096)>] | ||
val mutable Size : int | ||
|
||
[<DefaultValue; Params(ImageInterpolation.Linear, ImageInterpolation.Cubic, ImageInterpolation.Lanczos)>] | ||
val mutable Interpolation : ImageInterpolation | ||
|
||
let mutable image = null | ||
|
||
let scaleFactor = V2d(0.234, 0.894) | ||
|
||
[<GlobalSetup>] | ||
member x.Setup() = | ||
Aardvark.Init() | ||
let rnd = RandomSystem 0 | ||
image <- new PixImage<float32>(Col.Format.RGBA, x.Size, x.Size) | ||
image.Volume.SetByIndex(ignore >> rnd.UniformFloat) |> ignore | ||
|
||
[<Benchmark(Description = "Aardvark (Tensors)", Baseline = true)>] | ||
member x.AardvarkTensors() = | ||
let volume = image.Volume.Scaled(scaleFactor, x.Interpolation) | ||
PixImage<float32>(image.Format, volume) | ||
|
||
[<Benchmark>] | ||
member x.OpenCV() = | ||
TensorExtensions.ScaledOpenCV(image, scaleFactor, x.Interpolation) | ||
Check failure on line 35 in src/Aardvark.OpenCV.Tests/Benchmarks/ImageBench.fs GitHub Actions / build
Check failure on line 35 in src/Aardvark.OpenCV.Tests/Benchmarks/ImageBench.fs GitHub Actions / build
Check failure on line 35 in src/Aardvark.OpenCV.Tests/Benchmarks/ImageBench.fs GitHub Actions / build
Check failure on line 35 in src/Aardvark.OpenCV.Tests/Benchmarks/ImageBench.fs GitHub Actions / build
Check failure on line 35 in src/Aardvark.OpenCV.Tests/Benchmarks/ImageBench.fs GitHub Actions / build
Check failure on line 35 in src/Aardvark.OpenCV.Tests/Benchmarks/ImageBench.fs GitHub Actions / build
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Aardvark.OpenCV.Benchmarks | ||
|
||
open System.Reflection | ||
open BenchmarkDotNet.Running; | ||
open BenchmarkDotNet.Configs | ||
open BenchmarkDotNet.Jobs | ||
open BenchmarkDotNet.Toolchains | ||
|
||
module Program = | ||
|
||
[<EntryPoint>] | ||
let main argv = | ||
|
||
let cfg = | ||
let job = Job.ShortRun.WithToolchain(InProcess.Emit.InProcessEmitToolchain.Instance) | ||
ManualConfig.Create(DefaultConfig.Instance).WithOptions(ConfigOptions.DisableOptimizationsValidator).AddJob(job) | ||
|
||
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(argv, cfg) |> ignore | ||
|
||
0 |
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Aardvark.OpenCV.Tests | ||
|
||
open Aardvark.Base | ||
open Aardvark.OpenCV | ||
open NUnit.Framework | ||
|
||
module ``Clustering Tests`` = | ||
|
||
let private vec value = | ||
Vector<float32> [| value; value; value |] | ||
|
||
[<SetUp>] | ||
let init() = | ||
Aardvark.Init() | ||
|
||
[<Test>] | ||
let ``K-means``() = | ||
let data = [| vec -1.2f; vec -1.3f; vec -1.4f; vec 2.3f; vec 2.4f |] | ||
|
||
let mutable clusters = Array.empty | ||
let mutable centers = Array.empty | ||
OpenCVKMeansClustering.ClusterKMeans(data, 2, 1, false, &clusters, ¢ers); | ||
|
||
Assert.AreEqual([| 0; 0; 0; 1; 1 |], clusters); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Aardvark.OpenCV.Tests | ||
|
||
open Aardvark.Base | ||
open Aardvark.OpenCV | ||
open NUnit.Framework | ||
|
||
module ``Fitting Tests`` = | ||
|
||
[<SetUp>] | ||
let init() = | ||
Aardvark.Init() | ||
|
||
[<Test>] | ||
let ``Plane3d least squares``() = | ||
let data = [| V3d(1.0, 2.0, -1.0); V3d(5.0, 4.0, -1.0); V3d(23.0, 100.0, -1.0) |] | ||
let plane = data.FitPlane3dLeastSquares(); | ||
Assert.AreEqual(Plane3d(V3d.ZAxis, -1), plane); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Aardvark.OpenCV.Tests | ||
|
||
open Aardvark.Base | ||
open Aardvark.OpenCV | ||
open NUnit.Framework | ||
|
||
module ``Image Processing Tests`` = | ||
|
||
[<SetUp>] | ||
let init() = | ||
Aardvark.Init() | ||
|
||
[<Test>] | ||
let ``Scaling`` () = | ||
let rnd = RandomSystem() | ||
|
||
let pi = PixImage<uint8>(Col.Format.RGBA, 512, 567) | ||
pi.Volume.SetByIndex(ignore >> rnd.UniformInt >> uint8) |> ignore | ||
|
||
let scaleFactor = V2d(0.134, 0.234) | ||
let result = pi.ScaledOpenCV(scaleFactor, ImageInterpolation.Cubic) | ||
let expected = pi.Scaled(scaleFactor, ImageInterpolation.Cubic) | ||
let psnr = PixImage.peakSignalToNoiseRatio result expected | ||
|
||
Assert.Greater(psnr, 20.0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
group Test | ||
|
||
FSharp.Core | ||
|
||
Aardvark.PixImage.ImageSharp | ||
|
||
NUnit | ||
NUnit3TestAdapter | ||
Microsoft.NET.Test.Sdk | ||
Microsoft.NET.Test.Sdk | ||
|
||
BenchmarkDotNet |
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 ImageProcessing | ||
{ | ||
[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($"Channel type {type} is not supported."); | ||
} | ||
|
||
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)); | ||
} | ||
} |