Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mat.Reshape test #1241

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions src/OpenCvSharp/Modules/core/Mat/Mat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,8 @@ public Mat(Mat m, Range rowRange, Range? colRange = null)
/// <param name="ranges">Array of selected ranges of m along each dimensionality.</param>
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();
Expand Down Expand Up @@ -539,9 +537,6 @@ public static MatExpr Zeros(Size size, MatType type)
/// <returns></returns>
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);
Expand Down Expand Up @@ -582,9 +577,6 @@ public static MatExpr Ones(Size size, MatType type)
/// <returns></returns>
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);
Expand Down Expand Up @@ -626,8 +618,6 @@ public static MatExpr Eye(int rows, int cols, MatType type)
public static Mat<TElem> FromArray<TElem>(params TElem[] arr)
where TElem : unmanaged
{
if (arr == null)
throw new ArgumentNullException(nameof(arr));
if (arr.Length == 0)
throw new ArgumentException("arr.Length == 0");

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -1788,8 +1776,6 @@ public void Create(Size size, MatType type)
/// <param name="type">New matrix type.</param>
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(
Expand Down Expand Up @@ -2461,8 +2447,6 @@ public Mat SubMat(Rect roi)
/// <returns></returns>
public Mat SubMat(params Range[] ranges)
{
if (ranges == null)
throw new ArgumentNullException(nameof(ranges));
ThrowIfDisposed();

NativeMethods.HandleException(
Expand Down Expand Up @@ -3355,7 +3339,7 @@ public void Set<T>(int[] idx, T value) where T : struct

#region Get/SetArray

private static readonly Dictionary<Type, int> dataDimensionMap = new Dictionary<Type, int>
private static readonly IReadOnlyDictionary<Type, int> dataDimensionMap = new Dictionary<Type, int>
{
{typeof(byte), 1},
{typeof(sbyte), 1},
Expand Down Expand Up @@ -3403,7 +3387,7 @@ public void Set<T>(int[] idx, T value) where T : struct
{typeof(Vec6d), 6},
};

private static readonly Dictionary<Type, MatType[]> acceptableTypesMap = new Dictionary<Type, MatType[]>
private static readonly IReadOnlyDictionary<Type, MatType[]> acceptableTypesMap = new Dictionary<Type, MatType[]>
{
{typeof(byte), new[]{MatType.CV_8SC1, MatType.CV_8UC1}},
{typeof(sbyte), new[]{MatType.CV_8SC1, MatType.CV_8UC1}},
Expand Down
17 changes: 17 additions & 0 deletions test/OpenCvSharp.Tests/core/MatTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down