@@ -55,7 +55,9 @@ Ptr<cv::cuda::DensePyrLKOpticalFlow> cv::cuda::DensePyrLKOpticalFlow::create(Siz
55
55
56
56
namespace pyrlk
57
57
{
58
- void loadConstants (int2 winSize, int iters, cudaStream_t stream);
58
+ void loadConstants (int * winSize, int iters, cudaStream_t stream);
59
+ void loadWinSize (int * winSize, int * halfWinSize, cudaStream_t stream);
60
+ void loadIters (int * iters, cudaStream_t stream);
59
61
template <typename T, int cn> struct pyrLK_caller
60
62
{
61
63
static void sparse (PtrStepSz<typename device::TypeVec<T, cn>::vec_type> I, PtrStepSz<typename device::TypeVec<T, cn>::vec_type> J, const float2* prevPts, float2* nextPts, uchar* status, float * err, int ptcount,
@@ -88,7 +90,8 @@ namespace
88
90
void dense (const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, Stream& stream);
89
91
90
92
protected:
91
- Size winSize_;
93
+ int winSize_[2 ];
94
+ int halfWinSize_[2 ];
92
95
int maxLevel_;
93
96
int iters_;
94
97
bool useInitialFlow_;
@@ -100,8 +103,14 @@ namespace
100
103
};
101
104
102
105
PyrLKOpticalFlowBase::PyrLKOpticalFlowBase (Size winSize, int maxLevel, int iters, bool useInitialFlow) :
103
- winSize_ (winSize), maxLevel_(maxLevel), iters_(iters), useInitialFlow_(useInitialFlow)
106
+ maxLevel_ (maxLevel), iters_(iters), useInitialFlow_(useInitialFlow)
104
107
{
108
+ winSize_[0 ] = winSize.width ;
109
+ winSize_[1 ] = winSize.height ;
110
+ halfWinSize_[0 ] = (winSize.width - 1 ) / 2 ;
111
+ halfWinSize_[1 ] = (winSize.height - 1 ) / 2 ;
112
+ pyrlk::loadWinSize (winSize_, halfWinSize_, 0 );
113
+ pyrlk::loadIters (&iters_, 0 );
105
114
}
106
115
107
116
void calcPatchSize (Size winSize, dim3& block, dim3& patch)
@@ -148,7 +157,7 @@ namespace
148
157
CV_Assert (prevPyr[0 ].size () == nextPyr[0 ].size ());
149
158
CV_Assert (prevPts.rows == 1 && prevPts.type () == CV_32FC2);
150
159
CV_Assert (maxLevel_ >= 0 );
151
- CV_Assert (winSize_. width > 2 && winSize_. height > 2 );
160
+ CV_Assert (winSize_[ 0 ] > 2 && winSize_[ 1 ] > 2 );
152
161
if (useInitialFlow_)
153
162
CV_Assert (nextPts.size () == prevPts.size () && nextPts.type () == prevPts.type ());
154
163
else
@@ -171,9 +180,11 @@ namespace
171
180
}
172
181
173
182
dim3 block, patch;
174
- calcPatchSize (winSize_, block, patch);
183
+ calcPatchSize (Size ( winSize_[ 0 ], winSize_[ 1 ]) , block, patch);
175
184
CV_Assert (patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6 );
176
- pyrlk::loadConstants (make_int2 (winSize_.width , winSize_.height ), iters_, StreamAccessor::getStream (stream));
185
+ cudaStream_t stream_ = StreamAccessor::getStream (stream);
186
+ pyrlk::loadWinSize (winSize_, halfWinSize_, stream_);
187
+ pyrlk::loadIters (&iters_, stream_);
177
188
178
189
const int cn = prevPyr[0 ].channels ();
179
190
const int type = prevPyr[0 ].depth ();
@@ -185,12 +196,12 @@ namespace
185
196
// while ushort does work, it has significantly worse performance, and thus doesn't pass accuracy tests.
186
197
static const func_t funcs[6 ][4 ] =
187
198
{
188
- { pyrlk::dispatcher<uchar, 1 > , /* pyrlk::dispatcher<uchar, 2>*/ 0 , pyrlk::dispatcher<uchar, 3 > , pyrlk::dispatcher<uchar, 4 > },
189
- { /* pyrlk::dispatcher<char, 1>*/ 0 , /* pyrlk::dispatcher<char, 2>*/ 0 , /* pyrlk::dispatcher<char, 3>*/ 0 , /* pyrlk::dispatcher<char, 4>*/ 0 },
190
- { pyrlk::dispatcher<ushort , 1 > , /* pyrlk::dispatcher<ushort, 2>*/ 0 , pyrlk::dispatcher<ushort , 3 > , pyrlk::dispatcher<ushort , 4 > },
191
- { /* pyrlk::dispatcher<short, 1>*/ 0 , /* pyrlk::dispatcher<short, 2>*/ 0 , /* pyrlk::dispatcher<short, 3>*/ 0 , /* pyrlk::dispatcher<short, 4>*/ 0 },
192
- { pyrlk::dispatcher<int , 1 > , /* pyrlk::dispatcher<int, 2>*/ 0 , pyrlk::dispatcher<int , 3 > , pyrlk::dispatcher<int , 4 > },
193
- { pyrlk::dispatcher<float , 1 > , /* pyrlk::dispatcher<float, 2>*/ 0 , pyrlk::dispatcher<float , 3 > , pyrlk::dispatcher<float , 4 > }
199
+ { pyrlk::dispatcher<uchar, 1 > , /* pyrlk::dispatcher<uchar, 2>*/ 0 , pyrlk::dispatcher<uchar, 3 > , pyrlk::dispatcher<uchar, 4 > },
200
+ { /* pyrlk::dispatcher<char, 1>*/ 0 , /* pyrlk::dispatcher<char, 2>*/ 0 , /* pyrlk::dispatcher<char, 3>*/ 0 , /* pyrlk::dispatcher<char, 4>*/ 0 },
201
+ { pyrlk::dispatcher<ushort , 1 > , /* pyrlk::dispatcher<ushort, 2>*/ 0 , pyrlk::dispatcher<ushort , 3 > , pyrlk::dispatcher<ushort , 4 > },
202
+ { /* pyrlk::dispatcher<short, 1>*/ 0 , /* pyrlk::dispatcher<short, 2>*/ 0 , /* pyrlk::dispatcher<short, 3>*/ 0 , /* pyrlk::dispatcher<short, 4>*/ 0 },
203
+ { pyrlk::dispatcher<int , 1 > , /* pyrlk::dispatcher<int, 2>*/ 0 , pyrlk::dispatcher<int , 3 > , pyrlk::dispatcher<int , 4 > },
204
+ { pyrlk::dispatcher<float , 1 > , /* pyrlk::dispatcher<float, 2>*/ 0 , pyrlk::dispatcher<float , 3 > , pyrlk::dispatcher<float , 4 > }
194
205
};
195
206
196
207
func_t func = funcs[type][cn-1 ];
@@ -201,7 +212,7 @@ namespace
201
212
prevPts.ptr <float2>(), nextPts.ptr <float2>(),
202
213
status.ptr (), level == 0 && err ? err->ptr <float >() : 0 ,
203
214
prevPts.cols , level, block, patch,
204
- StreamAccessor::getStream (stream) );
215
+ stream_ );
205
216
}
206
217
}
207
218
@@ -229,7 +240,7 @@ namespace
229
240
CV_Assert ( prevImg.type () == CV_8UC1 );
230
241
CV_Assert ( prevImg.size () == nextImg.size () && prevImg.type () == nextImg.type () );
231
242
CV_Assert ( maxLevel_ >= 0 );
232
- CV_Assert ( winSize_. width > 2 && winSize_. height > 2 );
243
+ CV_Assert ( winSize_[ 0 ] > 2 && winSize_[ 1 ] > 2 );
233
244
234
245
// build the image pyramids.
235
246
@@ -262,9 +273,11 @@ namespace
262
273
vPyr[0 ].setTo (Scalar::all (0 ), stream);
263
274
uPyr[1 ].setTo (Scalar::all (0 ), stream);
264
275
vPyr[1 ].setTo (Scalar::all (0 ), stream);
265
-
266
- int2 winSize2i = make_int2 (winSize_.width , winSize_.height );
267
- pyrlk::loadConstants (winSize2i, iters_, StreamAccessor::getStream (stream));
276
+ cudaStream_t stream_ = StreamAccessor::getStream (stream);
277
+ pyrlk::loadWinSize (winSize_, halfWinSize_, stream_);
278
+ pyrlk::loadIters (&iters_, stream_);
279
+ int2 winSize2i = make_int2 (winSize_[0 ], winSize_[1 ]);
280
+ // pyrlk::loadConstants(winSize2i, iters_, StreamAccessor::getStream(stream));
268
281
269
282
int idx = 0 ;
270
283
@@ -275,7 +288,7 @@ namespace
275
288
pyrlk::pyrLK_caller<float ,1 >::dense (prevPyr_[level], nextPyr_[level],
276
289
uPyr[idx], vPyr[idx], uPyr[idx2], vPyr[idx2],
277
290
PtrStepSzf (), winSize2i,
278
- StreamAccessor::getStream (stream) );
291
+ stream_ );
279
292
280
293
if (level > 0 )
281
294
idx = idx2;
@@ -293,8 +306,13 @@ namespace
293
306
{
294
307
}
295
308
296
- virtual Size getWinSize () const { return winSize_; }
297
- virtual void setWinSize (Size winSize) { winSize_ = winSize; }
309
+ virtual Size getWinSize () const { return cv::Size (winSize_[0 ], winSize_[1 ]); }
310
+ virtual void setWinSize (Size winSize) {
311
+ winSize_[0 ] = winSize.width ;
312
+ winSize_[1 ] = winSize.height ;
313
+ halfWinSize_[0 ] = (winSize.width - 1 ) / 2 ;
314
+ halfWinSize_[1 ] = (winSize.height -1 ) / 2 ;
315
+ }
298
316
299
317
virtual int getMaxLevel () const { return maxLevel_; }
300
318
virtual void setMaxLevel (int maxLevel) { maxLevel_ = maxLevel; }
@@ -339,8 +357,13 @@ namespace
339
357
{
340
358
}
341
359
342
- virtual Size getWinSize () const { return winSize_; }
343
- virtual void setWinSize (Size winSize) { winSize_ = winSize; }
360
+ virtual Size getWinSize () const { return cv::Size (winSize_[0 ], winSize_[1 ]); }
361
+ virtual void setWinSize (Size winSize) {
362
+ winSize_[0 ] = winSize.width ;
363
+ winSize_[1 ] = winSize.height ;
364
+ halfWinSize_[0 ] = (winSize.width - 1 ) / 2 ;
365
+ halfWinSize_[1 ] = (winSize.height -1 ) / 2 ;
366
+ }
344
367
345
368
virtual int getMaxLevel () const { return maxLevel_; }
346
369
virtual void setMaxLevel (int maxLevel) { maxLevel_ = maxLevel; }
0 commit comments