Skip to content

Commit

Permalink
Implement scaling (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Sep 9, 2024
1 parent b0230f2 commit dfba7c7
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 14 deletions.
10 changes: 7 additions & 3 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ group Test
storage: none
source https://api.nuget.org/v3/index.json

nuget NUnit ~> 3.14.0
nuget NUnit3TestAdapter ~> 4.5.0
nuget Microsoft.NET.Test.Sdk ~> 17.9.0
nuget Aardvark.PixImage.ImageSharp ~> 2.0.1

nuget NUnit ~> 3.14.0
nuget NUnit3TestAdapter ~> 4.5.0
nuget Microsoft.NET.Test.Sdk ~> 17.9.0

nuget BenchmarkDotNet ~> 0.14.0
277 changes: 267 additions & 10 deletions paket.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Aardvark.OpenCV.Tests/Aardvark.OpenCV.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\bin\Debug</OutputPath>
Expand Down
17 changes: 17 additions & 0 deletions src/Aardvark.OpenCV.Tests/Program.cs
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);
}
}
}
38 changes: 38 additions & 0 deletions src/Aardvark.OpenCV.Tests/TensorExtensionsBench.cs
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);
}
}
24 changes: 24 additions & 0 deletions src/Aardvark.OpenCV.Tests/TensorExtensionsTests.cs
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");
}
}
}
6 changes: 5 additions & 1 deletion src/Aardvark.OpenCV.Tests/paket.references
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
1 change: 1 addition & 0 deletions src/Aardvark.OpenCV/Aardvark.OpenCV.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;1705;1591</NoWarn>
</PropertyGroup>
Expand Down
69 changes: 69 additions & 0 deletions src/Aardvark.OpenCV/TensorExtensions.cs
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));
}
}

0 comments on commit dfba7c7

Please sign in to comment.