From 8bd5787c4954f46bd1d88c33c597f0aaa5f8b6e7 Mon Sep 17 00:00:00 2001 From: shimat Date: Mon, 22 Mar 2021 20:44:16 +0900 Subject: [PATCH] add reshape test --- src/OpenCvSharp/Modules/core/Mat/Mat.cs | 22 +++------------------- test/OpenCvSharp.Tests/core/MatTest.cs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/OpenCvSharp/Modules/core/Mat/Mat.cs b/src/OpenCvSharp/Modules/core/Mat/Mat.cs index cc705329f..28ce0016b 100644 --- a/src/OpenCvSharp/Modules/core/Mat/Mat.cs +++ b/src/OpenCvSharp/Modules/core/Mat/Mat.cs @@ -221,10 +221,8 @@ public Mat(Mat m, Range rowRange, Range? colRange = null) /// Array of selected ranges of m along each dimensionality. public Mat(Mat m, params Range[] ranges) { - if (m == null) + if (m is null) throw new ArgumentNullException(nameof(m)); - if (ranges == null) - throw new ArgumentNullException(nameof(ranges)); if (ranges.Length == 0) throw new ArgumentException("empty ranges", nameof(ranges)); m.ThrowIfDisposed(); @@ -539,9 +537,6 @@ public static MatExpr Zeros(Size size, MatType type) /// public static MatExpr Zeros(MatType type, params int[] sizes) { - if (sizes == null) - throw new ArgumentNullException(nameof(sizes)); - NativeMethods.HandleException( NativeMethods.core_Mat_zeros2(sizes.Length, sizes, type, out var ret)); var retVal = new MatExpr(ret); @@ -582,9 +577,6 @@ public static MatExpr Ones(Size size, MatType type) /// public static MatExpr Ones(MatType type, params int[] sizes) { - if (sizes == null) - throw new ArgumentNullException(nameof(sizes)); - NativeMethods.HandleException( NativeMethods.core_Mat_ones2(sizes.Length, sizes, type, out var ret)); var retVal = new MatExpr(ret); @@ -626,8 +618,6 @@ public static MatExpr Eye(int rows, int cols, MatType type) public static Mat FromArray(params TElem[] arr) where TElem : unmanaged { - if (arr == null) - throw new ArgumentNullException(nameof(arr)); if (arr.Length == 0) throw new ArgumentException("arr.Length == 0"); @@ -1650,8 +1640,6 @@ public Mat Reshape(int cn, int rows = 0) public Mat Reshape(int cn, params int[] newDims) { ThrowIfDisposed(); - if (newDims == null) - throw new ArgumentNullException(nameof(newDims)); NativeMethods.HandleException( NativeMethods.core_Mat_reshape2(ptr, cn, newDims.Length, newDims, out var ret)); @@ -1788,8 +1776,6 @@ public void Create(Size size, MatType type) /// New matrix type. public void Create(MatType type, params int[] sizes) { - if (sizes == null) - throw new ArgumentNullException(nameof(sizes)); if (sizes.Length < 2) throw new ArgumentException("sizes.Length < 2"); NativeMethods.HandleException( @@ -2461,8 +2447,6 @@ public Mat SubMat(Rect roi) /// public Mat SubMat(params Range[] ranges) { - if (ranges == null) - throw new ArgumentNullException(nameof(ranges)); ThrowIfDisposed(); NativeMethods.HandleException( @@ -3355,7 +3339,7 @@ public void Set(int[] idx, T value) where T : struct #region Get/SetArray - private static readonly Dictionary dataDimensionMap = new Dictionary + private static readonly IReadOnlyDictionary dataDimensionMap = new Dictionary { {typeof(byte), 1}, {typeof(sbyte), 1}, @@ -3403,7 +3387,7 @@ public void Set(int[] idx, T value) where T : struct {typeof(Vec6d), 6}, }; - private static readonly Dictionary acceptableTypesMap = new Dictionary + private static readonly IReadOnlyDictionary acceptableTypesMap = new Dictionary { {typeof(byte), new[]{MatType.CV_8SC1, MatType.CV_8UC1}}, {typeof(sbyte), new[]{MatType.CV_8SC1, MatType.CV_8UC1}}, diff --git a/test/OpenCvSharp.Tests/core/MatTest.cs b/test/OpenCvSharp.Tests/core/MatTest.cs index 23e74622c..139805f14 100644 --- a/test/OpenCvSharp.Tests/core/MatTest.cs +++ b/test/OpenCvSharp.Tests/core/MatTest.cs @@ -364,6 +364,23 @@ public void Resize() mat.Resize(10); Assert.Equal(10, mat.Rows); } + + [Fact] + public void Reshape() + { + { + using var src = new Mat(2, 2, MatType.CV_8UC1); + using var dst = src.Reshape(0, 4); + Assert.Equal(new Size(1, 4), dst.Size()); + Assert.Equal(MatType.CV_8UC1, dst.Type()); + } + { + using var src = new Mat(2, 2, MatType.CV_8UC3); + using var dst = src.Reshape(1, 0); + Assert.Equal(new Size(6, 2), dst.Size()); + Assert.Equal(MatType.CV_8UC1, dst.Type()); + } + } [Fact] public void PushBack()