|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Aardvark.Base; |
| 4 | +using OpenCvSharp; |
| 5 | +using CvMat = OpenCvSharp.Mat; |
| 6 | + |
| 7 | +namespace Aardvark.OpenCV |
| 8 | +{ |
| 9 | + public static class TensorExtensions |
| 10 | + { |
| 11 | + [OnAardvarkInit] |
| 12 | + public static void Init() |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + private static readonly Dictionary<Type, Func<int, MatType>> matTypes = new() |
| 17 | + { |
| 18 | + { typeof(byte), MatType.CV_8UC }, |
| 19 | + { typeof(sbyte), MatType.CV_8SC }, |
| 20 | + { typeof(short), MatType.CV_16UC }, |
| 21 | + { typeof(ushort), MatType.CV_16SC }, |
| 22 | + { typeof(int), MatType.CV_32SC }, |
| 23 | + { typeof(float), MatType.CV_32FC }, |
| 24 | + { typeof(double), MatType.CV_64FC }, |
| 25 | + }; |
| 26 | + |
| 27 | + private static MatType ToMatType(this Type type, int channels) |
| 28 | + { |
| 29 | + if (matTypes.TryGetValue(type, out var toMatType)) return toMatType(channels); |
| 30 | + else throw new NotSupportedException(); |
| 31 | + } |
| 32 | + |
| 33 | + private static readonly Dictionary<ImageInterpolation, InterpolationFlags> interpolationFlags = new() |
| 34 | + { |
| 35 | + { ImageInterpolation.Near, InterpolationFlags.Nearest }, |
| 36 | + { ImageInterpolation.Linear, InterpolationFlags.Linear }, |
| 37 | + { ImageInterpolation.Cubic, InterpolationFlags.Cubic }, |
| 38 | + { ImageInterpolation.Lanczos, InterpolationFlags.Lanczos4 }, |
| 39 | + }; |
| 40 | + |
| 41 | + private static InterpolationFlags ToInterpolationFlags(this ImageInterpolation interpolation) |
| 42 | + { |
| 43 | + if (interpolationFlags.TryGetValue(interpolation, out InterpolationFlags flags)) return flags; |
| 44 | + else throw new NotSupportedException($"Interpolation {interpolation} is not supported."); |
| 45 | + } |
| 46 | + |
| 47 | + public static Volume<T> ScaledOpenCV<T>(this Volume<T> src, V2d scaleFactor, ImageInterpolation interpolation) |
| 48 | + { |
| 49 | + if (!src.HasImageLayout()) |
| 50 | + { |
| 51 | + throw new ArgumentException($"Volume must be in image layout (Origin = {src.Origin}, First = {src.First}, Delta = {src.Delta})."); |
| 52 | + } |
| 53 | + |
| 54 | + var dstSize = new V3l((V2l)(V2d.Half + scaleFactor * (V2d)src.Size.XY), src.Size.Z); |
| 55 | + var dst = dstSize.CreateImageVolume<T>(); |
| 56 | + |
| 57 | + var matType = typeof(T).ToMatType((int)src.SZ); |
| 58 | + |
| 59 | + var mSrc = CvMat.FromPixelData((int)src.SY, (int)src.SX, matType, src.Array); |
| 60 | + var mDst = CvMat.FromPixelData((int)dst.SY, (int)dst.SX, matType, dst.Array); |
| 61 | + Cv2.Resize(mSrc, mDst, new Size((int)dst.SX, (int)dst.SY), interpolation: interpolation.ToInterpolationFlags()); |
| 62 | + |
| 63 | + return dst; |
| 64 | + } |
| 65 | + |
| 66 | + public static PixImage<T> ScaledOpenCV<T>(this PixImage<T> src, V2d scaleFactor, ImageInterpolation interpolation) |
| 67 | + => new (src.Format, src.Volume.ScaledOpenCV(scaleFactor, interpolation)); |
| 68 | + } |
| 69 | +} |
0 commit comments