Skip to content

Commit f6b0fbc

Browse files
authored
Merge pull request shimat#1434 from timstokman/master
Fix access violation BackgroundSubtractorKNN
2 parents c08f82d + 0fc523c commit f6b0fbc

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

src/OpenCvSharp/Modules/video/BackgroundSubtractorKNN.cs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,20 @@ public int History
6363
get
6464
{
6565
ThrowIfDisposed();
66+
if (objectPtr == null)
67+
throw new NotSupportedException("objectPtr == null");
6668
NativeMethods.HandleException(
67-
NativeMethods.video_BackgroundSubtractorKNN_getHistory(ptr, out var ret));
69+
NativeMethods.video_BackgroundSubtractorKNN_getHistory(objectPtr.CvPtr, out var ret));
6870
GC.KeepAlive(this);
6971
return ret;
7072
}
7173
set
7274
{
7375
ThrowIfDisposed();
76+
if (objectPtr == null)
77+
throw new NotSupportedException("objectPtr == null");
7478
NativeMethods.HandleException(
75-
NativeMethods.video_BackgroundSubtractorKNN_setHistory(ptr, value));
79+
NativeMethods.video_BackgroundSubtractorKNN_setHistory(objectPtr.CvPtr, value));
7680
GC.KeepAlive(this);
7781
}
7882
}
@@ -85,16 +89,20 @@ public int NSamples
8589
get
8690
{
8791
ThrowIfDisposed();
92+
if (objectPtr == null)
93+
throw new NotSupportedException("objectPtr == null");
8894
NativeMethods.HandleException(
89-
NativeMethods.video_BackgroundSubtractorKNN_getNSamples(ptr, out var ret));
95+
NativeMethods.video_BackgroundSubtractorKNN_getNSamples(objectPtr.CvPtr, out var ret));
9096
GC.KeepAlive(this);
9197
return ret;
9298
}
9399
set
94100
{
95101
ThrowIfDisposed();
102+
if (objectPtr == null)
103+
throw new NotSupportedException("objectPtr == null");
96104
NativeMethods.HandleException(
97-
NativeMethods.video_BackgroundSubtractorKNN_setNSamples(ptr, value));
105+
NativeMethods.video_BackgroundSubtractorKNN_setNSamples(objectPtr.CvPtr, value));
98106
GC.KeepAlive(this);
99107
}
100108
}
@@ -108,16 +116,20 @@ public double Dist2Threshold
108116
get
109117
{
110118
ThrowIfDisposed();
119+
if (objectPtr == null)
120+
throw new NotSupportedException("objectPtr == null");
111121
NativeMethods.HandleException(
112-
NativeMethods.video_BackgroundSubtractorKNN_getDist2Threshold(ptr, out var ret));
122+
NativeMethods.video_BackgroundSubtractorKNN_getDist2Threshold(objectPtr.CvPtr, out var ret));
113123
GC.KeepAlive(this);
114124
return ret;
115125
}
116126
set
117127
{
118128
ThrowIfDisposed();
129+
if (objectPtr == null)
130+
throw new NotSupportedException("objectPtr == null");
119131
NativeMethods.HandleException(
120-
NativeMethods.video_BackgroundSubtractorKNN_setDist2Threshold(ptr, value));
132+
NativeMethods.video_BackgroundSubtractorKNN_setDist2Threshold(objectPtr.CvPtr, value));
121133
GC.KeepAlive(this);
122134
}
123135
}
@@ -132,16 +144,20 @@ public int KNNSamples
132144
get
133145
{
134146
ThrowIfDisposed();
147+
if (objectPtr == null)
148+
throw new NotSupportedException("objectPtr == null");
135149
NativeMethods.HandleException(
136-
NativeMethods.video_BackgroundSubtractorKNN_getkNNSamples(ptr, out var ret));
150+
NativeMethods.video_BackgroundSubtractorKNN_getkNNSamples(objectPtr.CvPtr, out var ret));
137151
GC.KeepAlive(this);
138152
return ret;
139153
}
140154
set
141155
{
142156
ThrowIfDisposed();
157+
if (objectPtr == null)
158+
throw new NotSupportedException("objectPtr == null");
143159
NativeMethods.HandleException(
144-
NativeMethods.video_BackgroundSubtractorKNN_setkNNSamples(ptr, value));
160+
NativeMethods.video_BackgroundSubtractorKNN_setkNNSamples(objectPtr.CvPtr, value));
145161
GC.KeepAlive(this);
146162
}
147163
}
@@ -155,16 +171,20 @@ public bool DetectShadows
155171
get
156172
{
157173
ThrowIfDisposed();
174+
if (objectPtr == null)
175+
throw new NotSupportedException("objectPtr == null");
158176
NativeMethods.HandleException(
159-
NativeMethods.video_BackgroundSubtractorKNN_getDetectShadows(ptr, out var ret));
177+
NativeMethods.video_BackgroundSubtractorKNN_getDetectShadows(objectPtr.CvPtr, out var ret));
160178
GC.KeepAlive(this);
161179
return ret != 0;
162180
}
163181
set
164182
{
165183
ThrowIfDisposed();
184+
if (objectPtr == null)
185+
throw new NotSupportedException("objectPtr == null");
166186
NativeMethods.HandleException(
167-
NativeMethods.video_BackgroundSubtractorKNN_setDetectShadows(ptr, value ? 1 : 0));
187+
NativeMethods.video_BackgroundSubtractorKNN_setDetectShadows(objectPtr.CvPtr, value ? 1 : 0));
168188
GC.KeepAlive(this);
169189
}
170190
}
@@ -179,16 +199,20 @@ public int ShadowValue
179199
get
180200
{
181201
ThrowIfDisposed();
202+
if (objectPtr == null)
203+
throw new NotSupportedException("objectPtr == null");
182204
NativeMethods.HandleException(
183-
NativeMethods.video_BackgroundSubtractorKNN_getShadowValue(ptr, out var ret));
205+
NativeMethods.video_BackgroundSubtractorKNN_getShadowValue(objectPtr.CvPtr, out var ret));
184206
GC.KeepAlive(this);
185207
return ret;
186208
}
187209
set
188210
{
189211
ThrowIfDisposed();
212+
if (objectPtr == null)
213+
throw new NotSupportedException("objectPtr == null");
190214
NativeMethods.HandleException(
191-
NativeMethods.video_BackgroundSubtractorKNN_setShadowValue(ptr, value));
215+
NativeMethods.video_BackgroundSubtractorKNN_setShadowValue(objectPtr.CvPtr, value));
192216
GC.KeepAlive(this);
193217
}
194218
}
@@ -205,16 +229,20 @@ public double ShadowThreshold
205229
get
206230
{
207231
ThrowIfDisposed();
232+
if (objectPtr == null)
233+
throw new NotSupportedException("objectPtr == null");
208234
NativeMethods.HandleException(
209-
NativeMethods.video_BackgroundSubtractorKNN_getShadowThreshold(ptr, out var ret));
235+
NativeMethods.video_BackgroundSubtractorKNN_getShadowThreshold(objectPtr.CvPtr, out var ret));
210236
GC.KeepAlive(this);
211237
return ret;
212238
}
213239
set
214240
{
215241
ThrowIfDisposed();
242+
if (objectPtr == null)
243+
throw new NotSupportedException("objectPtr == null");
216244
NativeMethods.HandleException(
217-
NativeMethods.video_BackgroundSubtractorKNN_setShadowThreshold(ptr, value));
245+
NativeMethods.video_BackgroundSubtractorKNN_setShadowThreshold(objectPtr.CvPtr, value));
218246
GC.KeepAlive(this);
219247
}
220248
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Xunit;
2+
3+
namespace OpenCvSharp.Tests.Video
4+
{
5+
// ReSharper disable InconsistentNaming
6+
7+
public class BackgroundSubtractorKNNTest : TestBase
8+
{
9+
[Fact]
10+
public void CheckProperties()
11+
{
12+
using (var knn = BackgroundSubtractorKNN.Create())
13+
{
14+
knn.DetectShadows = knn.DetectShadows;
15+
knn.History = knn.History;
16+
knn.ShadowThreshold = knn.ShadowThreshold;
17+
knn.ShadowValue = knn.ShadowValue;
18+
knn.Dist2Threshold = knn.Dist2Threshold;
19+
knn.KNNSamples = knn.KNNSamples;
20+
knn.NSamples = knn.NSamples;
21+
}
22+
}
23+
24+
[Fact]
25+
public void Apply()
26+
{
27+
using (var knn = BackgroundSubtractorKNN.Create())
28+
using (var src = Image("lenna.png"))
29+
using (var dst = new Mat())
30+
{
31+
knn.Apply(src, dst);
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)