Skip to content

Commit b5ea1be

Browse files
committed
fix bugs of UMat.IsSubMatrix and UMat[range/roi]
1 parent 0a57bb5 commit b5ea1be

File tree

3 files changed

+207
-17
lines changed

3 files changed

+207
-17
lines changed

OpenCvSharp.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<s:Boolean x:Key="/Default/UserDictionary/Words/=stddev/@EntryIndexedValue">True</s:Boolean>
117117
<s:Boolean x:Key="/Default/UserDictionary/Words/=stitcher/@EntryIndexedValue">True</s:Boolean>
118118
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subdiv/@EntryIndexedValue">True</s:Boolean>
119+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Submat/@EntryIndexedValue">True</s:Boolean>
119120
<s:Boolean x:Key="/Default/UserDictionary/Words/=submatrix/@EntryIndexedValue">True</s:Boolean>
120121
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subtractor/@EntryIndexedValue">True</s:Boolean>
121122
<s:Boolean x:Key="/Default/UserDictionary/Words/=Superpixel/@EntryIndexedValue">True</s:Boolean>

src/OpenCvSharpExtern/core_UMat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ CVAPI(ExceptionStatus) core_UMat_adjustROI(cv::UMat* self, int dtop, int dbottom
329329
END_WRAP
330330
}
331331

332-
CVAPI(ExceptionStatus) core_UMat_subUMat1(cv::UMat* self, int rowStart, int rowEnd, int colStart, int colEnd, cv::UMat** returnValue)
332+
CVAPI(ExceptionStatus) core_UMat_subMat1(cv::UMat* self, int rowStart, int rowEnd, int colStart, int colEnd, cv::UMat** returnValue)
333333
{
334334
BEGIN_WRAP
335335
const cv::Range rowRange(rowStart, rowEnd);
@@ -338,7 +338,7 @@ CVAPI(ExceptionStatus) core_UMat_subUMat1(cv::UMat* self, int rowStart, int rowE
338338
*returnValue = new cv::UMat(ret);
339339
END_WRAP
340340
}
341-
CVAPI(ExceptionStatus) core_UMat_subUMat2(cv::UMat* self, int nRanges, MyCvSlice* ranges, cv::UMat** returnValue)
341+
CVAPI(ExceptionStatus) core_UMat_subMat2(cv::UMat* self, int nRanges, MyCvSlice* ranges, cv::UMat** returnValue)
342342
{
343343
BEGIN_WRAP
344344
std::vector<cv::Range> rangesVec(nRanges);
@@ -358,7 +358,7 @@ CVAPI(ExceptionStatus) core_UMat_isContinuous(cv::UMat* self, int* returnValue)
358358
END_WRAP
359359
}
360360

361-
CVAPI(ExceptionStatus) core_UMat_isSubMatrix(cv::UMat* self, int* returnValue)
361+
CVAPI(ExceptionStatus) core_UMat_isSubmatrix(cv::UMat* self, int* returnValue)
362362
{
363363
BEGIN_WRAP
364364
*returnValue = self->isSubmatrix() ? 1 : 0;

test/OpenCvSharp.Tests/core/UMatTest.cs

Lines changed: 203 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Xunit;
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
24

35
namespace OpenCvSharp.Tests.Core
46
{
@@ -23,10 +25,44 @@ public void Empty()
2325
[Fact]
2426
public void Size()
2527
{
26-
using var umat = new UMat(new Size(1,2), MatType.CV_8UC1);
27-
Assert.Equal(new Size(1, 2), umat.Size());
28-
Assert.Equal(2, umat.Rows);
29-
Assert.Equal(1, umat.Cols);
28+
using var umat = new UMat(new Size(3,4), MatType.CV_8UC1);
29+
Assert.Equal(new Size(3, 4), umat.Size());
30+
Assert.Equal(4, umat.Rows);
31+
Assert.Equal(3, umat.Cols);
32+
}
33+
34+
[Fact]
35+
public void Total()
36+
{
37+
using var umat = new UMat(new Size(3, 4), MatType.CV_8UC1);
38+
Assert.Equal(3 * 4, umat.Total());
39+
}
40+
41+
[Fact]
42+
public void Dims()
43+
{
44+
using var umat = new UMat(new Size(1, 1), MatType.CV_16UC1);
45+
Assert.Equal(2, umat.Dims);
46+
}
47+
48+
[Fact]
49+
public void Step()
50+
{
51+
using var umat1 = new UMat(new Size(3, 3), MatType.CV_8UC1);
52+
Assert.Equal(3 * 1 * sizeof(byte), umat1.Step());
53+
Assert.Equal(3 * 1, umat1.Step1());
54+
55+
using var umat2 = new UMat(new Size(3, 3), MatType.CV_8UC3);
56+
Assert.Equal(3 * 3 * sizeof(byte), umat2.Step());
57+
Assert.Equal(3 * 3, umat2.Step1());
58+
59+
using var umat3 = new UMat(new Size(3, 3), MatType.CV_32SC1);
60+
Assert.Equal(3 * 1 * sizeof(int), umat3.Step());
61+
Assert.Equal(3 * 1, umat3.Step1());
62+
63+
using var umat4 = new UMat(new Size(3, 3), MatType.CV_32SC3);
64+
Assert.Equal(3 * 3 * sizeof(int), umat4.Step());
65+
Assert.Equal(3 * 3, umat4.Step1());
3066
}
3167

3268
[Fact]
@@ -38,6 +74,29 @@ public void Type()
3874
Assert.Equal(MatType.CV_8U, umat.Depth());
3975
}
4076

77+
[Fact]
78+
public void ElemSize()
79+
{
80+
foreach (var (matTypeFunction, elemSize1) in GetInputs())
81+
{
82+
for (int ch = 1; ch <= 6; ch++)
83+
{
84+
using var umat = new UMat(1, 1, matTypeFunction(ch));
85+
Assert.Equal(elemSize1, umat.ElemSize1());
86+
Assert.Equal(elemSize1 * ch, umat.ElemSize());
87+
}
88+
}
89+
90+
static IEnumerable<(Func<int, MatType> MatTypeFunction, int elemSize1)> GetInputs()
91+
{
92+
yield return (MatType.CV_8UC, 1);
93+
yield return (MatType.CV_16SC, 2);
94+
yield return (MatType.CV_32SC, 4);
95+
yield return (MatType.CV_32FC, 4);
96+
yield return (MatType.CV_64FC, 8);
97+
}
98+
}
99+
41100
[Fact]
42101
public void GetMat()
43102
{
@@ -57,16 +116,16 @@ public void CastToInputArray()
57116

58117
Cv2.BitwiseNot(src, dst);
59118

60-
AssertEquals(dst, new byte[,] {{255 - 64}});
119+
AssertEquals(dst, MatType.CV_8UC1, new byte[,] {{255 - 64}});
61120
}
62121

63122
[Fact]
64123
public void Diag()
65124
{
66-
using var main = new UMat(3, 1, MatType.CV_8UC1, new Scalar(3));
125+
using var main = new UMat(3, 1, MatType.CV_32FC1, new Scalar(3));
67126
using var diag = UMat.Diag(main);
68127

69-
AssertEquals(diag, new byte[,]
128+
AssertEquals(diag, MatType.CV_32FC1, new float[,]
70129
{
71130
{ 3, 0, 0 },
72131
{ 0, 3, 0 },
@@ -75,30 +134,160 @@ public void Diag()
75134
}
76135

77136
[Fact]
78-
public void Clone()
137+
public void CopyToClone()
79138
{
80-
var values = new byte[,] { { 1, 2 }, { 3, 4 } };
139+
var values = new double[,] { { 1, 2 }, { 3, 4 } };
81140
using var srcMat = Mat.FromArray(values);
82141

83142
using var srcUMat = new UMat();
84143
srcMat.CopyTo(srcUMat);
85144

86145
var dstUMat = srcUMat.Clone();
87146

88-
AssertEquals(dstUMat, values);
147+
AssertEquals(dstUMat, MatType.CV_64FC1, values);
89148
}
90149

91-
private static void AssertEquals(UMat umat, byte[,] expectedValues)
150+
[Fact]
151+
public void AssignTo()
152+
{
153+
using var srcUMat = new UMat(2, 2, MatType.CV_32SC1, Scalar.All(1234));
154+
155+
using var dstUMat = new UMat();
156+
srcUMat.AssignTo(dstUMat);
157+
158+
AssertEquals(dstUMat, MatType.CV_32SC1, new [,]
159+
{
160+
{ 1234, 1234 }, { 1234, 1234 }
161+
});
162+
}
163+
164+
[Fact]
165+
public void SetTo()
166+
{
167+
using var umat = new UMat(2, 2, MatType.CV_16SC1);
168+
umat.SetTo(Scalar.All(-5));
169+
170+
AssertEquals(umat, MatType.CV_16SC1, new short[,]
171+
{
172+
{ -5, -5 }, { -5, -5 }
173+
});
174+
}
175+
176+
[Fact]
177+
public void Dot()
178+
{
179+
using var mat1 = new Mat(2, 1, MatType.CV_32SC1, new[] {1, 2});
180+
using var mat2 = new Mat(2, 1, MatType.CV_32SC1, new[] {3, 4});
181+
182+
using var umat1 = new UMat(2, 1, MatType.CV_32SC1);
183+
using var umat2 = new UMat(2, 1, MatType.CV_32SC1);
184+
mat1.CopyTo(umat1);
185+
mat2.CopyTo(umat2);
186+
187+
Assert.Equal(1 * 3 + 2 * 4, umat1.Dot(umat2), 6);
188+
}
189+
190+
[Fact]
191+
public void Zeros()
192+
{
193+
using var umat = UMat.Zeros(2, 3, MatType.CV_16SC1);
194+
195+
AssertEquals(umat, MatType.CV_16SC1, new short[,]
196+
{
197+
{ 0, 0, 0 }, { 0, 0, 0 }
198+
});
199+
}
200+
201+
[Fact]
202+
public void Ones()
203+
{
204+
using var umat = UMat.Ones(2, 3, MatType.CV_8UC1);
205+
206+
AssertEquals(umat, MatType.CV_8UC1, new byte[,]
207+
{
208+
{ 1, 1, 1 }, { 1, 1, 1 }
209+
});
210+
}
211+
212+
[Fact]
213+
public void Eye()
214+
{
215+
using var umat = UMat.Eye(3, 3, MatType.CV_32FC1);
216+
217+
AssertEquals(umat, MatType.CV_32FC1, new float[,]
218+
{
219+
{ 1, 0, 0 },
220+
{ 0, 1, 0 },
221+
{ 0, 0, 1 }
222+
});
223+
}
224+
225+
[Fact]
226+
public void SubmatByRect()
227+
{
228+
var values = new double[,]
229+
{
230+
{ 1, 2, 3, 4 },
231+
{ 5, 6, 7, 8 },
232+
{ 9, 10, 11, 12 },
233+
{ 13, 14, 15, 16 }
234+
};
235+
using var srcMat = Mat.FromArray(values);
236+
using var srcUMat = new UMat();
237+
srcMat.CopyTo(srcUMat);
238+
239+
Assert.True(srcUMat.IsContinuous());
240+
Assert.False(srcUMat.IsSubmatrix());
241+
242+
var subUMat = srcUMat[new Rect(1, 2, 2, 2)];
243+
AssertEquals(subUMat, MatType.CV_64FC1, new double[,]
244+
{
245+
{10, 11},
246+
{14, 15}
247+
});
248+
Assert.False(subUMat.IsContinuous());
249+
Assert.True(subUMat.IsSubmatrix());
250+
}
251+
252+
[Fact]
253+
public void SubmatByRange()
254+
{
255+
var values = new double[,]
256+
{
257+
{ 1, 2, 3, 4 },
258+
{ 5, 6, 7, 8 },
259+
{ 9, 10, 11, 12 },
260+
{ 13, 14, 15, 16 }
261+
};
262+
using var srcMat = Mat.FromArray(values);
263+
using var srcUMat = new UMat();
264+
srcMat.CopyTo(srcUMat);
265+
266+
Assert.True(srcUMat.IsContinuous());
267+
Assert.False(srcUMat.IsSubmatrix());
268+
269+
var subUMat = srcUMat[new Range(2, 4), new Range(1, 3)];
270+
AssertEquals(subUMat, MatType.CV_64FC1, new double[,]
271+
{
272+
{10, 11},
273+
{14, 15}
274+
});
275+
Assert.False(subUMat.IsContinuous());
276+
Assert.True(subUMat.IsSubmatrix());
277+
}
278+
279+
private static void AssertEquals<T>(UMat umat, MatType expectedType, T[,] expectedValues)
280+
where T : unmanaged
92281
{
93282
int rows = expectedValues.GetLength(0);
94283
int cols = expectedValues.GetLength(1);
95284
Assert.False(umat.Empty());
96285
Assert.Equal(rows, umat.Rows);
97286
Assert.Equal(cols, umat.Cols);
98-
Assert.Equal(MatType.CV_8UC1, umat.Type());
287+
Assert.Equal(expectedType, umat.Type());
99288

100289
using var mat = umat.GetMat(AccessFlag.READ);
101-
Assert.True(mat.GetRectangularArray(out byte[,] matValues));
290+
Assert.True(mat.GetRectangularArray(out T[,] matValues));
102291
Assert.Equal(expectedValues, matValues);
103292
}
104293
}

0 commit comments

Comments
 (0)