Skip to content

Commit 206eba0

Browse files
authored
Merge pull request shimat#980 from shimat/fix_Mat
fix Mat right, bottom and Contains
2 parents 40c9e6c + 7ed88fb commit 206eba0

File tree

7 files changed

+424
-13
lines changed

7 files changed

+424
-13
lines changed

.github/workflows/macos10.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/cache@v1
2626
with:
2727
path: opencv_macos/
28-
key: opencv-4.3.0-macos-rev3
28+
key: opencv-4.3.0-macos-rev4
2929

3030
- name: Build OpenCV
3131
if: steps.opencv-cache.outputs.cache-hit != 'true'

src/OpenCvSharp/Modules/core/Struct/Rect.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public int Top
304304
#endif
305305
public int Bottom
306306
{
307-
get { return Y + Height - 1; }
307+
get { return Y + Height; }
308308
}
309309

310310
#if LANG_JP
@@ -333,7 +333,7 @@ public int Left
333333
#endif
334334
public int Right
335335
{
336-
get { return X + Width - 1; }
336+
get { return X + Width; }
337337
}
338338

339339
#if LANG_JP
@@ -399,7 +399,7 @@ public Point TopLeft
399399
#endif
400400
public Point BottomRight
401401
{
402-
get { return new Point(X + Width - 1, Y + Height - 1); }
402+
get { return new Point(X + Width, Y + Height); }
403403
}
404404

405405
#endregion
@@ -423,7 +423,7 @@ public Point BottomRight
423423
#endif
424424
public readonly bool Contains(int x, int y)
425425
{
426-
return (X <= x && Y <= y && X + Width - 1 > x && Y + Height - 1 > y);
426+
return (X <= x && Y <= y && X + Width > x && Y + Height > y);
427427
}
428428

429429
#if LANG_JP

src/OpenCvSharp/Modules/core/Struct/Rect2d.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public double Top
302302
#endif
303303
public double Bottom
304304
{
305-
get { return Y + Height - 1; }
305+
get { return Y + Height; }
306306
}
307307
#if LANG_JP
308308
/// <summary>
@@ -329,7 +329,7 @@ public double Left
329329
#endif
330330
public double Right
331331
{
332-
get { return X + Width - 1; }
332+
get { return X + Width; }
333333
}
334334

335335
#if LANG_JP
@@ -393,7 +393,7 @@ public Point2d TopLeft
393393
#endif
394394
public Point2d BottomRight
395395
{
396-
get { return new Point2d(X + Width - 1, Y + Height - 1); }
396+
get { return new Point2d(X + Width, Y + Height); }
397397
}
398398
#endregion
399399

@@ -425,7 +425,7 @@ public readonly Rect ToRect()
425425
#endif
426426
public readonly bool Contains(double x, double y)
427427
{
428-
return (X <= x && Y <= y && X + Width - 1 > x && Y + Height - 1 > y);
428+
return (X <= x && Y <= y && X + Width > x && Y + Height > y);
429429
}
430430

431431
#if LANG_JP

src/OpenCvSharp/Modules/core/Struct/Rect2f.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public float Top
298298
#endif
299299
public float Bottom
300300
{
301-
get { return Y + Height - 1; }
301+
get { return Y + Height; }
302302
}
303303

304304
#if LANG_JP
@@ -327,7 +327,7 @@ public float Left
327327
#endif
328328
public float Right
329329
{
330-
get { return X + Width - 1; }
330+
get { return X + Width; }
331331
}
332332

333333
#if LANG_JP
@@ -393,7 +393,7 @@ public Point2f TopLeft
393393
#endif
394394
public Point2f BottomRight
395395
{
396-
get { return new Point2f(X + Width - 1, Y + Height - 1); }
396+
get { return new Point2f(X + Width, Y + Height); }
397397
}
398398
#endregion
399399

@@ -416,7 +416,7 @@ public Point2f BottomRight
416416
#endif
417417
public readonly bool Contains(float x, float y)
418418
{
419-
return (X <= x && Y <= y && X + Width - 1 > x && Y + Height - 1 > y);
419+
return (X <= x && Y <= y && X + Width > x && Y + Height > y);
420420
}
421421

422422
#if LANG_JP
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using Xunit;
2+
using Xunit.Abstractions;
3+
4+
namespace OpenCvSharp.Tests
5+
{
6+
public class Rect2dTest
7+
{
8+
private readonly ITestOutputHelper testOutputHelper;
9+
10+
public Rect2dTest(ITestOutputHelper testOutputHelper)
11+
{
12+
this.testOutputHelper = testOutputHelper;
13+
}
14+
15+
[Fact]
16+
public void TopLeft()
17+
{
18+
var rect = new Rect2d(10, 10, 100, 100);
19+
20+
Assert.Equal(10, rect.Top);
21+
Assert.Equal(10, rect.Left);
22+
Assert.Equal(new Point2d(10, 10), rect.TopLeft);
23+
}
24+
25+
[Fact]
26+
public void BottomRight()
27+
{
28+
var rect = new Rect2d(10, 10, 100, 100);
29+
30+
Assert.Equal(110, rect.Bottom);
31+
Assert.Equal(110, rect.Right);
32+
Assert.Equal(new Point2d(110, 110), rect.BottomRight);
33+
}
34+
35+
[Fact]
36+
public void Contains()
37+
{
38+
var rect = new Rect2d(new Point2d(0, 0), new Size2d(3,3));
39+
40+
// OpenCV typically assumes that the top and left boundary of the rectangle are inclusive,
41+
// while the right and bottom boundaries are not. https://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=rect
42+
43+
Assert.False(rect.Contains(0, -1));
44+
Assert.False(rect.Contains(-1, 0));
45+
Assert.False(rect.Contains(-1, -1));
46+
47+
Assert.True(rect.Contains(0, 0));
48+
Assert.True(rect.Contains(0, 1));
49+
Assert.True(rect.Contains(1, 0));
50+
Assert.True(rect.Contains(1, 1));
51+
52+
Assert.True(rect.Contains(2, 0));
53+
Assert.True(rect.Contains(2, 1));
54+
Assert.True(rect.Contains(2, 2));
55+
Assert.True(rect.Contains(0, 2));
56+
Assert.True(rect.Contains(1, 2));
57+
Assert.True(rect.Contains(2, 2));
58+
59+
Assert.False(rect.Contains(0, 3));
60+
Assert.False(rect.Contains(1, 3));
61+
Assert.False(rect.Contains(2, 3));
62+
Assert.False(rect.Contains(3, 3));
63+
Assert.False(rect.Contains(3, 0));
64+
Assert.False(rect.Contains(3, 1));
65+
Assert.False(rect.Contains(3, 2));
66+
Assert.False(rect.Contains(3, 3));
67+
}
68+
69+
// https://github.com/shimat/opencvsharp/issues/74
70+
// https://github.com/shimat/opencvsharp/issues/75
71+
[Fact]
72+
public void ContainsTopLeft()
73+
{
74+
var rect = new Rect2d(10, 10, 100, 100);
75+
76+
Assert.True(rect.Contains(rect.TopLeft));
77+
Assert.True(rect.Contains(rect.Left, rect.Top));
78+
}
79+
80+
[Fact]
81+
public void DoNotContainsBottomRight()
82+
{
83+
var rect = new Rect2d(10, 10, 100, 100);
84+
85+
Assert.False(rect.Contains(rect.BottomRight));
86+
Assert.False(rect.Contains(rect.Right, rect.Bottom));
87+
}
88+
89+
[Fact]
90+
public void ContainsRect()
91+
{
92+
var rect = new Rect2d(10, 10, 100, 100);
93+
94+
Assert.True(rect.Contains(rect));
95+
}
96+
97+
[Fact]
98+
public void IntersectsWith()
99+
{
100+
var rect1 = new Rect2d(0, 0, 100, 100);
101+
var rect2 = new Rect2d(0, 0, 100, 100);
102+
Assert.True(rect1.IntersectsWith(rect2));
103+
104+
rect2 = new Rect2d(50, 0, 100, 100);
105+
Assert.True(rect1.IntersectsWith(rect2));
106+
107+
rect2 = new Rect2d(100, 0, 100, 100);
108+
Assert.False(rect1.IntersectsWith(rect2));
109+
}
110+
111+
[Fact]
112+
public void Intersect()
113+
{
114+
var rect1 = new Rect2d(0, 0, 100, 100);
115+
var rect2 = new Rect2d(0, 0, 100, 100);
116+
117+
var intersect = rect1.Intersect(rect2);
118+
Assert.Equal(new Rect2d(0, 0, 100, 100), intersect);
119+
120+
rect2 = new Rect2d(50, 0, 100, 100);
121+
intersect = rect1.Intersect(rect2);
122+
Assert.Equal(new Rect2d(50, 0, 50, 100), intersect);
123+
124+
rect2 = new Rect2d(100, 0, 100, 100);
125+
intersect = rect1.Intersect(rect2);
126+
Assert.Equal(new Rect2d(100, 0, 0, 100), intersect);
127+
}
128+
129+
[Fact]
130+
public void FromLTRB()
131+
{
132+
var rect = Rect2d.FromLTRB(1, 2, 3, 4);
133+
134+
Assert.Equal(new Rect2d(1, 2, 3, 3), rect);
135+
}
136+
}
137+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using Xunit;
2+
using Xunit.Abstractions;
3+
4+
namespace OpenCvSharp.Tests
5+
{
6+
public class Rect2fTest
7+
{
8+
private readonly ITestOutputHelper testOutputHelper;
9+
10+
public Rect2fTest(ITestOutputHelper testOutputHelper)
11+
{
12+
this.testOutputHelper = testOutputHelper;
13+
}
14+
15+
[Fact]
16+
public void TopLeft()
17+
{
18+
var rect = new Rect2f(10, 10, 100, 100);
19+
20+
Assert.Equal(10, rect.Top);
21+
Assert.Equal(10, rect.Left);
22+
Assert.Equal(new Point2f(10, 10), rect.TopLeft);
23+
}
24+
25+
[Fact]
26+
public void BottomRight()
27+
{
28+
var rect = new Rect2f(10, 10, 100, 100);
29+
30+
Assert.Equal(110, rect.Bottom);
31+
Assert.Equal(110, rect.Right);
32+
Assert.Equal(new Point2f(110, 110), rect.BottomRight);
33+
}
34+
35+
[Fact]
36+
public void Contains()
37+
{
38+
var rect = new Rect2f(new Point2f(0, 0), new Size2f(3,3));
39+
40+
// OpenCV typically assumes that the top and left boundary of the rectangle are inclusive,
41+
// while the right and bottom boundaries are not. https://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=rect
42+
43+
Assert.False(rect.Contains(0, -1));
44+
Assert.False(rect.Contains(-1, 0));
45+
Assert.False(rect.Contains(-1, -1));
46+
47+
Assert.True(rect.Contains(0, 0));
48+
Assert.True(rect.Contains(0, 1));
49+
Assert.True(rect.Contains(1, 0));
50+
Assert.True(rect.Contains(1, 1));
51+
52+
Assert.True(rect.Contains(2, 0));
53+
Assert.True(rect.Contains(2, 1));
54+
Assert.True(rect.Contains(2, 2));
55+
Assert.True(rect.Contains(0, 2));
56+
Assert.True(rect.Contains(1, 2));
57+
Assert.True(rect.Contains(2, 2));
58+
59+
Assert.False(rect.Contains(0, 3));
60+
Assert.False(rect.Contains(1, 3));
61+
Assert.False(rect.Contains(2, 3));
62+
Assert.False(rect.Contains(3, 3));
63+
Assert.False(rect.Contains(3, 0));
64+
Assert.False(rect.Contains(3, 1));
65+
Assert.False(rect.Contains(3, 2));
66+
Assert.False(rect.Contains(3, 3));
67+
}
68+
69+
// https://github.com/shimat/opencvsharp/issues/74
70+
// https://github.com/shimat/opencvsharp/issues/75
71+
[Fact]
72+
public void ContainsTopLeft()
73+
{
74+
var rect = new Rect2f(10, 10, 100, 100);
75+
76+
Assert.True(rect.Contains(rect.TopLeft));
77+
Assert.True(rect.Contains(rect.Left, rect.Top));
78+
}
79+
80+
[Fact]
81+
public void DoNotContainsBottomRight()
82+
{
83+
var rect = new Rect2f(10, 10, 100, 100);
84+
85+
Assert.False(rect.Contains(rect.BottomRight));
86+
Assert.False(rect.Contains(rect.Right, rect.Bottom));
87+
}
88+
89+
[Fact]
90+
public void ContainsRect()
91+
{
92+
var rect = new Rect2f(10, 10, 100, 100);
93+
94+
Assert.True(rect.Contains(rect));
95+
}
96+
97+
[Fact]
98+
public void IntersectsWith()
99+
{
100+
var rect1 = new Rect2f(0, 0, 100, 100);
101+
var rect2 = new Rect2f(0, 0, 100, 100);
102+
Assert.True(rect1.IntersectsWith(rect2));
103+
104+
rect2 = new Rect2f(50, 0, 100, 100);
105+
Assert.True(rect1.IntersectsWith(rect2));
106+
107+
rect2 = new Rect2f(100, 0, 100, 100);
108+
Assert.False(rect1.IntersectsWith(rect2));
109+
}
110+
111+
[Fact]
112+
public void Intersect()
113+
{
114+
var rect1 = new Rect2f(0, 0, 100, 100);
115+
var rect2 = new Rect2f(0, 0, 100, 100);
116+
117+
var intersect = rect1.Intersect(rect2);
118+
Assert.Equal(new Rect2f(0, 0, 100, 100), intersect);
119+
120+
rect2 = new Rect2f(50, 0, 100, 100);
121+
intersect = rect1.Intersect(rect2);
122+
Assert.Equal(new Rect2f(50, 0, 50, 100), intersect);
123+
124+
rect2 = new Rect2f(100, 0, 100, 100);
125+
intersect = rect1.Intersect(rect2);
126+
Assert.Equal(new Rect2f(100, 0, 0, 100), intersect);
127+
}
128+
129+
[Fact]
130+
public void FromLTRB()
131+
{
132+
var rect = Rect2f.FromLTRB(1, 2, 3, 4);
133+
134+
Assert.Equal(new Rect2f(1, 2, 3, 3), rect);
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)