@@ -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,11 @@ 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
+ winSize_ ({winSize.width , winSize.height }), halfWinSize_({(winSize.width - 1 ) / 2 , (winSize.height - 1 ) / 2 }),
107
+ maxLevel_ (maxLevel), iters_(iters), useInitialFlow_(useInitialFlow)
104
108
{
109
+ pyrlk::loadWinSize (winSize_, halfWinSize_, 0 );
110
+ pyrlk::loadIters (&iters_, 0 );
105
111
}
106
112
107
113
void calcPatchSize (Size winSize, dim3& block, dim3& patch)
@@ -148,7 +154,7 @@ namespace
148
154
CV_Assert (prevPyr[0 ].size () == nextPyr[0 ].size ());
149
155
CV_Assert (prevPts.rows == 1 && prevPts.type () == CV_32FC2);
150
156
CV_Assert (maxLevel_ >= 0 );
151
- CV_Assert (winSize_. width > 2 && winSize_. height > 2 );
157
+ CV_Assert (winSize_[ 0 ] > 2 && winSize_[ 1 ] > 2 );
152
158
if (useInitialFlow_)
153
159
CV_Assert (nextPts.size () == prevPts.size () && nextPts.type () == prevPts.type ());
154
160
else
@@ -171,9 +177,11 @@ namespace
171
177
}
172
178
173
179
dim3 block, patch;
174
- calcPatchSize (winSize_, block, patch);
180
+ calcPatchSize (Size ( winSize_[ 0 ], winSize_[ 1 ]) , block, patch);
175
181
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));
182
+ cudaStream_t stream_ = StreamAccessor::getStream (stream);
183
+ pyrlk::loadWinSize (winSize_, halfWinSize_, stream_);
184
+ pyrlk::loadIters (&iters_, stream_);
177
185
178
186
const int cn = prevPyr[0 ].channels ();
179
187
const int type = prevPyr[0 ].depth ();
@@ -185,12 +193,12 @@ namespace
185
193
// while ushort does work, it has significantly worse performance, and thus doesn't pass accuracy tests.
186
194
static const func_t funcs[6 ][4 ] =
187
195
{
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 > }
196
+ { pyrlk::dispatcher<uchar, 1 > , /* pyrlk::dispatcher<uchar, 2>*/ 0 , pyrlk::dispatcher<uchar, 3 > , pyrlk::dispatcher<uchar, 4 > },
197
+ { /* pyrlk::dispatcher<char, 1>*/ 0 , /* pyrlk::dispatcher<char, 2>*/ 0 , /* pyrlk::dispatcher<char, 3>*/ 0 , /* pyrlk::dispatcher<char, 4>*/ 0 },
198
+ { pyrlk::dispatcher<ushort , 1 > , /* pyrlk::dispatcher<ushort, 2>*/ 0 , pyrlk::dispatcher<ushort , 3 > , pyrlk::dispatcher<ushort , 4 > },
199
+ { /* pyrlk::dispatcher<short, 1>*/ 0 , /* pyrlk::dispatcher<short, 2>*/ 0 , /* pyrlk::dispatcher<short, 3>*/ 0 , /* pyrlk::dispatcher<short, 4>*/ 0 },
200
+ { pyrlk::dispatcher<int , 1 > , /* pyrlk::dispatcher<int, 2>*/ 0 , pyrlk::dispatcher<int , 3 > , pyrlk::dispatcher<int , 4 > },
201
+ { pyrlk::dispatcher<float , 1 > , /* pyrlk::dispatcher<float, 2>*/ 0 , pyrlk::dispatcher<float , 3 > , pyrlk::dispatcher<float , 4 > }
194
202
};
195
203
196
204
func_t func = funcs[type][cn-1 ];
@@ -201,7 +209,7 @@ namespace
201
209
prevPts.ptr <float2>(), nextPts.ptr <float2>(),
202
210
status.ptr (), level == 0 && err ? err->ptr <float >() : 0 ,
203
211
prevPts.cols , level, block, patch,
204
- StreamAccessor::getStream (stream) );
212
+ stream_ );
205
213
}
206
214
}
207
215
@@ -229,7 +237,7 @@ namespace
229
237
CV_Assert ( prevImg.type () == CV_8UC1 );
230
238
CV_Assert ( prevImg.size () == nextImg.size () && prevImg.type () == nextImg.type () );
231
239
CV_Assert ( maxLevel_ >= 0 );
232
- CV_Assert ( winSize_. width > 2 && winSize_. height > 2 );
240
+ CV_Assert ( winSize_[ 0 ] > 2 && winSize_[ 1 ] > 2 );
233
241
234
242
// build the image pyramids.
235
243
@@ -262,9 +270,11 @@ namespace
262
270
vPyr[0 ].setTo (Scalar::all (0 ), stream);
263
271
uPyr[1 ].setTo (Scalar::all (0 ), stream);
264
272
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));
273
+ cudaStream_t stream_ = StreamAccessor::getStream (stream);
274
+ pyrlk::loadWinSize (winSize_, halfWinSize_, stream_);
275
+ pyrlk::loadIters (&iters_, stream_);
276
+ int2 winSize2i = make_int2 (winSize_[0 ], winSize_[1 ]);
277
+ // pyrlk::loadConstants(winSize2i, iters_, StreamAccessor::getStream(stream));
268
278
269
279
int idx = 0 ;
270
280
@@ -275,7 +285,7 @@ namespace
275
285
pyrlk::pyrLK_caller<float ,1 >::dense (prevPyr_[level], nextPyr_[level],
276
286
uPyr[idx], vPyr[idx], uPyr[idx2], vPyr[idx2],
277
287
PtrStepSzf (), winSize2i,
278
- StreamAccessor::getStream (stream) );
288
+ stream_ );
279
289
280
290
if (level > 0 )
281
291
idx = idx2;
@@ -293,8 +303,13 @@ namespace
293
303
{
294
304
}
295
305
296
- virtual Size getWinSize () const { return winSize_; }
297
- virtual void setWinSize (Size winSize) { winSize_ = winSize; }
306
+ virtual Size getWinSize () const { return cv::Size (winSize_[0 ], winSize_[1 ]); }
307
+ virtual void setWinSize (Size winSize) {
308
+ winSize_[0 ] = winSize.width ;
309
+ winSize_[1 ] = winSize.height ;
310
+ halfWinSize_[0 ] = (winSize.width - 1 ) / 2 ;
311
+ halfWinSize_[1 ] = (winSize.height -1 ) / 2 ;
312
+ }
298
313
299
314
virtual int getMaxLevel () const { return maxLevel_; }
300
315
virtual void setMaxLevel (int maxLevel) { maxLevel_ = maxLevel; }
@@ -339,8 +354,13 @@ namespace
339
354
{
340
355
}
341
356
342
- virtual Size getWinSize () const { return winSize_; }
343
- virtual void setWinSize (Size winSize) { winSize_ = winSize; }
357
+ virtual Size getWinSize () const { return cv::Size (winSize_[0 ], winSize_[1 ]); }
358
+ virtual void setWinSize (Size winSize) {
359
+ winSize_[0 ] = winSize.width ;
360
+ winSize_[1 ] = winSize.height ;
361
+ halfWinSize_[0 ] = (winSize.width - 1 ) / 2 ;
362
+ halfWinSize_[1 ] = (winSize.height -1 ) / 2 ;
363
+ }
344
364
345
365
virtual int getMaxLevel () const { return maxLevel_; }
346
366
virtual void setMaxLevel (int maxLevel) { maxLevel_ = maxLevel; }
0 commit comments