Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/src/lkpyramid.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
#include "precomp.hpp"
43
#include <float.h>
44
#include <stdio.h>
45
#include "lkpyramid.hpp"
46
#include "opencl_kernels_video.hpp"
47
#include "opencv2/core/hal/intrin.hpp"
48
#ifdef HAVE_OPENCV_CALIB3D
49
#include "opencv2/calib3d.hpp"
50
#endif
51
52
#include "opencv2/core/openvx/ovx_defs.hpp"
53
54
#define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
55
56
namespace
57
{
58
static void calcSharrDeriv(const cv::Mat& src, cv::Mat& dst)
59
{
60
using namespace cv;
61
using cv::detail::deriv_type;
62
int rows = src.rows, cols = src.cols, cn = src.channels(), colsn = cols*cn, depth = src.depth();
63
CV_Assert(depth == CV_8U);
64
dst.create(rows, cols, CV_MAKETYPE(DataType<deriv_type>::depth, cn*2));
65
66
int x, y, delta = (int)alignSize((cols + 2)*cn, 16);
67
AutoBuffer<deriv_type> _tempBuf(delta*2 + 64);
68
deriv_type *trow0 = alignPtr(_tempBuf.data() + cn, 16), *trow1 = alignPtr(trow0 + delta, 16);
69
70
#if CV_SIMD128
71
v_int16x8 c3 = v_setall_s16(3), c10 = v_setall_s16(10);
72
bool haveSIMD = checkHardwareSupport(CV_CPU_SSE2) || checkHardwareSupport(CV_CPU_NEON);
73
#endif
74
75
for( y = 0; y < rows; y++ )
76
{
77
const uchar* srow0 = src.ptr<uchar>(y > 0 ? y-1 : rows > 1 ? 1 : 0);
78
const uchar* srow1 = src.ptr<uchar>(y);
79
const uchar* srow2 = src.ptr<uchar>(y < rows-1 ? y+1 : rows > 1 ? rows-2 : 0);
80
deriv_type* drow = dst.ptr<deriv_type>(y);
81
82
// do vertical convolution
83
x = 0;
84
#if CV_SIMD128
85
if(haveSIMD)
86
{
87
for( ; x <= colsn - 8; x += 8 )
88
{
89
v_int16x8 s0 = v_reinterpret_as_s16(v_load_expand(srow0 + x));
90
v_int16x8 s1 = v_reinterpret_as_s16(v_load_expand(srow1 + x));
91
v_int16x8 s2 = v_reinterpret_as_s16(v_load_expand(srow2 + x));
92
93
v_int16x8 t1 = s2 - s0;
94
v_int16x8 t0 = v_mul_wrap(s0 + s2, c3) + v_mul_wrap(s1, c10);
95
96
v_store(trow0 + x, t0);
97
v_store(trow1 + x, t1);
98
}
99
}
100
#endif
101
102
for( ; x < colsn; x++ )
103
{
104
int t0 = (srow0[x] + srow2[x])*3 + srow1[x]*10;
105
int t1 = srow2[x] - srow0[x];
106
trow0[x] = (deriv_type)t0;
107
trow1[x] = (deriv_type)t1;
108
}
109
110
// make border
111
int x0 = (cols > 1 ? 1 : 0)*cn, x1 = (cols > 1 ? cols-2 : 0)*cn;
112
for( int k = 0; k < cn; k++ )
113
{
114
trow0[-cn + k] = trow0[x0 + k]; trow0[colsn + k] = trow0[x1 + k];
115
trow1[-cn + k] = trow1[x0 + k]; trow1[colsn + k] = trow1[x1 + k];
116
}
117
118
// do horizontal convolution, interleave the results and store them to dst
119
x = 0;
120
#if CV_SIMD128
121
if(haveSIMD)
122
{
123
for( ; x <= colsn - 8; x += 8 )
124
{
125
v_int16x8 s0 = v_load(trow0 + x - cn);
126
v_int16x8 s1 = v_load(trow0 + x + cn);
127
v_int16x8 s2 = v_load(trow1 + x - cn);
128
v_int16x8 s3 = v_load(trow1 + x);
129
v_int16x8 s4 = v_load(trow1 + x + cn);
130
131
v_int16x8 t0 = s1 - s0;
132
v_int16x8 t1 = v_mul_wrap(s2 + s4, c3) + v_mul_wrap(s3, c10);
133
134
v_store_interleave((drow + x*2), t0, t1);
135
}
136
}
137
#endif
138
for( ; x < colsn; x++ )
139
{
140
deriv_type t0 = (deriv_type)(trow0[x+cn] - trow0[x-cn]);
141
deriv_type t1 = (deriv_type)((trow1[x+cn] + trow1[x-cn])*3 + trow1[x]*10);
142
drow[x*2] = t0; drow[x*2+1] = t1;
143
}
144
}
145
}
146
147
}//namespace
148
149
cv::detail::LKTrackerInvoker::LKTrackerInvoker(
150
const Mat& _prevImg, const Mat& _prevDeriv, const Mat& _nextImg,
151
const Point2f* _prevPts, Point2f* _nextPts,
152
uchar* _status, float* _err,
153
Size _winSize, TermCriteria _criteria,
154
int _level, int _maxLevel, int _flags, float _minEigThreshold )
155
{
156
prevImg = &_prevImg;
157
prevDeriv = &_prevDeriv;
158
nextImg = &_nextImg;
159
prevPts = _prevPts;
160
nextPts = _nextPts;
161
status = _status;
162
err = _err;
163
winSize = _winSize;
164
criteria = _criteria;
165
level = _level;
166
maxLevel = _maxLevel;
167
flags = _flags;
168
minEigThreshold = _minEigThreshold;
169
}
170
171
#if defined __arm__ && !CV_NEON
172
typedef int64 acctype;
173
typedef int itemtype;
174
#else
175
typedef float acctype;
176
typedef float itemtype;
177
#endif
178
179
void cv::detail::LKTrackerInvoker::operator()(const Range& range) const
180
{
181
CV_INSTRUMENT_REGION();
182
183
Point2f halfWin((winSize.width-1)*0.5f, (winSize.height-1)*0.5f);
184
const Mat& I = *prevImg;
185
const Mat& J = *nextImg;
186
const Mat& derivI = *prevDeriv;
187
188
int j, cn = I.channels(), cn2 = cn*2;
189
cv::AutoBuffer<deriv_type> _buf(winSize.area()*(cn + cn2));
190
int derivDepth = DataType<deriv_type>::depth;
191
192
Mat IWinBuf(winSize, CV_MAKETYPE(derivDepth, cn), _buf.data());
193
Mat derivIWinBuf(winSize, CV_MAKETYPE(derivDepth, cn2), _buf.data() + winSize.area()*cn);
194
195
for( int ptidx = range.start; ptidx < range.end; ptidx++ )
196
{
197
Point2f prevPt = prevPts[ptidx]*(float)(1./(1 << level));
198
Point2f nextPt;
199
if( level == maxLevel )
200
{
201
if( flags & OPTFLOW_USE_INITIAL_FLOW )
202
nextPt = nextPts[ptidx]*(float)(1./(1 << level));
203
else
204
nextPt = prevPt;
205
}
206
else
207
nextPt = nextPts[ptidx]*2.f;
208
nextPts[ptidx] = nextPt;
209
210
Point2i iprevPt, inextPt;
211
prevPt -= halfWin;
212
iprevPt.x = cvFloor(prevPt.x);
213
iprevPt.y = cvFloor(prevPt.y);
214
215
if( iprevPt.x < -winSize.width || iprevPt.x >= derivI.cols ||
216
iprevPt.y < -winSize.height || iprevPt.y >= derivI.rows )
217
{
218
if( level == 0 )
219
{
220
if( status )
221
status[ptidx] = false;
222
if( err )
223
err[ptidx] = 0;
224
}
225
continue;
226
}
227
228
float a = prevPt.x - iprevPt.x;
229
float b = prevPt.y - iprevPt.y;
230
const int W_BITS = 14, W_BITS1 = 14;
231
const float FLT_SCALE = 1.f/(1 << 20);
232
int iw00 = cvRound((1.f - a)*(1.f - b)*(1 << W_BITS));
233
int iw01 = cvRound(a*(1.f - b)*(1 << W_BITS));
234
int iw10 = cvRound((1.f - a)*b*(1 << W_BITS));
235
int iw11 = (1 << W_BITS) - iw00 - iw01 - iw10;
236
237
int dstep = (int)(derivI.step/derivI.elemSize1());
238
int stepI = (int)(I.step/I.elemSize1());
239
int stepJ = (int)(J.step/J.elemSize1());
240
acctype iA11 = 0, iA12 = 0, iA22 = 0;
241
float A11, A12, A22;
242
243
#if CV_SSE2
244
__m128i qw0 = _mm_set1_epi32(iw00 + (iw01 << 16));
245
__m128i qw1 = _mm_set1_epi32(iw10 + (iw11 << 16));
246
__m128i z = _mm_setzero_si128();
247
__m128i qdelta_d = _mm_set1_epi32(1 << (W_BITS1-1));
248
__m128i qdelta = _mm_set1_epi32(1 << (W_BITS1-5-1));
249
__m128 qA11 = _mm_setzero_ps(), qA12 = _mm_setzero_ps(), qA22 = _mm_setzero_ps();
250
#endif
251
252
#if CV_NEON
253
254
float CV_DECL_ALIGNED(16) nA11[] = { 0, 0, 0, 0 }, nA12[] = { 0, 0, 0, 0 }, nA22[] = { 0, 0, 0, 0 };
255
const int shifter1 = -(W_BITS - 5); //negative so it shifts right
256
const int shifter2 = -(W_BITS);
257
258
const int16x4_t d26 = vdup_n_s16((int16_t)iw00);
259
const int16x4_t d27 = vdup_n_s16((int16_t)iw01);
260
const int16x4_t d28 = vdup_n_s16((int16_t)iw10);
261
const int16x4_t d29 = vdup_n_s16((int16_t)iw11);
262
const int32x4_t q11 = vdupq_n_s32((int32_t)shifter1);
263
const int32x4_t q12 = vdupq_n_s32((int32_t)shifter2);
264
265
#endif
266
267
// extract the patch from the first image, compute covariation matrix of derivatives
268
int x, y;
269
for( y = 0; y < winSize.height; y++ )
270
{
271
const uchar* src = I.ptr() + (y + iprevPt.y)*stepI + iprevPt.x*cn;
272
const deriv_type* dsrc = derivI.ptr<deriv_type>() + (y + iprevPt.y)*dstep + iprevPt.x*cn2;
273
274
deriv_type* Iptr = IWinBuf.ptr<deriv_type>(y);
275
deriv_type* dIptr = derivIWinBuf.ptr<deriv_type>(y);
276
277
x = 0;
278
279
#if CV_SSE2
280
for( ; x <= winSize.width*cn - 4; x += 4, dsrc += 4*2, dIptr += 4*2 )
281
{
282
__m128i v00, v01, v10, v11, t0, t1;
283
284
v00 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int*)(src + x)), z);
285
v01 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int*)(src + x + cn)), z);
286
v10 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int*)(src + x + stepI)), z);
287
v11 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int*)(src + x + stepI + cn)), z);
288
289
t0 = _mm_add_epi32(_mm_madd_epi16(_mm_unpacklo_epi16(v00, v01), qw0),
290
_mm_madd_epi16(_mm_unpacklo_epi16(v10, v11), qw1));
291
t0 = _mm_srai_epi32(_mm_add_epi32(t0, qdelta), W_BITS1-5);
292
_mm_storel_epi64((__m128i*)(Iptr + x), _mm_packs_epi32(t0,t0));
293
294
v00 = _mm_loadu_si128((const __m128i*)(dsrc));
295
v01 = _mm_loadu_si128((const __m128i*)(dsrc + cn2));
296
v10 = _mm_loadu_si128((const __m128i*)(dsrc + dstep));
297
v11 = _mm_loadu_si128((const __m128i*)(dsrc + dstep + cn2));
298
299
t0 = _mm_add_epi32(_mm_madd_epi16(_mm_unpacklo_epi16(v00, v01), qw0),
300
_mm_madd_epi16(_mm_unpacklo_epi16(v10, v11), qw1));
301
t1 = _mm_add_epi32(_mm_madd_epi16(_mm_unpackhi_epi16(v00, v01), qw0),
302
_mm_madd_epi16(_mm_unpackhi_epi16(v10, v11), qw1));
303
t0 = _mm_srai_epi32(_mm_add_epi32(t0, qdelta_d), W_BITS1);
304
t1 = _mm_srai_epi32(_mm_add_epi32(t1, qdelta_d), W_BITS1);
305
v00 = _mm_packs_epi32(t0, t1); // Ix0 Iy0 Ix1 Iy1 ...
306
307
_mm_storeu_si128((__m128i*)dIptr, v00);
308
t0 = _mm_srai_epi32(v00, 16); // Iy0 Iy1 Iy2 Iy3
309
t1 = _mm_srai_epi32(_mm_slli_epi32(v00, 16), 16); // Ix0 Ix1 Ix2 Ix3
310
311
__m128 fy = _mm_cvtepi32_ps(t0);
312
__m128 fx = _mm_cvtepi32_ps(t1);
313
314
qA22 = _mm_add_ps(qA22, _mm_mul_ps(fy, fy));
315
qA12 = _mm_add_ps(qA12, _mm_mul_ps(fx, fy));
316
qA11 = _mm_add_ps(qA11, _mm_mul_ps(fx, fx));
317
}
318
#endif
319
320
#if CV_NEON
321
for( ; x <= winSize.width*cn - 4; x += 4, dsrc += 4*2, dIptr += 4*2 )
322
{
323
324
uint8x8_t d0 = vld1_u8(&src[x]);
325
uint8x8_t d2 = vld1_u8(&src[x+cn]);
326
uint16x8_t q0 = vmovl_u8(d0);
327
uint16x8_t q1 = vmovl_u8(d2);
328
329
int32x4_t q5 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q0)), d26);
330
int32x4_t q6 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q1)), d27);
331
332
uint8x8_t d4 = vld1_u8(&src[x + stepI]);
333
uint8x8_t d6 = vld1_u8(&src[x + stepI + cn]);
334
uint16x8_t q2 = vmovl_u8(d4);
335
uint16x8_t q3 = vmovl_u8(d6);
336
337
int32x4_t q7 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q2)), d28);
338
int32x4_t q8 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q3)), d29);
339
340
q5 = vaddq_s32(q5, q6);
341
q7 = vaddq_s32(q7, q8);
342
q5 = vaddq_s32(q5, q7);
343
344
int16x4x2_t d0d1 = vld2_s16(dsrc);
345
int16x4x2_t d2d3 = vld2_s16(&dsrc[cn2]);
346
347
q5 = vqrshlq_s32(q5, q11);
348
349
int32x4_t q4 = vmull_s16(d0d1.val[0], d26);
350
q6 = vmull_s16(d0d1.val[1], d26);
351
352
int16x4_t nd0 = vmovn_s32(q5);
353
354
q7 = vmull_s16(d2d3.val[0], d27);
355
q8 = vmull_s16(d2d3.val[1], d27);
356
357
vst1_s16(&Iptr[x], nd0);
358
359
int16x4x2_t d4d5 = vld2_s16(&dsrc[dstep]);
360
int16x4x2_t d6d7 = vld2_s16(&dsrc[dstep+cn2]);
361
362
q4 = vaddq_s32(q4, q7);
363
q6 = vaddq_s32(q6, q8);
364
365
q7 = vmull_s16(d4d5.val[0], d28);
366
int32x4_t q14 = vmull_s16(d4d5.val[1], d28);
367
q8 = vmull_s16(d6d7.val[0], d29);
368
int32x4_t q15 = vmull_s16(d6d7.val[1], d29);
369
370
q7 = vaddq_s32(q7, q8);
371
q14 = vaddq_s32(q14, q15);
372
373
q4 = vaddq_s32(q4, q7);
374
q6 = vaddq_s32(q6, q14);
375
376
float32x4_t nq0 = vld1q_f32(nA11);
377
float32x4_t nq1 = vld1q_f32(nA12);
378
float32x4_t nq2 = vld1q_f32(nA22);
379
380
q4 = vqrshlq_s32(q4, q12);
381
q6 = vqrshlq_s32(q6, q12);
382
383
q7 = vmulq_s32(q4, q4);
384
q8 = vmulq_s32(q4, q6);
385
q15 = vmulq_s32(q6, q6);
386
387
nq0 = vaddq_f32(nq0, vcvtq_f32_s32(q7));
388
nq1 = vaddq_f32(nq1, vcvtq_f32_s32(q8));
389
nq2 = vaddq_f32(nq2, vcvtq_f32_s32(q15));
390
391
vst1q_f32(nA11, nq0);
392
vst1q_f32(nA12, nq1);
393
vst1q_f32(nA22, nq2);
394
395
int16x4_t d8 = vmovn_s32(q4);
396
int16x4_t d12 = vmovn_s32(q6);
397
398
int16x4x2_t d8d12;
399
d8d12.val[0] = d8; d8d12.val[1] = d12;
400
vst2_s16(dIptr, d8d12);
401
}
402
#endif
403
404
for( ; x < winSize.width*cn; x++, dsrc += 2, dIptr += 2 )
405
{
406
int ival = CV_DESCALE(src[x]*iw00 + src[x+cn]*iw01 +
407
src[x+stepI]*iw10 + src[x+stepI+cn]*iw11, W_BITS1-5);
408
int ixval = CV_DESCALE(dsrc[0]*iw00 + dsrc[cn2]*iw01 +
409
dsrc[dstep]*iw10 + dsrc[dstep+cn2]*iw11, W_BITS1);
410
int iyval = CV_DESCALE(dsrc[1]*iw00 + dsrc[cn2+1]*iw01 + dsrc[dstep+1]*iw10 +
411
dsrc[dstep+cn2+1]*iw11, W_BITS1);
412
413
Iptr[x] = (short)ival;
414
dIptr[0] = (short)ixval;
415
dIptr[1] = (short)iyval;
416
417
iA11 += (itemtype)(ixval*ixval);
418
iA12 += (itemtype)(ixval*iyval);
419
iA22 += (itemtype)(iyval*iyval);
420
}
421
}
422
423
#if CV_SSE2
424
float CV_DECL_ALIGNED(16) A11buf[4], A12buf[4], A22buf[4];
425
_mm_store_ps(A11buf, qA11);
426
_mm_store_ps(A12buf, qA12);
427
_mm_store_ps(A22buf, qA22);
428
iA11 += A11buf[0] + A11buf[1] + A11buf[2] + A11buf[3];
429
iA12 += A12buf[0] + A12buf[1] + A12buf[2] + A12buf[3];
430
iA22 += A22buf[0] + A22buf[1] + A22buf[2] + A22buf[3];
431
#endif
432
433
#if CV_NEON
434
iA11 += nA11[0] + nA11[1] + nA11[2] + nA11[3];
435
iA12 += nA12[0] + nA12[1] + nA12[2] + nA12[3];
436
iA22 += nA22[0] + nA22[1] + nA22[2] + nA22[3];
437
#endif
438
439
A11 = iA11*FLT_SCALE;
440
A12 = iA12*FLT_SCALE;
441
A22 = iA22*FLT_SCALE;
442
443
float D = A11*A22 - A12*A12;
444
float minEig = (A22 + A11 - std::sqrt((A11-A22)*(A11-A22) +
445
4.f*A12*A12))/(2*winSize.width*winSize.height);
446
447
if( err && (flags & OPTFLOW_LK_GET_MIN_EIGENVALS) != 0 )
448
err[ptidx] = (float)minEig;
449
450
if( minEig < minEigThreshold || D < FLT_EPSILON )
451
{
452
if( level == 0 && status )
453
status[ptidx] = false;
454
continue;
455
}
456
457
D = 1.f/D;
458
459
nextPt -= halfWin;
460
Point2f prevDelta;
461
462
for( j = 0; j < criteria.maxCount; j++ )
463
{
464
inextPt.x = cvFloor(nextPt.x);
465
inextPt.y = cvFloor(nextPt.y);
466
467
if( inextPt.x < -winSize.width || inextPt.x >= J.cols ||
468
inextPt.y < -winSize.height || inextPt.y >= J.rows )
469
{
470
if( level == 0 && status )
471
status[ptidx] = false;
472
break;
473
}
474
475
a = nextPt.x - inextPt.x;
476
b = nextPt.y - inextPt.y;
477
iw00 = cvRound((1.f - a)*(1.f - b)*(1 << W_BITS));
478
iw01 = cvRound(a*(1.f - b)*(1 << W_BITS));
479
iw10 = cvRound((1.f - a)*b*(1 << W_BITS));
480
iw11 = (1 << W_BITS) - iw00 - iw01 - iw10;
481
acctype ib1 = 0, ib2 = 0;
482
float b1, b2;
483
#if CV_SSE2
484
qw0 = _mm_set1_epi32(iw00 + (iw01 << 16));
485
qw1 = _mm_set1_epi32(iw10 + (iw11 << 16));
486
__m128 qb0 = _mm_setzero_ps(), qb1 = _mm_setzero_ps();
487
#endif
488
489
#if CV_NEON
490
float CV_DECL_ALIGNED(16) nB1[] = { 0,0,0,0 }, nB2[] = { 0,0,0,0 };
491
492
const int16x4_t d26_2 = vdup_n_s16((int16_t)iw00);
493
const int16x4_t d27_2 = vdup_n_s16((int16_t)iw01);
494
const int16x4_t d28_2 = vdup_n_s16((int16_t)iw10);
495
const int16x4_t d29_2 = vdup_n_s16((int16_t)iw11);
496
497
#endif
498
499
for( y = 0; y < winSize.height; y++ )
500
{
501
const uchar* Jptr = J.ptr() + (y + inextPt.y)*stepJ + inextPt.x*cn;
502
const deriv_type* Iptr = IWinBuf.ptr<deriv_type>(y);
503
const deriv_type* dIptr = derivIWinBuf.ptr<deriv_type>(y);
504
505
x = 0;
506
507
#if CV_SSE2
508
for( ; x <= winSize.width*cn - 8; x += 8, dIptr += 8*2 )
509
{
510
__m128i diff0 = _mm_loadu_si128((const __m128i*)(Iptr + x)), diff1;
511
__m128i v00 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(Jptr + x)), z);
512
__m128i v01 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(Jptr + x + cn)), z);
513
__m128i v10 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(Jptr + x + stepJ)), z);
514
__m128i v11 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(Jptr + x + stepJ + cn)), z);
515
516
__m128i t0 = _mm_add_epi32(_mm_madd_epi16(_mm_unpacklo_epi16(v00, v01), qw0),
517
_mm_madd_epi16(_mm_unpacklo_epi16(v10, v11), qw1));
518
__m128i t1 = _mm_add_epi32(_mm_madd_epi16(_mm_unpackhi_epi16(v00, v01), qw0),
519
_mm_madd_epi16(_mm_unpackhi_epi16(v10, v11), qw1));
520
t0 = _mm_srai_epi32(_mm_add_epi32(t0, qdelta), W_BITS1-5);
521
t1 = _mm_srai_epi32(_mm_add_epi32(t1, qdelta), W_BITS1-5);
522
diff0 = _mm_subs_epi16(_mm_packs_epi32(t0, t1), diff0);
523
diff1 = _mm_unpackhi_epi16(diff0, diff0);
524
diff0 = _mm_unpacklo_epi16(diff0, diff0); // It0 It0 It1 It1 ...
525
v00 = _mm_loadu_si128((const __m128i*)(dIptr)); // Ix0 Iy0 Ix1 Iy1 ...
526
v01 = _mm_loadu_si128((const __m128i*)(dIptr + 8));
527
v10 = _mm_unpacklo_epi16(v00, v01);
528
v11 = _mm_unpackhi_epi16(v00, v01);
529
v00 = _mm_unpacklo_epi16(diff0, diff1);
530
v01 = _mm_unpackhi_epi16(diff0, diff1);
531
v00 = _mm_madd_epi16(v00, v10);
532
v11 = _mm_madd_epi16(v01, v11);
533
qb0 = _mm_add_ps(qb0, _mm_cvtepi32_ps(v00));
534
qb1 = _mm_add_ps(qb1, _mm_cvtepi32_ps(v11));
535
}
536
#endif
537
538
#if CV_NEON
539
for( ; x <= winSize.width*cn - 8; x += 8, dIptr += 8*2 )
540
{
541
542
uint8x8_t d0 = vld1_u8(&Jptr[x]);
543
uint8x8_t d2 = vld1_u8(&Jptr[x+cn]);
544
uint8x8_t d4 = vld1_u8(&Jptr[x+stepJ]);
545
uint8x8_t d6 = vld1_u8(&Jptr[x+stepJ+cn]);
546
547
uint16x8_t q0 = vmovl_u8(d0);
548
uint16x8_t q1 = vmovl_u8(d2);
549
uint16x8_t q2 = vmovl_u8(d4);
550
uint16x8_t q3 = vmovl_u8(d6);
551
552
int32x4_t nq4 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q0)), d26_2);
553
int32x4_t nq5 = vmull_s16(vget_high_s16(vreinterpretq_s16_u16(q0)), d26_2);
554
555
int32x4_t nq6 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q1)), d27_2);
556
int32x4_t nq7 = vmull_s16(vget_high_s16(vreinterpretq_s16_u16(q1)), d27_2);
557
558
int32x4_t nq8 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q2)), d28_2);
559
int32x4_t nq9 = vmull_s16(vget_high_s16(vreinterpretq_s16_u16(q2)), d28_2);
560
561
int32x4_t nq10 = vmull_s16(vget_low_s16(vreinterpretq_s16_u16(q3)), d29_2);
562
int32x4_t nq11 = vmull_s16(vget_high_s16(vreinterpretq_s16_u16(q3)), d29_2);
563
564
nq4 = vaddq_s32(nq4, nq6);
565
nq5 = vaddq_s32(nq5, nq7);
566
nq8 = vaddq_s32(nq8, nq10);
567
nq9 = vaddq_s32(nq9, nq11);
568
569
int16x8_t q6 = vld1q_s16(&Iptr[x]);
570
571
nq4 = vaddq_s32(nq4, nq8);
572
nq5 = vaddq_s32(nq5, nq9);
573
574
nq8 = vmovl_s16(vget_high_s16(q6));
575
nq6 = vmovl_s16(vget_low_s16(q6));
576
577
nq4 = vqrshlq_s32(nq4, q11);
578
nq5 = vqrshlq_s32(nq5, q11);
579
580
int16x8x2_t q0q1 = vld2q_s16(dIptr);
581
float32x4_t nB1v = vld1q_f32(nB1);
582
float32x4_t nB2v = vld1q_f32(nB2);
583
584
nq4 = vsubq_s32(nq4, nq6);
585
nq5 = vsubq_s32(nq5, nq8);
586
587
int32x4_t nq2 = vmovl_s16(vget_low_s16(q0q1.val[0]));
588
int32x4_t nq3 = vmovl_s16(vget_high_s16(q0q1.val[0]));
589
590
nq7 = vmovl_s16(vget_low_s16(q0q1.val[1]));
591
nq8 = vmovl_s16(vget_high_s16(q0q1.val[1]));
592
593
nq9 = vmulq_s32(nq4, nq2);
594
nq10 = vmulq_s32(nq5, nq3);
595
596
nq4 = vmulq_s32(nq4, nq7);
597
nq5 = vmulq_s32(nq5, nq8);
598
599
nq9 = vaddq_s32(nq9, nq10);
600
nq4 = vaddq_s32(nq4, nq5);
601
602
nB1v = vaddq_f32(nB1v, vcvtq_f32_s32(nq9));
603
nB2v = vaddq_f32(nB2v, vcvtq_f32_s32(nq4));
604
605
vst1q_f32(nB1, nB1v);
606
vst1q_f32(nB2, nB2v);
607
}
608
#endif
609
610
for( ; x < winSize.width*cn; x++, dIptr += 2 )
611
{
612
int diff = CV_DESCALE(Jptr[x]*iw00 + Jptr[x+cn]*iw01 +
613
Jptr[x+stepJ]*iw10 + Jptr[x+stepJ+cn]*iw11,
614
W_BITS1-5) - Iptr[x];
615
ib1 += (itemtype)(diff*dIptr[0]);
616
ib2 += (itemtype)(diff*dIptr[1]);
617
}
618
}
619
620
#if CV_SSE2
621
float CV_DECL_ALIGNED(16) bbuf[4];
622
_mm_store_ps(bbuf, _mm_add_ps(qb0, qb1));
623
ib1 += bbuf[0] + bbuf[2];
624
ib2 += bbuf[1] + bbuf[3];
625
#endif
626
627
#if CV_NEON
628
629
ib1 += (float)(nB1[0] + nB1[1] + nB1[2] + nB1[3]);
630
ib2 += (float)(nB2[0] + nB2[1] + nB2[2] + nB2[3]);
631
#endif
632
633
b1 = ib1*FLT_SCALE;
634
b2 = ib2*FLT_SCALE;
635
636
Point2f delta( (float)((A12*b2 - A22*b1) * D),
637
(float)((A12*b1 - A11*b2) * D));
638
//delta = -delta;
639
640
nextPt += delta;
641
nextPts[ptidx] = nextPt + halfWin;
642
643
if( delta.ddot(delta) <= criteria.epsilon )
644
break;
645
646
if( j > 0 && std::abs(delta.x + prevDelta.x) < 0.01 &&
647
std::abs(delta.y + prevDelta.y) < 0.01 )
648
{
649
nextPts[ptidx] -= delta*0.5f;
650
break;
651
}
652
prevDelta = delta;
653
}
654
655
CV_Assert(status != NULL);
656
if( status[ptidx] && err && level == 0 && (flags & OPTFLOW_LK_GET_MIN_EIGENVALS) == 0 )
657
{
658
Point2f nextPoint = nextPts[ptidx] - halfWin;
659
Point inextPoint;
660
661
inextPoint.x = cvFloor(nextPoint.x);
662
inextPoint.y = cvFloor(nextPoint.y);
663
664
if( inextPoint.x < -winSize.width || inextPoint.x >= J.cols ||
665
inextPoint.y < -winSize.height || inextPoint.y >= J.rows )
666
{
667
if( status )
668
status[ptidx] = false;
669
continue;
670
}
671
672
float aa = nextPoint.x - inextPoint.x;
673
float bb = nextPoint.y - inextPoint.y;
674
iw00 = cvRound((1.f - aa)*(1.f - bb)*(1 << W_BITS));
675
iw01 = cvRound(aa*(1.f - bb)*(1 << W_BITS));
676
iw10 = cvRound((1.f - aa)*bb*(1 << W_BITS));
677
iw11 = (1 << W_BITS) - iw00 - iw01 - iw10;
678
float errval = 0.f;
679
680
for( y = 0; y < winSize.height; y++ )
681
{
682
const uchar* Jptr = J.ptr() + (y + inextPoint.y)*stepJ + inextPoint.x*cn;
683
const deriv_type* Iptr = IWinBuf.ptr<deriv_type>(y);
684
685
for( x = 0; x < winSize.width*cn; x++ )
686
{
687
int diff = CV_DESCALE(Jptr[x]*iw00 + Jptr[x+cn]*iw01 +
688
Jptr[x+stepJ]*iw10 + Jptr[x+stepJ+cn]*iw11,
689
W_BITS1-5) - Iptr[x];
690
errval += std::abs((float)diff);
691
}
692
}
693
err[ptidx] = errval * 1.f/(32*winSize.width*cn*winSize.height);
694
}
695
}
696
}
697
698
int cv::buildOpticalFlowPyramid(InputArray _img, OutputArrayOfArrays pyramid, Size winSize, int maxLevel, bool withDerivatives,
699
int pyrBorder, int derivBorder, bool tryReuseInputImage)
700
{
701
CV_INSTRUMENT_REGION();
702
703
Mat img = _img.getMat();
704
CV_Assert(img.depth() == CV_8U && winSize.width > 2 && winSize.height > 2 );
705
int pyrstep = withDerivatives ? 2 : 1;
706
707
pyramid.create(1, (maxLevel + 1) * pyrstep, 0 /*type*/, -1, true);
708
709
int derivType = CV_MAKETYPE(DataType<cv::detail::deriv_type>::depth, img.channels() * 2);
710
711
//level 0
712
bool lvl0IsSet = false;
713
if(tryReuseInputImage && img.isSubmatrix() && (pyrBorder & BORDER_ISOLATED) == 0)
714
{
715
Size wholeSize;
716
Point ofs;
717
img.locateROI(wholeSize, ofs);
718
if (ofs.x >= winSize.width && ofs.y >= winSize.height
719
&& ofs.x + img.cols + winSize.width <= wholeSize.width
720
&& ofs.y + img.rows + winSize.height <= wholeSize.height)
721
{
722
pyramid.getMatRef(0) = img;
723
lvl0IsSet = true;
724
}
725
}
726
727
if(!lvl0IsSet)
728
{
729
Mat& temp = pyramid.getMatRef(0);
730
731
if(!temp.empty())
732
temp.adjustROI(winSize.height, winSize.height, winSize.width, winSize.width);
733
if(temp.type() != img.type() || temp.cols != winSize.width*2 + img.cols || temp.rows != winSize.height * 2 + img.rows)
734
temp.create(img.rows + winSize.height*2, img.cols + winSize.width*2, img.type());
735
736
if(pyrBorder == BORDER_TRANSPARENT)
737
img.copyTo(temp(Rect(winSize.width, winSize.height, img.cols, img.rows)));
738
else
739
copyMakeBorder(img, temp, winSize.height, winSize.height, winSize.width, winSize.width, pyrBorder);
740
temp.adjustROI(-winSize.height, -winSize.height, -winSize.width, -winSize.width);
741
}
742
743
Size sz = img.size();
744
Mat prevLevel = pyramid.getMatRef(0);
745
Mat thisLevel = prevLevel;
746
747
for(int level = 0; level <= maxLevel; ++level)
748
{
749
if (level != 0)
750
{
751
Mat& temp = pyramid.getMatRef(level * pyrstep);
752
753
if(!temp.empty())
754
temp.adjustROI(winSize.height, winSize.height, winSize.width, winSize.width);
755
if(temp.type() != img.type() || temp.cols != winSize.width*2 + sz.width || temp.rows != winSize.height * 2 + sz.height)
756
temp.create(sz.height + winSize.height*2, sz.width + winSize.width*2, img.type());
757
758
thisLevel = temp(Rect(winSize.width, winSize.height, sz.width, sz.height));
759
pyrDown(prevLevel, thisLevel, sz);
760
761
if(pyrBorder != BORDER_TRANSPARENT)
762
copyMakeBorder(thisLevel, temp, winSize.height, winSize.height, winSize.width, winSize.width, pyrBorder|BORDER_ISOLATED);
763
temp.adjustROI(-winSize.height, -winSize.height, -winSize.width, -winSize.width);
764
}
765
766
if(withDerivatives)
767
{
768
Mat& deriv = pyramid.getMatRef(level * pyrstep + 1);
769
770
if(!deriv.empty())
771
deriv.adjustROI(winSize.height, winSize.height, winSize.width, winSize.width);
772
if(deriv.type() != derivType || deriv.cols != winSize.width*2 + sz.width || deriv.rows != winSize.height * 2 + sz.height)
773
deriv.create(sz.height + winSize.height*2, sz.width + winSize.width*2, derivType);
774
775
Mat derivI = deriv(Rect(winSize.width, winSize.height, sz.width, sz.height));
776
calcSharrDeriv(thisLevel, derivI);
777
778
if(derivBorder != BORDER_TRANSPARENT)
779
copyMakeBorder(derivI, deriv, winSize.height, winSize.height, winSize.width, winSize.width, derivBorder|BORDER_ISOLATED);
780
deriv.adjustROI(-winSize.height, -winSize.height, -winSize.width, -winSize.width);
781
}
782
783
sz = Size((sz.width+1)/2, (sz.height+1)/2);
784
if( sz.width <= winSize.width || sz.height <= winSize.height )
785
{
786
pyramid.create(1, (level + 1) * pyrstep, 0 /*type*/, -1, true);//check this
787
return level;
788
}
789
790
prevLevel = thisLevel;
791
}
792
793
return maxLevel;
794
}
795
796
namespace cv
797
{
798
namespace
799
{
800
class SparsePyrLKOpticalFlowImpl : public SparsePyrLKOpticalFlow
801
{
802
struct dim3
803
{
804
unsigned int x, y, z;
805
dim3() : x(0), y(0), z(0) { }
806
};
807
public:
808
SparsePyrLKOpticalFlowImpl(Size winSize_ = Size(21,21),
809
int maxLevel_ = 3,
810
TermCriteria criteria_ = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
811
int flags_ = 0,
812
double minEigThreshold_ = 1e-4) :
813
winSize(winSize_), maxLevel(maxLevel_), criteria(criteria_), flags(flags_), minEigThreshold(minEigThreshold_)
814
#ifdef HAVE_OPENCL
815
, iters(criteria_.maxCount), derivLambda(criteria_.epsilon), useInitialFlow(0 != (flags_ & OPTFLOW_LK_GET_MIN_EIGENVALS))
816
#endif
817
{
818
}
819
820
virtual Size getWinSize() const CV_OVERRIDE { return winSize;}
821
virtual void setWinSize(Size winSize_) CV_OVERRIDE { winSize = winSize_;}
822
823
virtual int getMaxLevel() const CV_OVERRIDE { return maxLevel;}
824
virtual void setMaxLevel(int maxLevel_) CV_OVERRIDE { maxLevel = maxLevel_;}
825
826
virtual TermCriteria getTermCriteria() const CV_OVERRIDE { return criteria;}
827
virtual void setTermCriteria(TermCriteria& crit_) CV_OVERRIDE { criteria=crit_;}
828
829
virtual int getFlags() const CV_OVERRIDE { return flags; }
830
virtual void setFlags(int flags_) CV_OVERRIDE { flags=flags_;}
831
832
virtual double getMinEigThreshold() const CV_OVERRIDE { return minEigThreshold;}
833
virtual void setMinEigThreshold(double minEigThreshold_) CV_OVERRIDE { minEigThreshold=minEigThreshold_;}
834
835
virtual void calc(InputArray prevImg, InputArray nextImg,
836
InputArray prevPts, InputOutputArray nextPts,
837
OutputArray status,
838
OutputArray err = cv::noArray()) CV_OVERRIDE;
839
840
private:
841
#ifdef HAVE_OPENCL
842
bool checkParam()
843
{
844
iters = std::min(std::max(iters, 0), 100);
845
846
derivLambda = std::min(std::max(derivLambda, 0.0), 1.0);
847
if (derivLambda < 0)
848
return false;
849
if (maxLevel < 0 || winSize.width <= 2 || winSize.height <= 2)
850
return false;
851
if (winSize.width < 8 || winSize.height < 8 ||
852
winSize.width > 24 || winSize.height > 24)
853
return false;
854
calcPatchSize();
855
if (patch.x <= 0 || patch.x >= 6 || patch.y <= 0 || patch.y >= 6)
856
return false;
857
return true;
858
}
859
860
bool sparse(const UMat &prevImg, const UMat &nextImg, const UMat &prevPts, UMat &nextPts, UMat &status, UMat &err)
861
{
862
if (!checkParam())
863
return false;
864
865
UMat temp1 = (useInitialFlow ? nextPts : prevPts).reshape(1);
866
UMat temp2 = nextPts.reshape(1);
867
multiply(1.0f / (1 << maxLevel) /2.0f, temp1, temp2);
868
869
status.setTo(Scalar::all(1));
870
871
// build the image pyramids.
872
std::vector<UMat> prevPyr; prevPyr.resize(maxLevel + 1);
873
std::vector<UMat> nextPyr; nextPyr.resize(maxLevel + 1);
874
875
// allocate buffers with aligned pitch to be able to use cl_khr_image2d_from_buffer extension
876
// This is the required pitch alignment in pixels
877
int pitchAlign = (int)ocl::Device::getDefault().imagePitchAlignment();
878
if (pitchAlign>0)
879
{
880
prevPyr[0] = UMat(prevImg.rows,(prevImg.cols+pitchAlign-1)&(-pitchAlign),CV_32FC1).colRange(0,prevImg.cols);
881
nextPyr[0] = UMat(nextImg.rows,(nextImg.cols+pitchAlign-1)&(-pitchAlign),CV_32FC1).colRange(0,nextImg.cols);
882
for (int level = 1; level <= maxLevel; ++level)
883
{
884
int cols,rows;
885
// allocate buffers with aligned pitch to be able to use image on buffer extension
886
cols = (prevPyr[level - 1].cols+1)/2;
887
rows = (prevPyr[level - 1].rows+1)/2;
888
prevPyr[level] = UMat(rows,(cols+pitchAlign-1)&(-pitchAlign),prevPyr[level-1].type()).colRange(0,cols);
889
cols = (nextPyr[level - 1].cols+1)/2;
890
rows = (nextPyr[level - 1].rows+1)/2;
891
nextPyr[level] = UMat(rows,(cols+pitchAlign-1)&(-pitchAlign),nextPyr[level-1].type()).colRange(0,cols);
892
}
893
}
894
895
prevImg.convertTo(prevPyr[0], CV_32F);
896
nextImg.convertTo(nextPyr[0], CV_32F);
897
898
for (int level = 1; level <= maxLevel; ++level)
899
{
900
pyrDown(prevPyr[level - 1], prevPyr[level]);
901
pyrDown(nextPyr[level - 1], nextPyr[level]);
902
}
903
904
// dI/dx ~ Ix, dI/dy ~ Iy
905
for (int level = maxLevel; level >= 0; level--)
906
{
907
if (!lkSparse_run(prevPyr[level], nextPyr[level], prevPts,
908
nextPts, status, err,
909
prevPts.cols, level))
910
return false;
911
}
912
return true;
913
}
914
#endif
915
916
Size winSize;
917
int maxLevel;
918
TermCriteria criteria;
919
int flags;
920
double minEigThreshold;
921
#ifdef HAVE_OPENCL
922
int iters;
923
double derivLambda;
924
bool useInitialFlow;
925
dim3 patch;
926
void calcPatchSize()
927
{
928
dim3 block;
929
930
if (winSize.width > 32 && winSize.width > 2 * winSize.height)
931
{
932
block.x = 32;
933
block.y = 8;
934
}
935
else
936
{
937
block.x = 16;
938
block.y = 16;
939
}
940
941
patch.x = (winSize.width + block.x - 1) / block.x;
942
patch.y = (winSize.height + block.y - 1) / block.y;
943
944
block.z = patch.z = 1;
945
}
946
947
bool lkSparse_run(UMat &I, UMat &J, const UMat &prevPts, UMat &nextPts, UMat &status, UMat& err,
948
int ptcount, int level)
949
{
950
size_t localThreads[3] = { 8, 8};
951
size_t globalThreads[3] = { 8 * (size_t)ptcount, 8};
952
char calcErr = (0 == level) ? 1 : 0;
953
954
int wsx = 1, wsy = 1;
955
if(winSize.width < 16)
956
wsx = 0;
957
if(winSize.height < 16)
958
wsy = 0;
959
cv::String build_options;
960
if (isDeviceCPU())
961
build_options = " -D CPU";
962
else
963
build_options = cv::format("-D WSX=%d -D WSY=%d",
964
wsx, wsy);
965
966
ocl::Kernel kernel;
967
if (!kernel.create("lkSparse", cv::ocl::video::pyrlk_oclsrc, build_options))
968
return false;
969
970
CV_Assert(I.depth() == CV_32F && J.depth() == CV_32F);
971
ocl::Image2D imageI(I, false, ocl::Image2D::canCreateAlias(I));
972
ocl::Image2D imageJ(J, false, ocl::Image2D::canCreateAlias(J));
973
974
int idxArg = 0;
975
idxArg = kernel.set(idxArg, imageI); //image2d_t I
976
idxArg = kernel.set(idxArg, imageJ); //image2d_t J
977
idxArg = kernel.set(idxArg, ocl::KernelArg::PtrReadOnly(prevPts)); // __global const float2* prevPts
978
idxArg = kernel.set(idxArg, ocl::KernelArg::PtrReadWrite(nextPts)); // __global const float2* nextPts
979
idxArg = kernel.set(idxArg, ocl::KernelArg::PtrReadWrite(status)); // __global uchar* status
980
idxArg = kernel.set(idxArg, ocl::KernelArg::PtrReadWrite(err)); // __global float* err
981
idxArg = kernel.set(idxArg, (int)level); // const int level
982
idxArg = kernel.set(idxArg, (int)I.rows); // const int rows
983
idxArg = kernel.set(idxArg, (int)I.cols); // const int cols
984
idxArg = kernel.set(idxArg, (int)patch.x); // int PATCH_X
985
idxArg = kernel.set(idxArg, (int)patch.y); // int PATCH_Y
986
idxArg = kernel.set(idxArg, (int)winSize.width); // int c_winSize_x
987
idxArg = kernel.set(idxArg, (int)winSize.height); // int c_winSize_y
988
idxArg = kernel.set(idxArg, (int)iters); // int c_iters
989
idxArg = kernel.set(idxArg, (char)calcErr); //char calcErr
990
return kernel.run(2, globalThreads, localThreads, true); // sync=true because ocl::Image2D lifetime is not handled well for temp UMat
991
}
992
private:
993
inline static bool isDeviceCPU()
994
{
995
return (cv::ocl::Device::TYPE_CPU == cv::ocl::Device::getDefault().type());
996
}
997
998
999
bool ocl_calcOpticalFlowPyrLK(InputArray _prevImg, InputArray _nextImg,
1000
InputArray _prevPts, InputOutputArray _nextPts,
1001
OutputArray _status, OutputArray _err)
1002
{
1003
if (0 != (OPTFLOW_LK_GET_MIN_EIGENVALS & flags))
1004
return false;
1005
if (!cv::ocl::Device::getDefault().imageSupport())
1006
return false;
1007
if (_nextImg.size() != _prevImg.size())
1008
return false;
1009
int typePrev = _prevImg.type();
1010
int typeNext = _nextImg.type();
1011
if ((1 != CV_MAT_CN(typePrev)) || (1 != CV_MAT_CN(typeNext)))
1012
return false;
1013
if ((0 != CV_MAT_DEPTH(typePrev)) || (0 != CV_MAT_DEPTH(typeNext)))
1014
return false;
1015
1016
if (_prevPts.empty() || _prevPts.type() != CV_32FC2 || (!_prevPts.isContinuous()))
1017
return false;
1018
if ((1 != _prevPts.size().height) && (1 != _prevPts.size().width))
1019
return false;
1020
size_t npoints = _prevPts.total();
1021
if (useInitialFlow)
1022
{
1023
if (_nextPts.empty() || _nextPts.type() != CV_32FC2 || (!_prevPts.isContinuous()))
1024
return false;
1025
if ((1 != _nextPts.size().height) && (1 != _nextPts.size().width))
1026
return false;
1027
if (_nextPts.total() != npoints)
1028
return false;
1029
}
1030
else
1031
{
1032
_nextPts.create(_prevPts.size(), _prevPts.type());
1033
}
1034
1035
if (!checkParam())
1036
return false;
1037
1038
UMat umatErr;
1039
if (_err.needed())
1040
{
1041
_err.create((int)npoints, 1, CV_32FC1);
1042
umatErr = _err.getUMat();
1043
}
1044
else
1045
umatErr.create((int)npoints, 1, CV_32FC1);
1046
1047
_status.create((int)npoints, 1, CV_8UC1);
1048
UMat umatNextPts = _nextPts.getUMat();
1049
UMat umatStatus = _status.getUMat();
1050
UMat umatPrevPts;
1051
_prevPts.getMat().copyTo(umatPrevPts);
1052
return sparse(_prevImg.getUMat(), _nextImg.getUMat(), umatPrevPts, umatNextPts, umatStatus, umatErr);
1053
}
1054
#endif
1055
1056
#ifdef HAVE_OPENVX
1057
bool openvx_pyrlk(InputArray _prevImg, InputArray _nextImg, InputArray _prevPts, InputOutputArray _nextPts,
1058
OutputArray _status, OutputArray _err)
1059
{
1060
using namespace ivx;
1061
1062
// Pyramids as inputs are not acceptable because there's no (direct or simple) way
1063
// to build vx_pyramid on user data
1064
if(_prevImg.kind() != _InputArray::MAT || _nextImg.kind() != _InputArray::MAT)
1065
return false;
1066
1067
Mat prevImgMat = _prevImg.getMat(), nextImgMat = _nextImg.getMat();
1068
1069
if(prevImgMat.type() != CV_8UC1 || nextImgMat.type() != CV_8UC1)
1070
return false;
1071
1072
if (ovx::skipSmallImages<VX_KERNEL_OPTICAL_FLOW_PYR_LK>(prevImgMat.cols, prevImgMat.rows))
1073
return false;
1074
1075
CV_Assert(prevImgMat.size() == nextImgMat.size());
1076
Mat prevPtsMat = _prevPts.getMat();
1077
int checkPrev = prevPtsMat.checkVector(2, CV_32F, false);
1078
CV_Assert( checkPrev >= 0 );
1079
size_t npoints = checkPrev;
1080
1081
if( !(flags & OPTFLOW_USE_INITIAL_FLOW) )
1082
_nextPts.create(prevPtsMat.size(), prevPtsMat.type(), -1, true);
1083
Mat nextPtsMat = _nextPts.getMat();
1084
CV_Assert( nextPtsMat.checkVector(2, CV_32F, false) == (int)npoints );
1085
1086
_status.create((int)npoints, 1, CV_8U, -1, true);
1087
Mat statusMat = _status.getMat();
1088
uchar* status = statusMat.ptr();
1089
for(size_t i = 0; i < npoints; i++ )
1090
status[i] = true;
1091
1092
// OpenVX doesn't return detection errors
1093
if( _err.needed() )
1094
{
1095
return false;
1096
}
1097
1098
try
1099
{
1100
Context context = ovx::getOpenVXContext();
1101
1102
if(context.vendorID() == VX_ID_KHRONOS)
1103
{
1104
// PyrLK in OVX 1.0.1 performs vxCommitImagePatch incorrecty and crashes
1105
if(VX_VERSION == VX_VERSION_1_0)
1106
return false;
1107
// Implementation ignores border mode
1108
// So check that minimal size of image in pyramid is big enough
1109
int width = prevImgMat.cols, height = prevImgMat.rows;
1110
for(int i = 0; i < maxLevel+1; i++)
1111
{
1112
if(width < winSize.width + 1 || height < winSize.height + 1)
1113
return false;
1114
else
1115
{
1116
width /= 2; height /= 2;
1117
}
1118
}
1119
}
1120
1121
Image prevImg = Image::createFromHandle(context, Image::matTypeToFormat(prevImgMat.type()),
1122
Image::createAddressing(prevImgMat), (void*)prevImgMat.data);
1123
Image nextImg = Image::createFromHandle(context, Image::matTypeToFormat(nextImgMat.type()),
1124
Image::createAddressing(nextImgMat), (void*)nextImgMat.data);
1125
1126
Graph graph = Graph::create(context);
1127
1128
Pyramid prevPyr = Pyramid::createVirtual(graph, (vx_size)maxLevel+1, VX_SCALE_PYRAMID_HALF,
1129
prevImg.width(), prevImg.height(), prevImg.format());
1130
Pyramid nextPyr = Pyramid::createVirtual(graph, (vx_size)maxLevel+1, VX_SCALE_PYRAMID_HALF,
1131
nextImg.width(), nextImg.height(), nextImg.format());
1132
1133
ivx::Node::create(graph, VX_KERNEL_GAUSSIAN_PYRAMID, prevImg, prevPyr);
1134
ivx::Node::create(graph, VX_KERNEL_GAUSSIAN_PYRAMID, nextImg, nextPyr);
1135
1136
Array prevPts = Array::create(context, VX_TYPE_KEYPOINT, npoints);
1137
Array estimatedPts = Array::create(context, VX_TYPE_KEYPOINT, npoints);
1138
Array nextPts = Array::create(context, VX_TYPE_KEYPOINT, npoints);
1139
1140
std::vector<vx_keypoint_t> vxPrevPts(npoints), vxEstPts(npoints), vxNextPts(npoints);
1141
for(size_t i = 0; i < npoints; i++)
1142
{
1143
vx_keypoint_t& prevPt = vxPrevPts[i]; vx_keypoint_t& estPt = vxEstPts[i];
1144
prevPt.x = prevPtsMat.at<Point2f>(i).x; prevPt.y = prevPtsMat.at<Point2f>(i).y;
1145
estPt.x = nextPtsMat.at<Point2f>(i).x; estPt.y = nextPtsMat.at<Point2f>(i).y;
1146
prevPt.tracking_status = estPt.tracking_status = vx_true_e;
1147
}
1148
prevPts.addItems(vxPrevPts); estimatedPts.addItems(vxEstPts);
1149
1150
if( (criteria.type & TermCriteria::COUNT) == 0 )
1151
criteria.maxCount = 30;
1152
else
1153
criteria.maxCount = std::min(std::max(criteria.maxCount, 0), 100);
1154
if( (criteria.type & TermCriteria::EPS) == 0 )
1155
criteria.epsilon = 0.01;
1156
else
1157
criteria.epsilon = std::min(std::max(criteria.epsilon, 0.), 10.);
1158
criteria.epsilon *= criteria.epsilon;
1159
1160
vx_enum termEnum = (criteria.type == TermCriteria::COUNT) ? VX_TERM_CRITERIA_ITERATIONS :
1161
(criteria.type == TermCriteria::EPS) ? VX_TERM_CRITERIA_EPSILON :
1162
VX_TERM_CRITERIA_BOTH;
1163
1164
//minEigThreshold is fixed to 0.0001f
1165
ivx::Scalar termination = ivx::Scalar::create<VX_TYPE_ENUM>(context, termEnum);
1166
ivx::Scalar epsilon = ivx::Scalar::create<VX_TYPE_FLOAT32>(context, criteria.epsilon);
1167
ivx::Scalar numIterations = ivx::Scalar::create<VX_TYPE_UINT32>(context, criteria.maxCount);
1168
ivx::Scalar useInitial = ivx::Scalar::create<VX_TYPE_BOOL>(context, (vx_bool)(flags & OPTFLOW_USE_INITIAL_FLOW));
1169
//assume winSize is square
1170
ivx::Scalar windowSize = ivx::Scalar::create<VX_TYPE_SIZE>(context, (vx_size)winSize.width);
1171
1172
ivx::Node::create(graph, VX_KERNEL_OPTICAL_FLOW_PYR_LK, prevPyr, nextPyr, prevPts, estimatedPts,
1173
nextPts, termination, epsilon, numIterations, useInitial, windowSize);
1174
1175
graph.verify();
1176
graph.process();
1177
1178
nextPts.copyTo(vxNextPts);
1179
for(size_t i = 0; i < npoints; i++)
1180
{
1181
vx_keypoint_t kp = vxNextPts[i];
1182
nextPtsMat.at<Point2f>(i) = Point2f(kp.x, kp.y);
1183
statusMat.at<uchar>(i) = (bool)kp.tracking_status;
1184
}
1185
1186
#ifdef VX_VERSION_1_1
1187
//we should take user memory back before release
1188
//(it's not done automatically according to standard)
1189
prevImg.swapHandle(); nextImg.swapHandle();
1190
#endif
1191
}
1192
catch (RuntimeError & e)
1193
{
1194
VX_DbgThrow(e.what());
1195
}
1196
catch (WrapperError & e)
1197
{
1198
VX_DbgThrow(e.what());
1199
}
1200
1201
return true;
1202
}
1203
#endif
1204
};
1205
1206
1207
1208
void SparsePyrLKOpticalFlowImpl::calc( InputArray _prevImg, InputArray _nextImg,
1209
InputArray _prevPts, InputOutputArray _nextPts,
1210
OutputArray _status, OutputArray _err)
1211
{
1212
CV_INSTRUMENT_REGION();
1213
1214
CV_OCL_RUN(ocl::isOpenCLActivated() &&
1215
(_prevImg.isUMat() || _nextImg.isUMat()) &&
1216
ocl::Image2D::isFormatSupported(CV_32F, 1, false),
1217
ocl_calcOpticalFlowPyrLK(_prevImg, _nextImg, _prevPts, _nextPts, _status, _err))
1218
1219
// Disabled due to bad accuracy
1220
CV_OVX_RUN(false,
1221
openvx_pyrlk(_prevImg, _nextImg, _prevPts, _nextPts, _status, _err))
1222
1223
Mat prevPtsMat = _prevPts.getMat();
1224
const int derivDepth = DataType<cv::detail::deriv_type>::depth;
1225
1226
CV_Assert( maxLevel >= 0 && winSize.width > 2 && winSize.height > 2 );
1227
1228
int level=0, i, npoints;
1229
CV_Assert( (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 );
1230
1231
if( npoints == 0 )
1232
{
1233
_nextPts.release();
1234
_status.release();
1235
_err.release();
1236
return;
1237
}
1238
1239
if( !(flags & OPTFLOW_USE_INITIAL_FLOW) )
1240
_nextPts.create(prevPtsMat.size(), prevPtsMat.type(), -1, true);
1241
1242
Mat nextPtsMat = _nextPts.getMat();
1243
CV_Assert( nextPtsMat.checkVector(2, CV_32F, true) == npoints );
1244
1245
const Point2f* prevPts = prevPtsMat.ptr<Point2f>();
1246
Point2f* nextPts = nextPtsMat.ptr<Point2f>();
1247
1248
_status.create((int)npoints, 1, CV_8U, -1, true);
1249
Mat statusMat = _status.getMat(), errMat;
1250
CV_Assert( statusMat.isContinuous() );
1251
uchar* status = statusMat.ptr();
1252
float* err = 0;
1253
1254
for( i = 0; i < npoints; i++ )
1255
status[i] = true;
1256
1257
if( _err.needed() )
1258
{
1259
_err.create((int)npoints, 1, CV_32F, -1, true);
1260
errMat = _err.getMat();
1261
CV_Assert( errMat.isContinuous() );
1262
err = errMat.ptr<float>();
1263
}
1264
1265
std::vector<Mat> prevPyr, nextPyr;
1266
int levels1 = -1;
1267
int lvlStep1 = 1;
1268
int levels2 = -1;
1269
int lvlStep2 = 1;
1270
1271
if(_prevImg.kind() == _InputArray::STD_VECTOR_MAT)
1272
{
1273
_prevImg.getMatVector(prevPyr);
1274
1275
levels1 = int(prevPyr.size()) - 1;
1276
CV_Assert(levels1 >= 0);
1277
1278
if (levels1 % 2 == 1 && prevPyr[0].channels() * 2 == prevPyr[1].channels() && prevPyr[1].depth() == derivDepth)
1279
{
1280
lvlStep1 = 2;
1281
levels1 /= 2;
1282
}
1283
1284
// ensure that pyramid has reqired padding
1285
if(levels1 > 0)
1286
{
1287
Size fullSize;
1288
Point ofs;
1289
prevPyr[lvlStep1].locateROI(fullSize, ofs);
1290
CV_Assert(ofs.x >= winSize.width && ofs.y >= winSize.height
1291
&& ofs.x + prevPyr[lvlStep1].cols + winSize.width <= fullSize.width
1292
&& ofs.y + prevPyr[lvlStep1].rows + winSize.height <= fullSize.height);
1293
}
1294
1295
if(levels1 < maxLevel)
1296
maxLevel = levels1;
1297
}
1298
1299
if(_nextImg.kind() == _InputArray::STD_VECTOR_MAT)
1300
{
1301
_nextImg.getMatVector(nextPyr);
1302
1303
levels2 = int(nextPyr.size()) - 1;
1304
CV_Assert(levels2 >= 0);
1305
1306
if (levels2 % 2 == 1 && nextPyr[0].channels() * 2 == nextPyr[1].channels() && nextPyr[1].depth() == derivDepth)
1307
{
1308
lvlStep2 = 2;
1309
levels2 /= 2;
1310
}
1311
1312
// ensure that pyramid has reqired padding
1313
if(levels2 > 0)
1314
{
1315
Size fullSize;
1316
Point ofs;
1317
nextPyr[lvlStep2].locateROI(fullSize, ofs);
1318
CV_Assert(ofs.x >= winSize.width && ofs.y >= winSize.height
1319
&& ofs.x + nextPyr[lvlStep2].cols + winSize.width <= fullSize.width
1320
&& ofs.y + nextPyr[lvlStep2].rows + winSize.height <= fullSize.height);
1321
}
1322
1323
if(levels2 < maxLevel)
1324
maxLevel = levels2;
1325
}
1326
1327
if (levels1 < 0)
1328
maxLevel = buildOpticalFlowPyramid(_prevImg, prevPyr, winSize, maxLevel, false);
1329
1330
if (levels2 < 0)
1331
maxLevel = buildOpticalFlowPyramid(_nextImg, nextPyr, winSize, maxLevel, false);
1332
1333
if( (criteria.type & TermCriteria::COUNT) == 0 )
1334
criteria.maxCount = 30;
1335
else
1336
criteria.maxCount = std::min(std::max(criteria.maxCount, 0), 100);
1337
if( (criteria.type & TermCriteria::EPS) == 0 )
1338
criteria.epsilon = 0.01;
1339
else
1340
criteria.epsilon = std::min(std::max(criteria.epsilon, 0.), 10.);
1341
criteria.epsilon *= criteria.epsilon;
1342
1343
// dI/dx ~ Ix, dI/dy ~ Iy
1344
Mat derivIBuf;
1345
if(lvlStep1 == 1)
1346
derivIBuf.create(prevPyr[0].rows + winSize.height*2, prevPyr[0].cols + winSize.width*2, CV_MAKETYPE(derivDepth, prevPyr[0].channels() * 2));
1347
1348
for( level = maxLevel; level >= 0; level-- )
1349
{
1350
Mat derivI;
1351
if(lvlStep1 == 1)
1352
{
1353
Size imgSize = prevPyr[level * lvlStep1].size();
1354
Mat _derivI( imgSize.height + winSize.height*2,
1355
imgSize.width + winSize.width*2, derivIBuf.type(), derivIBuf.ptr() );
1356
derivI = _derivI(Rect(winSize.width, winSize.height, imgSize.width, imgSize.height));
1357
calcSharrDeriv(prevPyr[level * lvlStep1], derivI);
1358
copyMakeBorder(derivI, _derivI, winSize.height, winSize.height, winSize.width, winSize.width, BORDER_CONSTANT|BORDER_ISOLATED);
1359
}
1360
else
1361
derivI = prevPyr[level * lvlStep1 + 1];
1362
1363
CV_Assert(prevPyr[level * lvlStep1].size() == nextPyr[level * lvlStep2].size());
1364
CV_Assert(prevPyr[level * lvlStep1].type() == nextPyr[level * lvlStep2].type());
1365
1366
typedef cv::detail::LKTrackerInvoker LKTrackerInvoker;
1367
parallel_for_(Range(0, npoints), LKTrackerInvoker(prevPyr[level * lvlStep1], derivI,
1368
nextPyr[level * lvlStep2], prevPts, nextPts,
1369
status, err,
1370
winSize, criteria, level, maxLevel,
1371
flags, (float)minEigThreshold));
1372
}
1373
}
1374
1375
} // namespace
1376
} // namespace cv
1377
cv::Ptr<cv::SparsePyrLKOpticalFlow> cv::SparsePyrLKOpticalFlow::create(Size winSize, int maxLevel, TermCriteria crit, int flags, double minEigThreshold){
1378
return makePtr<SparsePyrLKOpticalFlowImpl>(winSize,maxLevel,crit,flags,minEigThreshold);
1379
}
1380
void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
1381
InputArray _prevPts, InputOutputArray _nextPts,
1382
OutputArray _status, OutputArray _err,
1383
Size winSize, int maxLevel,
1384
TermCriteria criteria,
1385
int flags, double minEigThreshold )
1386
{
1387
Ptr<cv::SparsePyrLKOpticalFlow> optflow = cv::SparsePyrLKOpticalFlow::create(winSize,maxLevel,criteria,flags,minEigThreshold);
1388
optflow->calc(_prevImg,_nextImg,_prevPts,_nextPts,_status,_err);
1389
}
1390
1391
cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullAffine )
1392
{
1393
CV_INSTRUMENT_REGION();
1394
#ifndef HAVE_OPENCV_CALIB3D
1395
CV_UNUSED(src1); CV_UNUSED(src2); CV_UNUSED(fullAffine);
1396
CV_Error(Error::StsError, "estimateRigidTransform requires calib3d module");
1397
#else
1398
Mat A = src1.getMat(), B = src2.getMat();
1399
1400
const int COUNT = 15;
1401
const int WIDTH = 160, HEIGHT = 120;
1402
1403
std::vector<Point2f> pA, pB;
1404
std::vector<uchar> status;
1405
1406
double scale = 1.;
1407
int i, j, k;
1408
1409
if( A.size() != B.size() )
1410
CV_Error( Error::StsUnmatchedSizes, "Both input images must have the same size" );
1411
1412
if( A.type() != B.type() )
1413
CV_Error( Error::StsUnmatchedFormats, "Both input images must have the same data type" );
1414
1415
int count = A.checkVector(2);
1416
1417
if( count > 0 )
1418
{
1419
// inputs are points
1420
A.reshape(2, count).convertTo(pA, CV_32F);
1421
B.reshape(2, count).convertTo(pB, CV_32F);
1422
}
1423
else if( A.depth() == CV_8U )
1424
{
1425
// inputs are images
1426
int cn = A.channels();
1427
CV_Assert( cn == 1 || cn == 3 || cn == 4 );
1428
Size sz0 = A.size();
1429
Size sz1(WIDTH, HEIGHT);
1430
1431
scale = std::max(1., std::max( (double)sz1.width/sz0.width, (double)sz1.height/sz0.height ));
1432
1433
sz1.width = cvRound( sz0.width * scale );
1434
sz1.height = cvRound( sz0.height * scale );
1435
1436
bool equalSizes = sz1.width == sz0.width && sz1.height == sz0.height;
1437
1438
if( !equalSizes || cn != 1 )
1439
{
1440
Mat sA, sB;
1441
1442
if( cn != 1 )
1443
{
1444
Mat gray;
1445
cvtColor(A, gray, COLOR_BGR2GRAY);
1446
resize(gray, sA, sz1, 0., 0., INTER_AREA);
1447
cvtColor(B, gray, COLOR_BGR2GRAY);
1448
resize(gray, sB, sz1, 0., 0., INTER_AREA);
1449
}
1450
else
1451
{
1452
resize(A, sA, sz1, 0., 0., INTER_AREA);
1453
resize(B, sB, sz1, 0., 0., INTER_AREA);
1454
}
1455
1456
A = sA;
1457
B = sB;
1458
}
1459
1460
int count_y = COUNT;
1461
int count_x = cvRound((double)COUNT*sz1.width/sz1.height);
1462
count = count_x * count_y;
1463
1464
pA.resize(count);
1465
pB.resize(count);
1466
status.resize(count);
1467
1468
for( i = 0, k = 0; i < count_y; i++ )
1469
for( j = 0; j < count_x; j++, k++ )
1470
{
1471
pA[k].x = (j+0.5f)*sz1.width/count_x;
1472
pA[k].y = (i+0.5f)*sz1.height/count_y;
1473
}
1474
1475
// find the corresponding points in B
1476
calcOpticalFlowPyrLK(A, B, pA, pB, status, noArray(), Size(21, 21), 3,
1477
TermCriteria(TermCriteria::MAX_ITER,40,0.1));
1478
1479
// repack the remained points
1480
for( i = 0, k = 0; i < count; i++ )
1481
if( status[i] )
1482
{
1483
if( i > k )
1484
{
1485
pA[k] = pA[i];
1486
pB[k] = pB[i];
1487
}
1488
k++;
1489
}
1490
count = k;
1491
pA.resize(count);
1492
pB.resize(count);
1493
}
1494
else
1495
CV_Error( Error::StsUnsupportedFormat, "Both input images must have either 8uC1 or 8uC3 type" );
1496
1497
if (fullAffine)
1498
{
1499
return estimateAffine2D(pA, pB);
1500
}
1501
else
1502
{
1503
return estimateAffinePartial2D(pA, pB);
1504
}
1505
#endif
1506
}
1507
1508