Blame view

3rdparty/opencv-4.5.4/modules/video/src/dis_flow.cpp 66.9 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
  /*M///////////////////////////////////////////////////////////////////////////////////////
  //
  //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  //
  //  By downloading, copying, installing or using the software you agree to this license.
  //  If you do not agree to this license, do not download, install,
  //  copy or use the software.
  //
  //
  //                           License Agreement
  //                For Open Source Computer Vision Library
  //
  // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  // Third party copyrights are property of their respective owners.
  //
  // Redistribution and use in source and binary forms, with or without modification,
  // are permitted provided that the following conditions are met:
  //
  //   * Redistribution's of source code must retain the above copyright notice,
  //     this list of conditions and the following disclaimer.
  //
  //   * Redistribution's in binary form must reproduce the above copyright notice,
  //     this list of conditions and the following disclaimer in the documentation
  //     and/or other materials provided with the distribution.
  //
  //   * The name of the copyright holders may not be used to endorse or promote products
  //     derived from this software without specific prior written permission.
  //
  // This software is provided by the copyright holders and contributors "as is" and
  // any express or implied warranties, including, but not limited to, the implied
  // warranties of merchantability and fitness for a particular purpose are disclaimed.
  // In no event shall the Intel Corporation or contributors be liable for any direct,
  // indirect, incidental, special, exemplary, or consequential damages
  // (including, but not limited to, procurement of substitute goods or services;
  // loss of use, data, or profits; or business interruption) however caused
  // and on any theory of liability, whether in contract, strict liability,
  // or tort (including negligence or otherwise) arising in any way out of
  // the use of this software, even if advised of the possibility of such damage.
  //
  //M*/
  
  #include "precomp.hpp"
  #include "opencv2/core/hal/intrin.hpp"
  #include "opencl_kernels_video.hpp"
  
  using namespace std;
  #define EPS 0.001F
  #define INF 1E+10F
  
  namespace cv {
  
  class DISOpticalFlowImpl CV_FINAL : public DISOpticalFlow
  {
    public:
      DISOpticalFlowImpl();
  
      void calc(InputArray I0, InputArray I1, InputOutputArray flow) CV_OVERRIDE;
      void collectGarbage() CV_OVERRIDE;
  
    protected: //!< algorithm parameters
      int finest_scale, coarsest_scale;
      int patch_size;
      int patch_stride;
      int grad_descent_iter;
      int variational_refinement_iter;
      float variational_refinement_alpha;
      float variational_refinement_gamma;
      float variational_refinement_delta;
      bool use_mean_normalization;
      bool use_spatial_propagation;
  
    protected: //!< some auxiliary variables
      int border_size;
      int w, h;   //!< flow buffer width and height on the current scale
      int ws, hs; //!< sparse flow buffer width and height on the current scale
  
    public:
      int getFinestScale() const CV_OVERRIDE { return finest_scale; }
      void setFinestScale(int val) CV_OVERRIDE { finest_scale = val; }
      int getPatchSize() const CV_OVERRIDE { return patch_size; }
      void setPatchSize(int val) CV_OVERRIDE { patch_size = val; }
      int getPatchStride() const CV_OVERRIDE { return patch_stride; }
      void setPatchStride(int val) CV_OVERRIDE { patch_stride = val; }
      int getGradientDescentIterations() const CV_OVERRIDE { return grad_descent_iter; }
      void setGradientDescentIterations(int val) CV_OVERRIDE { grad_descent_iter = val; }
      int getVariationalRefinementIterations() const CV_OVERRIDE { return variational_refinement_iter; }
      void setVariationalRefinementIterations(int val) CV_OVERRIDE { variational_refinement_iter = val; }
      float getVariationalRefinementAlpha() const CV_OVERRIDE { return variational_refinement_alpha; }
      void setVariationalRefinementAlpha(float val) CV_OVERRIDE { variational_refinement_alpha = val; }
      float getVariationalRefinementDelta() const CV_OVERRIDE { return variational_refinement_delta; }
      void setVariationalRefinementDelta(float val) CV_OVERRIDE { variational_refinement_delta = val; }
      float getVariationalRefinementGamma() const CV_OVERRIDE { return variational_refinement_gamma; }
      void setVariationalRefinementGamma(float val) CV_OVERRIDE { variational_refinement_gamma = val; }
  
      bool getUseMeanNormalization() const CV_OVERRIDE { return use_mean_normalization; }
      void setUseMeanNormalization(bool val) CV_OVERRIDE { use_mean_normalization = val; }
      bool getUseSpatialPropagation() const CV_OVERRIDE { return use_spatial_propagation; }
      void setUseSpatialPropagation(bool val) CV_OVERRIDE { use_spatial_propagation = val; }
  
    protected:                      //!< internal buffers
      vector<Mat_<uchar> > I0s;     //!< Gaussian pyramid for the current frame
      vector<Mat_<uchar> > I1s;     //!< Gaussian pyramid for the next frame
      vector<Mat_<uchar> > I1s_ext; //!< I1s with borders
  
      vector<Mat_<short> > I0xs; //!< Gaussian pyramid for the x gradient of the current frame
      vector<Mat_<short> > I0ys; //!< Gaussian pyramid for the y gradient of the current frame
  
      vector<Mat_<float> > Ux; //!< x component of the flow vectors
      vector<Mat_<float> > Uy; //!< y component of the flow vectors
  
      vector<Mat_<float> > initial_Ux; //!< x component of the initial flow field, if one was passed as an input
      vector<Mat_<float> > initial_Uy; //!< y component of the initial flow field, if one was passed as an input
  
      Mat_<Vec2f> U; //!< a buffer for the merged flow
  
      Mat_<float> Sx; //!< intermediate sparse flow representation (x component)
      Mat_<float> Sy; //!< intermediate sparse flow representation (y component)
  
      /* Structure tensor components: */
      Mat_<float> I0xx_buf; //!< sum of squares of x gradient values
      Mat_<float> I0yy_buf; //!< sum of squares of y gradient values
      Mat_<float> I0xy_buf; //!< sum of x and y gradient products
  
      /* Extra buffers that are useful if patch mean-normalization is used: */
      Mat_<float> I0x_buf; //!< sum of x gradient values
      Mat_<float> I0y_buf; //!< sum of y gradient values
  
      /* Auxiliary buffers used in structure tensor computation: */
      Mat_<float> I0xx_buf_aux;
      Mat_<float> I0yy_buf_aux;
      Mat_<float> I0xy_buf_aux;
      Mat_<float> I0x_buf_aux;
      Mat_<float> I0y_buf_aux;
  
      vector<Ptr<VariationalRefinement> > variational_refinement_processors;
  
    private: //!< private methods and parallel sections
      void prepareBuffers(Mat &I0, Mat &I1, Mat &flow, bool use_flow);
      void precomputeStructureTensor(Mat &dst_I0xx, Mat &dst_I0yy, Mat &dst_I0xy, Mat &dst_I0x, Mat &dst_I0y, Mat &I0x,
                                     Mat &I0y);
      int autoSelectCoarsestScale(int img_width);
      void autoSelectPatchSizeAndScales(int img_width);
  
      struct PatchInverseSearch_ParBody : public ParallelLoopBody
      {
          DISOpticalFlowImpl *dis;
          int nstripes, stripe_sz;
          int hs;
          Mat *Sx, *Sy, *Ux, *Uy, *I0, *I1, *I0x, *I0y;
          int num_iter, pyr_level;
  
          PatchInverseSearch_ParBody(DISOpticalFlowImpl &_dis, int _nstripes, int _hs, Mat &dst_Sx, Mat &dst_Sy,
                                     Mat &src_Ux, Mat &src_Uy, Mat &_I0, Mat &_I1, Mat &_I0x, Mat &_I0y, int _num_iter,
                                     int _pyr_level);
          void operator()(const Range &range) const CV_OVERRIDE;
      };
  
      struct Densification_ParBody : public ParallelLoopBody
      {
          DISOpticalFlowImpl *dis;
          int nstripes, stripe_sz;
          int h;
          Mat *Ux, *Uy, *Sx, *Sy, *I0, *I1;
  
          Densification_ParBody(DISOpticalFlowImpl &_dis, int _nstripes, int _h, Mat &dst_Ux, Mat &dst_Uy, Mat &src_Sx,
                                Mat &src_Sy, Mat &_I0, Mat &_I1);
          void operator()(const Range &range) const CV_OVERRIDE;
      };
  
  #ifdef HAVE_OPENCL
      vector<UMat> u_I0s;     //!< Gaussian pyramid for the current frame
      vector<UMat> u_I1s;     //!< Gaussian pyramid for the next frame
      vector<UMat> u_I1s_ext; //!< I1s with borders
  
      vector<UMat> u_I0xs; //!< Gaussian pyramid for the x gradient of the current frame
      vector<UMat> u_I0ys; //!< Gaussian pyramid for the y gradient of the current frame
  
      vector<UMat> u_U; //!< (x,y) component of the flow vectors (CV_32FC2)
      vector<UMat> u_initial_U; //!< (x, y) components of the initial flow field, if one was passed as an input (CV_32FC2)
  
      UMat u_S; //!< intermediate sparse flow representation (x,y components - CV_32FC2)
  
      /* Structure tensor components: */
      UMat u_I0xx_buf; //!< sum of squares of x gradient values
      UMat u_I0yy_buf; //!< sum of squares of y gradient values
      UMat u_I0xy_buf; //!< sum of x and y gradient products
  
      /* Extra buffers that are useful if patch mean-normalization is used: */
      UMat u_I0x_buf; //!< sum of x gradient values
      UMat u_I0y_buf; //!< sum of y gradient values
  
      /* Auxiliary buffers used in structure tensor computation: */
      UMat u_I0xx_buf_aux;
      UMat u_I0yy_buf_aux;
      UMat u_I0xy_buf_aux;
      UMat u_I0x_buf_aux;
      UMat u_I0y_buf_aux;
  
      bool ocl_precomputeStructureTensor(UMat &dst_I0xx, UMat &dst_I0yy, UMat &dst_I0xy,
                                         UMat &dst_I0x, UMat &dst_I0y, UMat &I0x, UMat &I0y);
      void ocl_prepareBuffers(UMat &I0, UMat &I1, InputArray flow, bool use_flow);
      bool ocl_calc(InputArray I0, InputArray I1, InputOutputArray flow);
      bool ocl_Densification(UMat &dst_U, UMat &src_S, UMat &_I0, UMat &_I1);
      bool ocl_PatchInverseSearch(UMat &src_U,
                                  UMat &I0, UMat &I1, UMat &I0x, UMat &I0y, int num_iter, int pyr_level);
  #endif
  };
  
  DISOpticalFlowImpl::DISOpticalFlowImpl()
  {
      CV_INSTRUMENT_REGION();
  
      finest_scale = 2;
      patch_size = 8;
      patch_stride = 4;
      grad_descent_iter = 16;
      variational_refinement_iter = 5;
      variational_refinement_alpha = 20.f;
      variational_refinement_gamma = 10.f;
      variational_refinement_delta = 5.f;
  
      border_size = 16;
      use_mean_normalization = true;
      use_spatial_propagation = true;
      coarsest_scale = 10;
  
      /* Use separate variational refinement instances for different scales to avoid repeated memory allocation: */
      int max_possible_scales = 10;
      ws = hs = w = h = 0;
      for (int i = 0; i < max_possible_scales; i++)
          variational_refinement_processors.push_back(VariationalRefinement::create());
  }
  
  void DISOpticalFlowImpl::prepareBuffers(Mat &I0, Mat &I1, Mat &flow, bool use_flow)
  {
      CV_INSTRUMENT_REGION();
  
      I0s.resize(coarsest_scale + 1);
      I1s.resize(coarsest_scale + 1);
      I1s_ext.resize(coarsest_scale + 1);
      I0xs.resize(coarsest_scale + 1);
      I0ys.resize(coarsest_scale + 1);
      Ux.resize(coarsest_scale + 1);
      Uy.resize(coarsest_scale + 1);
  
      Mat flow_uv[2];
      if (use_flow)
      {
          split(flow, flow_uv);
          initial_Ux.resize(coarsest_scale + 1);
          initial_Uy.resize(coarsest_scale + 1);
      }
  
      int fraction = 1;
      int cur_rows = 0, cur_cols = 0;
  
      for (int i = 0; i <= coarsest_scale; i++)
      {
          /* Avoid initializing the pyramid levels above the finest scale, as they won't be used anyway */
          if (i == finest_scale)
          {
              cur_rows = I0.rows / fraction;
              cur_cols = I0.cols / fraction;
              I0s[i].create(cur_rows, cur_cols);
              resize(I0, I0s[i], I0s[i].size(), 0.0, 0.0, INTER_AREA);
              I1s[i].create(cur_rows, cur_cols);
              resize(I1, I1s[i], I1s[i].size(), 0.0, 0.0, INTER_AREA);
  
              /* These buffers are reused in each scale so we initialize them once on the finest scale: */
              Sx.create(cur_rows / patch_stride, cur_cols / patch_stride);
              Sy.create(cur_rows / patch_stride, cur_cols / patch_stride);
              I0xx_buf.create(cur_rows / patch_stride, cur_cols / patch_stride);
              I0yy_buf.create(cur_rows / patch_stride, cur_cols / patch_stride);
              I0xy_buf.create(cur_rows / patch_stride, cur_cols / patch_stride);
              I0x_buf.create(cur_rows / patch_stride, cur_cols / patch_stride);
              I0y_buf.create(cur_rows / patch_stride, cur_cols / patch_stride);
  
              I0xx_buf_aux.create(cur_rows, cur_cols / patch_stride);
              I0yy_buf_aux.create(cur_rows, cur_cols / patch_stride);
              I0xy_buf_aux.create(cur_rows, cur_cols / patch_stride);
              I0x_buf_aux.create(cur_rows, cur_cols / patch_stride);
              I0y_buf_aux.create(cur_rows, cur_cols / patch_stride);
  
              U.create(cur_rows, cur_cols);
          }
          else if (i > finest_scale)
          {
              cur_rows = I0s[i - 1].rows / 2;
              cur_cols = I0s[i - 1].cols / 2;
              I0s[i].create(cur_rows, cur_cols);
              resize(I0s[i - 1], I0s[i], I0s[i].size(), 0.0, 0.0, INTER_AREA);
              I1s[i].create(cur_rows, cur_cols);
              resize(I1s[i - 1], I1s[i], I1s[i].size(), 0.0, 0.0, INTER_AREA);
          }
  
          if (i >= finest_scale)
          {
              I1s_ext[i].create(cur_rows + 2 * border_size, cur_cols + 2 * border_size);
              copyMakeBorder(I1s[i], I1s_ext[i], border_size, border_size, border_size, border_size, BORDER_REPLICATE);
              I0xs[i].create(cur_rows, cur_cols);
              I0ys[i].create(cur_rows, cur_cols);
              spatialGradient(I0s[i], I0xs[i], I0ys[i]);
              Ux[i].create(cur_rows, cur_cols);
              Uy[i].create(cur_rows, cur_cols);
              variational_refinement_processors[i]->setAlpha(variational_refinement_alpha);
              variational_refinement_processors[i]->setDelta(variational_refinement_delta);
              variational_refinement_processors[i]->setGamma(variational_refinement_gamma);
              variational_refinement_processors[i]->setSorIterations(5);
              variational_refinement_processors[i]->setFixedPointIterations(variational_refinement_iter);
  
              if (use_flow)
              {
                  resize(flow_uv[0], initial_Ux[i], Size(cur_cols, cur_rows));
                  initial_Ux[i] /= fraction;
                  resize(flow_uv[1], initial_Uy[i], Size(cur_cols, cur_rows));
                  initial_Uy[i] /= fraction;
              }
          }
  
          fraction *= 2;
      }
  }
  
  /* This function computes the structure tensor elements (local sums of I0x^2, I0x*I0y and I0y^2).
   * A simple box filter is not used instead because we need to compute these sums on a sparse grid
   * and store them densely in the output buffers.
   */
  void DISOpticalFlowImpl::precomputeStructureTensor(Mat &dst_I0xx, Mat &dst_I0yy, Mat &dst_I0xy, Mat &dst_I0x,
                                                     Mat &dst_I0y, Mat &I0x, Mat &I0y)
  {
      CV_INSTRUMENT_REGION();
  
      float *I0xx_ptr = dst_I0xx.ptr<float>();
      float *I0yy_ptr = dst_I0yy.ptr<float>();
      float *I0xy_ptr = dst_I0xy.ptr<float>();
      float *I0x_ptr = dst_I0x.ptr<float>();
      float *I0y_ptr = dst_I0y.ptr<float>();
  
      float *I0xx_aux_ptr = I0xx_buf_aux.ptr<float>();
      float *I0yy_aux_ptr = I0yy_buf_aux.ptr<float>();
      float *I0xy_aux_ptr = I0xy_buf_aux.ptr<float>();
      float *I0x_aux_ptr = I0x_buf_aux.ptr<float>();
      float *I0y_aux_ptr = I0y_buf_aux.ptr<float>();
  
      /* Separable box filter: horizontal pass */
      for (int i = 0; i < h; i++)
      {
          float sum_xx = 0.0f, sum_yy = 0.0f, sum_xy = 0.0f, sum_x = 0.0f, sum_y = 0.0f;
          short *x_row = I0x.ptr<short>(i);
          short *y_row = I0y.ptr<short>(i);
          for (int j = 0; j < patch_size; j++)
          {
              sum_xx += x_row[j] * x_row[j];
              sum_yy += y_row[j] * y_row[j];
              sum_xy += x_row[j] * y_row[j];
              sum_x += x_row[j];
              sum_y += y_row[j];
          }
          I0xx_aux_ptr[i * ws] = sum_xx;
          I0yy_aux_ptr[i * ws] = sum_yy;
          I0xy_aux_ptr[i * ws] = sum_xy;
          I0x_aux_ptr[i * ws] = sum_x;
          I0y_aux_ptr[i * ws] = sum_y;
          int js = 1;
          for (int j = patch_size; j < w; j++)
          {
              sum_xx += (x_row[j] * x_row[j] - x_row[j - patch_size] * x_row[j - patch_size]);
              sum_yy += (y_row[j] * y_row[j] - y_row[j - patch_size] * y_row[j - patch_size]);
              sum_xy += (x_row[j] * y_row[j] - x_row[j - patch_size] * y_row[j - patch_size]);
              sum_x += (x_row[j] - x_row[j - patch_size]);
              sum_y += (y_row[j] - y_row[j - patch_size]);
              if ((j - patch_size + 1) % patch_stride == 0)
              {
                  I0xx_aux_ptr[i * ws + js] = sum_xx;
                  I0yy_aux_ptr[i * ws + js] = sum_yy;
                  I0xy_aux_ptr[i * ws + js] = sum_xy;
                  I0x_aux_ptr[i * ws + js] = sum_x;
                  I0y_aux_ptr[i * ws + js] = sum_y;
                  js++;
              }
          }
      }
  
      AutoBuffer<float> sum_xx(ws), sum_yy(ws), sum_xy(ws), sum_x(ws), sum_y(ws);
      for (int j = 0; j < ws; j++)
      {
          sum_xx[j] = 0.0f;
          sum_yy[j] = 0.0f;
          sum_xy[j] = 0.0f;
          sum_x[j] = 0.0f;
          sum_y[j] = 0.0f;
      }
  
      /* Separable box filter: vertical pass */
      for (int i = 0; i < patch_size; i++)
          for (int j = 0; j < ws; j++)
          {
              sum_xx[j] += I0xx_aux_ptr[i * ws + j];
              sum_yy[j] += I0yy_aux_ptr[i * ws + j];
              sum_xy[j] += I0xy_aux_ptr[i * ws + j];
              sum_x[j] += I0x_aux_ptr[i * ws + j];
              sum_y[j] += I0y_aux_ptr[i * ws + j];
          }
      for (int j = 0; j < ws; j++)
      {
          I0xx_ptr[j] = sum_xx[j];
          I0yy_ptr[j] = sum_yy[j];
          I0xy_ptr[j] = sum_xy[j];
          I0x_ptr[j] = sum_x[j];
          I0y_ptr[j] = sum_y[j];
      }
      int is = 1;
      for (int i = patch_size; i < h; i++)
      {
          for (int j = 0; j < ws; j++)
          {
              sum_xx[j] += (I0xx_aux_ptr[i * ws + j] - I0xx_aux_ptr[(i - patch_size) * ws + j]);
              sum_yy[j] += (I0yy_aux_ptr[i * ws + j] - I0yy_aux_ptr[(i - patch_size) * ws + j]);
              sum_xy[j] += (I0xy_aux_ptr[i * ws + j] - I0xy_aux_ptr[(i - patch_size) * ws + j]);
              sum_x[j] += (I0x_aux_ptr[i * ws + j] - I0x_aux_ptr[(i - patch_size) * ws + j]);
              sum_y[j] += (I0y_aux_ptr[i * ws + j] - I0y_aux_ptr[(i - patch_size) * ws + j]);
          }
          if ((i - patch_size + 1) % patch_stride == 0)
          {
              for (int j = 0; j < ws; j++)
              {
                  I0xx_ptr[is * ws + j] = sum_xx[j];
                  I0yy_ptr[is * ws + j] = sum_yy[j];
                  I0xy_ptr[is * ws + j] = sum_xy[j];
                  I0x_ptr[is * ws + j] = sum_x[j];
                  I0y_ptr[is * ws + j] = sum_y[j];
              }
              is++;
          }
      }
  }
  
  int DISOpticalFlowImpl::autoSelectCoarsestScale(int img_width)
  {
      const int fratio = 5;
      return std::max(0, (int)std::floor(log2((2.0f*(float)img_width) / ((float)fratio * (float)patch_size))));
  }
  
  void DISOpticalFlowImpl::autoSelectPatchSizeAndScales(int img_width)
  {
      switch (finest_scale)
      {
      case 1:
          patch_size = 8;
          coarsest_scale = autoSelectCoarsestScale(img_width);
          finest_scale = std::max(coarsest_scale-2, 0);
          break;
  
      case 3:
          patch_size = 12;
          coarsest_scale = autoSelectCoarsestScale(img_width);
          finest_scale = std::max(coarsest_scale-4, 0);
          break;
  
      case 4:
          patch_size = 12;
          coarsest_scale = autoSelectCoarsestScale(img_width);
          finest_scale = std::max(coarsest_scale-5, 0);
          break;
  
      // default case, fall-through.
      case 2:
      default:
          patch_size = 8;
          coarsest_scale = autoSelectCoarsestScale(img_width);
          finest_scale = std::max(coarsest_scale-2, 0);
          break;
      }
  }
  
  DISOpticalFlowImpl::PatchInverseSearch_ParBody::PatchInverseSearch_ParBody(DISOpticalFlowImpl &_dis, int _nstripes,
                                                                             int _hs, Mat &dst_Sx, Mat &dst_Sy,
                                                                             Mat &src_Ux, Mat &src_Uy, Mat &_I0, Mat &_I1,
                                                                             Mat &_I0x, Mat &_I0y, int _num_iter,
                                                                             int _pyr_level)
      : dis(&_dis), nstripes(_nstripes), hs(_hs), Sx(&dst_Sx), Sy(&dst_Sy), Ux(&src_Ux), Uy(&src_Uy), I0(&_I0), I1(&_I1),
        I0x(&_I0x), I0y(&_I0y), num_iter(_num_iter), pyr_level(_pyr_level)
  {
      stripe_sz = (int)ceil(hs / (double)nstripes);
  }
  
  /////////////////////////////////////////////* Patch processing functions */////////////////////////////////////////////
  
  /* Some auxiliary macros */
  #define HAL_INIT_BILINEAR_8x8_PATCH_EXTRACTION                                                                         \
      v_float32x4 w00v = v_setall_f32(w00);                                                                              \
      v_float32x4 w01v = v_setall_f32(w01);                                                                              \
      v_float32x4 w10v = v_setall_f32(w10);                                                                              \
      v_float32x4 w11v = v_setall_f32(w11);                                                                              \
                                                                                                                         \
      v_uint16x8 I0_row_8, I1_row_8, I1_row_shifted_8, I1_row_next_8, I1_row_next_shifted_8, tmp;                        \
      v_uint32x4 I0_row_4_left, I1_row_4_left, I1_row_shifted_4_left, I1_row_next_4_left, I1_row_next_shifted_4_left;    \
      v_uint32x4 I0_row_4_right, I1_row_4_right, I1_row_shifted_4_right, I1_row_next_4_right,                            \
        I1_row_next_shifted_4_right;                                                                                     \
      v_float32x4 I_diff_left, I_diff_right;                                                                             \
                                                                                                                         \
      /* Preload and expand the first row of I1: */                                                                      \
      I1_row_8 = v_load_expand(I1_ptr);                                                                                  \
      I1_row_shifted_8 = v_load_expand(I1_ptr + 1);                                                                      \
      v_expand(I1_row_8, I1_row_4_left, I1_row_4_right);                                                                 \
      v_expand(I1_row_shifted_8, I1_row_shifted_4_left, I1_row_shifted_4_right);                                         \
      I1_ptr += I1_stride;
  
  #define HAL_PROCESS_BILINEAR_8x8_PATCH_EXTRACTION                                                                      \
      /* Load the next row of I1: */                                                                                     \
      I1_row_next_8 = v_load_expand(I1_ptr);                                                                             \
      I1_row_next_shifted_8 = v_load_expand(I1_ptr + 1);                                                                 \
      /* Separate the left and right halves: */                                                                          \
      v_expand(I1_row_next_8, I1_row_next_4_left, I1_row_next_4_right);                                                  \
      v_expand(I1_row_next_shifted_8, I1_row_next_shifted_4_left, I1_row_next_shifted_4_right);                          \
                                                                                                                         \
      /* Load current row of I0: */                                                                                      \
      I0_row_8 = v_load_expand(I0_ptr);                                                                                  \
      v_expand(I0_row_8, I0_row_4_left, I0_row_4_right);                                                                 \
                                                                                                                         \
      /* Compute diffs between I0 and bilinearly interpolated I1: */                                                     \
      I_diff_left = w00v * v_cvt_f32(v_reinterpret_as_s32(I1_row_4_left)) +                                              \
                    w01v * v_cvt_f32(v_reinterpret_as_s32(I1_row_shifted_4_left)) +                                      \
                    w10v * v_cvt_f32(v_reinterpret_as_s32(I1_row_next_4_left)) +                                         \
                    w11v * v_cvt_f32(v_reinterpret_as_s32(I1_row_next_shifted_4_left)) -                                 \
                    v_cvt_f32(v_reinterpret_as_s32(I0_row_4_left));                                                      \
      I_diff_right = w00v * v_cvt_f32(v_reinterpret_as_s32(I1_row_4_right)) +                                            \
                     w01v * v_cvt_f32(v_reinterpret_as_s32(I1_row_shifted_4_right)) +                                    \
                     w10v * v_cvt_f32(v_reinterpret_as_s32(I1_row_next_4_right)) +                                       \
                     w11v * v_cvt_f32(v_reinterpret_as_s32(I1_row_next_shifted_4_right)) -                               \
                     v_cvt_f32(v_reinterpret_as_s32(I0_row_4_right));
  
  #define HAL_BILINEAR_8x8_PATCH_EXTRACTION_NEXT_ROW                                                                     \
      I0_ptr += I0_stride;                                                                                               \
      I1_ptr += I1_stride;                                                                                               \
                                                                                                                         \
      I1_row_4_left = I1_row_next_4_left;                                                                                \
      I1_row_4_right = I1_row_next_4_right;                                                                              \
      I1_row_shifted_4_left = I1_row_next_shifted_4_left;                                                                \
      I1_row_shifted_4_right = I1_row_next_shifted_4_right;
  
  /* This function essentially performs one iteration of gradient descent when finding the most similar patch in I1 for a
   * given one in I0. It assumes that I0_ptr and I1_ptr already point to the corresponding patches and w00, w01, w10, w11
   * are precomputed bilinear interpolation weights. It returns the SSD (sum of squared differences) between these patches
   * and computes the values (dst_dUx, dst_dUy) that are used in the flow vector update. HAL acceleration is implemented
   * only for the default patch size (8x8). Everything is processed in floats as using fixed-point approximations harms
   * the quality significantly.
   */
  inline float processPatch(float &dst_dUx, float &dst_dUy, uchar *I0_ptr, uchar *I1_ptr, short *I0x_ptr, short *I0y_ptr,
                            int I0_stride, int I1_stride, float w00, float w01, float w10, float w11, int patch_sz)
  {
      float SSD = 0.0f;
  #if CV_SIMD128
      if (patch_sz == 8)
      {
          /* Variables to accumulate the sums */
          v_float32x4 Ux_vec = v_setall_f32(0);
          v_float32x4 Uy_vec = v_setall_f32(0);
          v_float32x4 SSD_vec = v_setall_f32(0);
  
          v_int16x8 I0x_row, I0y_row;
          v_int32x4 I0x_row_4_left, I0x_row_4_right, I0y_row_4_left, I0y_row_4_right;
  
          HAL_INIT_BILINEAR_8x8_PATCH_EXTRACTION;
          for (int row = 0; row < 8; row++)
          {
              HAL_PROCESS_BILINEAR_8x8_PATCH_EXTRACTION;
              I0x_row = v_load(I0x_ptr);
              v_expand(I0x_row, I0x_row_4_left, I0x_row_4_right);
              I0y_row = v_load(I0y_ptr);
              v_expand(I0y_row, I0y_row_4_left, I0y_row_4_right);
  
              /* Update the sums: */
              Ux_vec += I_diff_left * v_cvt_f32(I0x_row_4_left) + I_diff_right * v_cvt_f32(I0x_row_4_right);
              Uy_vec += I_diff_left * v_cvt_f32(I0y_row_4_left) + I_diff_right * v_cvt_f32(I0y_row_4_right);
              SSD_vec += I_diff_left * I_diff_left + I_diff_right * I_diff_right;
  
              I0x_ptr += I0_stride;
              I0y_ptr += I0_stride;
              HAL_BILINEAR_8x8_PATCH_EXTRACTION_NEXT_ROW;
          }
  
          /* Final reduce operations: */
          dst_dUx = v_reduce_sum(Ux_vec);
          dst_dUy = v_reduce_sum(Uy_vec);
          SSD = v_reduce_sum(SSD_vec);
      }
      else
  #endif
      {
          dst_dUx = 0.0f;
          dst_dUy = 0.0f;
          float diff;
          for (int i = 0; i < patch_sz; i++)
              for (int j = 0; j < patch_sz; j++)
              {
                  diff = w00 * I1_ptr[i * I1_stride + j] + w01 * I1_ptr[i * I1_stride + j + 1] +
                         w10 * I1_ptr[(i + 1) * I1_stride + j] + w11 * I1_ptr[(i + 1) * I1_stride + j + 1] -
                         I0_ptr[i * I0_stride + j];
  
                  SSD += diff * diff;
                  dst_dUx += diff * I0x_ptr[i * I0_stride + j];
                  dst_dUy += diff * I0y_ptr[i * I0_stride + j];
              }
      }
      return SSD;
  }
  
  /* Same as processPatch, but with patch mean normalization, which improves robustness under changing
   * lighting conditions
   */
  inline float processPatchMeanNorm(float &dst_dUx, float &dst_dUy, uchar *I0_ptr, uchar *I1_ptr, short *I0x_ptr,
                                    short *I0y_ptr, int I0_stride, int I1_stride, float w00, float w01, float w10,
                                    float w11, int patch_sz, float x_grad_sum, float y_grad_sum)
  {
      float sum_diff = 0.0, sum_diff_sq = 0.0;
      float sum_I0x_mul = 0.0, sum_I0y_mul = 0.0;
      float n = (float)patch_sz * patch_sz;
  
  #if CV_SIMD128
      if (patch_sz == 8)
      {
          /* Variables to accumulate the sums */
          v_float32x4 sum_I0x_mul_vec = v_setall_f32(0);
          v_float32x4 sum_I0y_mul_vec = v_setall_f32(0);
          v_float32x4 sum_diff_vec = v_setall_f32(0);
          v_float32x4 sum_diff_sq_vec = v_setall_f32(0);
  
          v_int16x8 I0x_row, I0y_row;
          v_int32x4 I0x_row_4_left, I0x_row_4_right, I0y_row_4_left, I0y_row_4_right;
  
          HAL_INIT_BILINEAR_8x8_PATCH_EXTRACTION;
          for (int row = 0; row < 8; row++)
          {
              HAL_PROCESS_BILINEAR_8x8_PATCH_EXTRACTION;
              I0x_row = v_load(I0x_ptr);
              v_expand(I0x_row, I0x_row_4_left, I0x_row_4_right);
              I0y_row = v_load(I0y_ptr);
              v_expand(I0y_row, I0y_row_4_left, I0y_row_4_right);
  
              /* Update the sums: */
              sum_I0x_mul_vec += I_diff_left * v_cvt_f32(I0x_row_4_left) + I_diff_right * v_cvt_f32(I0x_row_4_right);
              sum_I0y_mul_vec += I_diff_left * v_cvt_f32(I0y_row_4_left) + I_diff_right * v_cvt_f32(I0y_row_4_right);
              sum_diff_sq_vec += I_diff_left * I_diff_left + I_diff_right * I_diff_right;
              sum_diff_vec += I_diff_left + I_diff_right;
  
              I0x_ptr += I0_stride;
              I0y_ptr += I0_stride;
              HAL_BILINEAR_8x8_PATCH_EXTRACTION_NEXT_ROW;
          }
  
          /* Final reduce operations: */
          sum_I0x_mul = v_reduce_sum(sum_I0x_mul_vec);
          sum_I0y_mul = v_reduce_sum(sum_I0y_mul_vec);
          sum_diff = v_reduce_sum(sum_diff_vec);
          sum_diff_sq = v_reduce_sum(sum_diff_sq_vec);
      }
      else
  #endif
      {
          float diff;
          for (int i = 0; i < patch_sz; i++)
              for (int j = 0; j < patch_sz; j++)
              {
                  diff = w00 * I1_ptr[i * I1_stride + j] + w01 * I1_ptr[i * I1_stride + j + 1] +
                         w10 * I1_ptr[(i + 1) * I1_stride + j] + w11 * I1_ptr[(i + 1) * I1_stride + j + 1] -
                         I0_ptr[i * I0_stride + j];
  
                  sum_diff += diff;
                  sum_diff_sq += diff * diff;
  
                  sum_I0x_mul += diff * I0x_ptr[i * I0_stride + j];
                  sum_I0y_mul += diff * I0y_ptr[i * I0_stride + j];
              }
      }
      dst_dUx = sum_I0x_mul - sum_diff * x_grad_sum / n;
      dst_dUy = sum_I0y_mul - sum_diff * y_grad_sum / n;
      return sum_diff_sq - sum_diff * sum_diff / n;
  }
  
  /* Similar to processPatch, but compute only the sum of squared differences (SSD) between the patches */
  inline float computeSSD(uchar *I0_ptr, uchar *I1_ptr, int I0_stride, int I1_stride, float w00, float w01, float w10,
                          float w11, int patch_sz)
  {
      float SSD = 0.0f;
  #if CV_SIMD128
      if (patch_sz == 8)
      {
          v_float32x4 SSD_vec = v_setall_f32(0);
          HAL_INIT_BILINEAR_8x8_PATCH_EXTRACTION;
          for (int row = 0; row < 8; row++)
          {
              HAL_PROCESS_BILINEAR_8x8_PATCH_EXTRACTION;
              SSD_vec += I_diff_left * I_diff_left + I_diff_right * I_diff_right;
              HAL_BILINEAR_8x8_PATCH_EXTRACTION_NEXT_ROW;
          }
          SSD = v_reduce_sum(SSD_vec);
      }
      else
  #endif
      {
          float diff;
          for (int i = 0; i < patch_sz; i++)
              for (int j = 0; j < patch_sz; j++)
              {
                  diff = w00 * I1_ptr[i * I1_stride + j] + w01 * I1_ptr[i * I1_stride + j + 1] +
                         w10 * I1_ptr[(i + 1) * I1_stride + j] + w11 * I1_ptr[(i + 1) * I1_stride + j + 1] -
                         I0_ptr[i * I0_stride + j];
                  SSD += diff * diff;
              }
      }
      return SSD;
  }
  
  /* Same as computeSSD, but with patch mean normalization */
  inline float computeSSDMeanNorm(uchar *I0_ptr, uchar *I1_ptr, int I0_stride, int I1_stride, float w00, float w01,
                                  float w10, float w11, int patch_sz)
  {
      float sum_diff = 0.0f, sum_diff_sq = 0.0f;
      float n = (float)patch_sz * patch_sz;
  #if CV_SIMD128
      if (patch_sz == 8)
      {
          v_float32x4 sum_diff_vec = v_setall_f32(0);
          v_float32x4 sum_diff_sq_vec = v_setall_f32(0);
          HAL_INIT_BILINEAR_8x8_PATCH_EXTRACTION;
          for (int row = 0; row < 8; row++)
          {
              HAL_PROCESS_BILINEAR_8x8_PATCH_EXTRACTION;
              sum_diff_sq_vec += I_diff_left * I_diff_left + I_diff_right * I_diff_right;
              sum_diff_vec += I_diff_left + I_diff_right;
              HAL_BILINEAR_8x8_PATCH_EXTRACTION_NEXT_ROW;
          }
          sum_diff = v_reduce_sum(sum_diff_vec);
          sum_diff_sq = v_reduce_sum(sum_diff_sq_vec);
      }
      else
      {
  #endif
          float diff;
          for (int i = 0; i < patch_sz; i++)
              for (int j = 0; j < patch_sz; j++)
              {
                  diff = w00 * I1_ptr[i * I1_stride + j] + w01 * I1_ptr[i * I1_stride + j + 1] +
                         w10 * I1_ptr[(i + 1) * I1_stride + j] + w11 * I1_ptr[(i + 1) * I1_stride + j + 1] -
                         I0_ptr[i * I0_stride + j];
  
                  sum_diff += diff;
                  sum_diff_sq += diff * diff;
              }
  #if CV_SIMD128
      }
  #endif
      return sum_diff_sq - sum_diff * sum_diff / n;
  }
  
  #undef HAL_INIT_BILINEAR_8x8_PATCH_EXTRACTION
  #undef HAL_PROCESS_BILINEAR_8x8_PATCH_EXTRACTION
  #undef HAL_BILINEAR_8x8_PATCH_EXTRACTION_NEXT_ROW
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  void DISOpticalFlowImpl::PatchInverseSearch_ParBody::operator()(const Range &range) const
  {
      CV_INSTRUMENT_REGION();
  
      // force separate processing of stripes if we are using spatial propagation:
      if (dis->use_spatial_propagation && range.end > range.start + 1)
      {
          for (int n = range.start; n < range.end; n++)
              (*this)(Range(n, n + 1));
          return;
      }
      int psz = dis->patch_size;
      int psz2 = psz / 2;
      int w_ext = dis->w + 2 * dis->border_size; //!< width of I1_ext
      int bsz = dis->border_size;
  
      /* Input dense flow */
      float *Ux_ptr = Ux->ptr<float>();
      float *Uy_ptr = Uy->ptr<float>();
  
      /* Output sparse flow */
      float *Sx_ptr = Sx->ptr<float>();
      float *Sy_ptr = Sy->ptr<float>();
  
      uchar *I0_ptr = I0->ptr<uchar>();
      uchar *I1_ptr = I1->ptr<uchar>();
      short *I0x_ptr = I0x->ptr<short>();
      short *I0y_ptr = I0y->ptr<short>();
  
      /* Precomputed structure tensor */
      float *xx_ptr = dis->I0xx_buf.ptr<float>();
      float *yy_ptr = dis->I0yy_buf.ptr<float>();
      float *xy_ptr = dis->I0xy_buf.ptr<float>();
      /* And extra buffers for mean-normalization: */
      float *x_ptr = dis->I0x_buf.ptr<float>();
      float *y_ptr = dis->I0y_buf.ptr<float>();
  
      bool use_temporal_candidates = false;
      float *initial_Ux_ptr = NULL, *initial_Uy_ptr = NULL;
      if (!dis->initial_Ux.empty())
      {
          initial_Ux_ptr = dis->initial_Ux[pyr_level].ptr<float>();
          initial_Uy_ptr = dis->initial_Uy[pyr_level].ptr<float>();
          use_temporal_candidates = true;
      }
  
      int i, j, dir;
      int start_is, end_is, start_js, end_js;
      int start_i, start_j;
      float i_lower_limit = bsz - psz + 1.0f;
      float i_upper_limit = bsz + dis->h - 1.0f;
      float j_lower_limit = bsz - psz + 1.0f;
      float j_upper_limit = bsz + dis->w - 1.0f;
      float dUx, dUy, i_I1, j_I1, w00, w01, w10, w11, dx, dy;
  
  #define INIT_BILINEAR_WEIGHTS(Ux, Uy) \
      i_I1 = min(max(i + Uy + bsz, i_lower_limit), i_upper_limit); \
      j_I1 = min(max(j + Ux + bsz, j_lower_limit), j_upper_limit); \
      { \
          float di = i_I1 - floor(i_I1); \
          float dj = j_I1 - floor(j_I1); \
          w11 = di       * dj; \
          w10 = di       * (1 - dj); \
          w01 = (1 - di) * dj; \
          w00 = (1 - di) * (1 - dj); \
      }
  
  #define COMPUTE_SSD(dst, Ux, Uy)                                                                                       \
      INIT_BILINEAR_WEIGHTS(Ux, Uy);                                                                                     \
      if (dis->use_mean_normalization)                                                                                   \
          dst = computeSSDMeanNorm(I0_ptr + i * dis->w + j, I1_ptr + (int)i_I1 * w_ext + (int)j_I1, dis->w, w_ext, w00,  \
                                   w01, w10, w11, psz);                                                                  \
      else                                                                                                               \
          dst = computeSSD(I0_ptr + i * dis->w + j, I1_ptr + (int)i_I1 * w_ext + (int)j_I1, dis->w, w_ext, w00, w01,     \
                           w10, w11, psz);
  
      int num_inner_iter = (int)floor(dis->grad_descent_iter / (float)num_iter);
      for (int iter = 0; iter < num_iter; iter++)
      {
          if (iter % 2 == 0)
          {
              dir = 1;
              start_is = min(range.start * stripe_sz, hs);
              end_is = min(range.end * stripe_sz, hs);
              start_js = 0;
              end_js = dis->ws;
              start_i = start_is * dis->patch_stride;
              start_j = 0;
          }
          else
          {
              dir = -1;
              start_is = min(range.end * stripe_sz, hs) - 1;
              end_is = min(range.start * stripe_sz, hs) - 1;
              start_js = dis->ws - 1;
              end_js = -1;
              start_i = start_is * dis->patch_stride;
              start_j = (dis->ws - 1) * dis->patch_stride;
          }
  
          i = start_i;
          for (int is = start_is; dir * is < dir * end_is; is += dir)
          {
              j = start_j;
              for (int js = start_js; dir * js < dir * end_js; js += dir)
              {
                  if (iter == 0)
                  {
                      /* Using result form the previous pyramid level as the very first approximation: */
                      Sx_ptr[is * dis->ws + js] = Ux_ptr[(i + psz2) * dis->w + j + psz2];
                      Sy_ptr[is * dis->ws + js] = Uy_ptr[(i + psz2) * dis->w + j + psz2];
                  }
  
                  float min_SSD = INF, cur_SSD;
                  if (use_temporal_candidates || dis->use_spatial_propagation)
                  {
                      COMPUTE_SSD(min_SSD, Sx_ptr[is * dis->ws + js], Sy_ptr[is * dis->ws + js]);
                  }
  
                  if (use_temporal_candidates)
                  {
                      /* Try temporal candidates (vectors from the initial flow field that was passed to the function) */
                      COMPUTE_SSD(cur_SSD, initial_Ux_ptr[(i + psz2) * dis->w + j + psz2],
                                  initial_Uy_ptr[(i + psz2) * dis->w + j + psz2]);
                      if (cur_SSD < min_SSD)
                      {
                          min_SSD = cur_SSD;
                          Sx_ptr[is * dis->ws + js] = initial_Ux_ptr[(i + psz2) * dis->w + j + psz2];
                          Sy_ptr[is * dis->ws + js] = initial_Uy_ptr[(i + psz2) * dis->w + j + psz2];
                      }
                  }
  
                  if (dis->use_spatial_propagation)
                  {
                      /* Try spatial candidates: */
                      if (dir * js > dir * start_js)
                      {
                          COMPUTE_SSD(cur_SSD, Sx_ptr[is * dis->ws + js - dir], Sy_ptr[is * dis->ws + js - dir]);
                          if (cur_SSD < min_SSD)
                          {
                              min_SSD = cur_SSD;
                              Sx_ptr[is * dis->ws + js] = Sx_ptr[is * dis->ws + js - dir];
                              Sy_ptr[is * dis->ws + js] = Sy_ptr[is * dis->ws + js - dir];
                          }
                      }
                      /* Flow vectors won't actually propagate across different stripes, which is the reason for keeping
                       * the number of stripes constant. It works well enough in practice and doesn't introduce any
                       * visible seams.
                       */
                      if (dir * is > dir * start_is)
                      {
                          COMPUTE_SSD(cur_SSD, Sx_ptr[(is - dir) * dis->ws + js], Sy_ptr[(is - dir) * dis->ws + js]);
                          if (cur_SSD < min_SSD)
                          {
                              min_SSD = cur_SSD;
                              Sx_ptr[is * dis->ws + js] = Sx_ptr[(is - dir) * dis->ws + js];
                              Sy_ptr[is * dis->ws + js] = Sy_ptr[(is - dir) * dis->ws + js];
                          }
                      }
                  }
  
                  /* Use the best candidate as a starting point for the gradient descent: */
                  float cur_Ux = Sx_ptr[is * dis->ws + js];
                  float cur_Uy = Sy_ptr[is * dis->ws + js];
  
                  /* Computing the inverse of the structure tensor: */
                  float detH = xx_ptr[is * dis->ws + js] * yy_ptr[is * dis->ws + js] -
                               xy_ptr[is * dis->ws + js] * xy_ptr[is * dis->ws + js];
                  if (abs(detH) < EPS)
                      detH = EPS;
                  float invH11 = yy_ptr[is * dis->ws + js] / detH;
                  float invH12 = -xy_ptr[is * dis->ws + js] / detH;
                  float invH22 = xx_ptr[is * dis->ws + js] / detH;
                  float prev_SSD = INF, SSD;
                  float x_grad_sum = x_ptr[is * dis->ws + js];
                  float y_grad_sum = y_ptr[is * dis->ws + js];
  
                  for (int t = 0; t < num_inner_iter; t++)
                  {
                      INIT_BILINEAR_WEIGHTS(cur_Ux, cur_Uy);
                      if (dis->use_mean_normalization)
                          SSD = processPatchMeanNorm(dUx, dUy,
                                  I0_ptr  + i * dis->w + j, I1_ptr + (int)i_I1 * w_ext + (int)j_I1,
                                  I0x_ptr + i * dis->w + j, I0y_ptr + i * dis->w + j,
                                  dis->w, w_ext, w00, w01, w10, w11, psz,
                                  x_grad_sum, y_grad_sum);
                      else
                          SSD = processPatch(dUx, dUy,
                                  I0_ptr  + i * dis->w + j, I1_ptr + (int)i_I1 * w_ext + (int)j_I1,
                                  I0x_ptr + i * dis->w + j, I0y_ptr + i * dis->w + j,
                                  dis->w, w_ext, w00, w01, w10, w11, psz);
  
                      dx = invH11 * dUx + invH12 * dUy;
                      dy = invH12 * dUx + invH22 * dUy;
                      cur_Ux -= dx;
                      cur_Uy -= dy;
  
                      /* Break when patch distance stops decreasing */
                      if (SSD >= prev_SSD)
                          break;
                      prev_SSD = SSD;
                  }
  
                  /* If gradient descent converged to a flow vector that is very far from the initial approximation
                   * (more than patch size) then we don't use it. Noticeably improves the robustness.
                   */
                  if (norm(Vec2f(cur_Ux - Sx_ptr[is * dis->ws + js], cur_Uy - Sy_ptr[is * dis->ws + js])) <= psz)
                  {
                      Sx_ptr[is * dis->ws + js] = cur_Ux;
                      Sy_ptr[is * dis->ws + js] = cur_Uy;
                  }
                  j += dir * dis->patch_stride;
              }
              i += dir * dis->patch_stride;
          }
      }
  #undef INIT_BILINEAR_WEIGHTS
  #undef COMPUTE_SSD
  }
  
  DISOpticalFlowImpl::Densification_ParBody::Densification_ParBody(DISOpticalFlowImpl &_dis, int _nstripes, int _h,
                                                                   Mat &dst_Ux, Mat &dst_Uy, Mat &src_Sx, Mat &src_Sy,
                                                                   Mat &_I0, Mat &_I1)
      : dis(&_dis), nstripes(_nstripes), h(_h), Ux(&dst_Ux), Uy(&dst_Uy), Sx(&src_Sx), Sy(&src_Sy), I0(&_I0), I1(&_I1)
  {
      stripe_sz = (int)ceil(h / (double)nstripes);
  }
  
  /* This function transforms a sparse optical flow field obtained by PatchInverseSearch (which computes flow values
   * on a sparse grid defined by patch_stride) into a dense optical flow field by weighted averaging of values from the
   * overlapping patches.
   */
  void DISOpticalFlowImpl::Densification_ParBody::operator()(const Range &range) const
  {
      CV_INSTRUMENT_REGION();
  
      int start_i = min(range.start * stripe_sz, h);
      int end_i = min(range.end * stripe_sz, h);
  
      /* Input sparse flow */
      float *Sx_ptr = Sx->ptr<float>();
      float *Sy_ptr = Sy->ptr<float>();
  
      /* Output dense flow */
      float *Ux_ptr = Ux->ptr<float>();
      float *Uy_ptr = Uy->ptr<float>();
  
      uchar *I0_ptr = I0->ptr<uchar>();
      uchar *I1_ptr = I1->ptr<uchar>();
  
      int psz = dis->patch_size;
      int pstr = dis->patch_stride;
      int i_l, i_u;
      int j_l, j_u;
      float i_m, j_m, diff;
  
      /* These values define the set of sparse grid locations that contain patches overlapping with the current dense flow
       * location */
      int start_is, end_is;
      int start_js, end_js;
  
  /* Some helper macros for updating this set of sparse grid locations */
  #define UPDATE_SPARSE_I_COORDINATES                                                                                    \
      if (i % pstr == 0 && i + psz <= h)                                                                                 \
          end_is++;                                                                                                      \
      if (i - psz >= 0 && (i - psz) % pstr == 0 && start_is < end_is)                                                    \
          start_is++;
  
  #define UPDATE_SPARSE_J_COORDINATES                                                                                    \
      if (j % pstr == 0 && j + psz <= dis->w)                                                                            \
          end_js++;                                                                                                      \
      if (j - psz >= 0 && (j - psz) % pstr == 0 && start_js < end_js)                                                    \
          start_js++;
  
      start_is = 0;
      end_is = -1;
      for (int i = 0; i < start_i; i++)
      {
          UPDATE_SPARSE_I_COORDINATES;
      }
      for (int i = start_i; i < end_i; i++)
      {
          UPDATE_SPARSE_I_COORDINATES;
          start_js = 0;
          end_js = -1;
          for (int j = 0; j < dis->w; j++)
          {
              UPDATE_SPARSE_J_COORDINATES;
              float coef, sum_coef = 0.0f;
              float sum_Ux = 0.0f;
              float sum_Uy = 0.0f;
  
              /* Iterate through all the patches that overlap the current location (i,j) */
              for (int is = start_is; is <= end_is; is++)
                  for (int js = start_js; js <= end_js; js++)
                  {
                      j_m = min(max(j + Sx_ptr[is * dis->ws + js], 0.0f), dis->w - 1.0f - EPS);
                      i_m = min(max(i + Sy_ptr[is * dis->ws + js], 0.0f), dis->h - 1.0f - EPS);
                      j_l = (int)j_m;
                      j_u = j_l + 1;
                      i_l = (int)i_m;
                      i_u = i_l + 1;
                      diff = (j_m - j_l) * (i_m - i_l) * I1_ptr[i_u * dis->w + j_u] +
                             (j_u - j_m) * (i_m - i_l) * I1_ptr[i_u * dis->w + j_l] +
                             (j_m - j_l) * (i_u - i_m) * I1_ptr[i_l * dis->w + j_u] +
                             (j_u - j_m) * (i_u - i_m) * I1_ptr[i_l * dis->w + j_l] - I0_ptr[i * dis->w + j];
                      coef = 1 / max(1.0f, abs(diff));
                      sum_Ux += coef * Sx_ptr[is * dis->ws + js];
                      sum_Uy += coef * Sy_ptr[is * dis->ws + js];
                      sum_coef += coef;
                  }
              CV_DbgAssert(sum_coef != 0);
              Ux_ptr[i * dis->w + j] = sum_Ux / sum_coef;
              Uy_ptr[i * dis->w + j] = sum_Uy / sum_coef;
          }
      }
  #undef UPDATE_SPARSE_I_COORDINATES
  #undef UPDATE_SPARSE_J_COORDINATES
  }
  
  #ifdef HAVE_OPENCL
  bool DISOpticalFlowImpl::ocl_PatchInverseSearch(UMat &src_U,
                                                  UMat &I0, UMat &I1, UMat &I0x, UMat &I0y, int num_iter, int /*pyr_level*/)
  {
      CV_INSTRUMENT_REGION();
      CV_INSTRUMENT_REGION_OPENCL();
  
      size_t globalSize[] = {(size_t)ws, (size_t)hs};
      size_t localSize[]  = {16, 16};
      int num_inner_iter = (int)floor(grad_descent_iter / (float)num_iter);
  
      String subgroups_build_options;
      if (ocl::Device::getDefault().isExtensionSupported("cl_khr_subgroups"))
          subgroups_build_options = " -DCV_USE_SUBGROUPS=1";
  
      String build_options = cv::format(
                  "-DDIS_BORDER_SIZE=%d -DDIS_PATCH_SIZE=%d -DDIS_PATCH_STRIDE=%d",
                  border_size, patch_size, patch_stride
              ) + subgroups_build_options;
  
  #if 0 // OpenCL debug
  u_Sx = Scalar::all(0);
  u_Sy = Scalar::all(0);
  #endif
  
      CV_Assert(num_iter == 2);
      for (int iter = 0; iter < num_iter; iter++)
      {
          if (iter == 0)
          {
              ocl::Kernel k1("dis_patch_inverse_search_fwd_1", ocl::video::dis_flow_oclsrc, build_options);
              size_t global_sz[] = {(size_t)hs * 8};
              size_t local_sz[]  = {8};
  
              k1.args(
                  ocl::KernelArg::PtrReadOnly(src_U),
                  ocl::KernelArg::PtrReadOnly(I0),
                  ocl::KernelArg::PtrReadOnly(I1),
                  (int)w, (int)h, (int)ws, (int)hs,
                  ocl::KernelArg::PtrWriteOnly(u_S)
              );
              if (!k1.run(1, global_sz, local_sz, false))
                  return false;
  
              ocl::Kernel k2("dis_patch_inverse_search_fwd_2", ocl::video::dis_flow_oclsrc, build_options);
  
              k2.args(
                  ocl::KernelArg::PtrReadOnly(src_U),
                  ocl::KernelArg::PtrReadOnly(I0),
                  ocl::KernelArg::PtrReadOnly(I1),
                  ocl::KernelArg::PtrReadOnly(I0x),
                  ocl::KernelArg::PtrReadOnly(I0y),
                  ocl::KernelArg::PtrReadOnly(u_I0xx_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0yy_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0xy_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0x_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0y_buf),
                  (int)w, (int)h, (int)ws, (int)hs,
                  (int)num_inner_iter,
                  ocl::KernelArg::PtrReadWrite(u_S)
              );
              if (!k2.run(2, globalSize, localSize, false))
                  return false;
          }
          else
          {
              ocl::Kernel k3("dis_patch_inverse_search_bwd_1", ocl::video::dis_flow_oclsrc, build_options);
              size_t global_sz[] = {(size_t)hs * 8};
              size_t local_sz[]  = {8};
  
              k3.args(
                  ocl::KernelArg::PtrReadOnly(I0),
                  ocl::KernelArg::PtrReadOnly(I1),
                  (int)w, (int)h, (int)ws, (int)hs,
                  ocl::KernelArg::PtrReadWrite(u_S)
              );
              if (!k3.run(1, global_sz, local_sz, false))
                  return false;
  
              ocl::Kernel k4("dis_patch_inverse_search_bwd_2", ocl::video::dis_flow_oclsrc, build_options);
  
              k4.args(
                  ocl::KernelArg::PtrReadOnly(I0),
                  ocl::KernelArg::PtrReadOnly(I1),
                  ocl::KernelArg::PtrReadOnly(I0x),
                  ocl::KernelArg::PtrReadOnly(I0y),
                  ocl::KernelArg::PtrReadOnly(u_I0xx_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0yy_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0xy_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0x_buf),
                  ocl::KernelArg::PtrReadOnly(u_I0y_buf),
                  (int)w, (int)h,(int)ws, (int)hs,
                  (int)num_inner_iter,
                  ocl::KernelArg::PtrReadWrite(u_S)
              );
              if (!k4.run(2, globalSize, localSize, false))
                  return false;
          }
      }
      return true;
  }
  
  bool DISOpticalFlowImpl::ocl_Densification(UMat &dst_U, UMat &src_S, UMat &_I0, UMat &_I1)
  {
      CV_INSTRUMENT_REGION();
      CV_INSTRUMENT_REGION_OPENCL();
  
      size_t globalSize[] = {(size_t)w, (size_t)h};
      size_t localSize[]  = {16, 16};
  
      String build_options = cv::format(
                  "-DDIS_PATCH_SIZE=%d -DDIS_PATCH_STRIDE=%d",
                  patch_size, patch_stride
              );
  
      ocl::Kernel kernel("dis_densification", ocl::video::dis_flow_oclsrc, build_options);
      kernel.args(
          ocl::KernelArg::PtrReadOnly(src_S),
          ocl::KernelArg::PtrReadOnly(_I0),
          ocl::KernelArg::PtrReadOnly(_I1),
          (int)w, (int)h, (int)ws,
          ocl::KernelArg::PtrWriteOnly(dst_U)
      );
      return kernel.run(2, globalSize, localSize, false);
  }
  
  void DISOpticalFlowImpl::ocl_prepareBuffers(UMat &I0, UMat &I1, InputArray flow, bool use_flow)
  {
      CV_INSTRUMENT_REGION();
      // not pure OpenCV code: CV_INSTRUMENT_REGION_OPENCL();
  
      u_I0s.resize(coarsest_scale + 1);
      u_I1s.resize(coarsest_scale + 1);
      u_I1s_ext.resize(coarsest_scale + 1);
      u_I0xs.resize(coarsest_scale + 1);
      u_I0ys.resize(coarsest_scale + 1);
      u_U.resize(coarsest_scale + 1);
  
      if (use_flow)
      {
          u_initial_U.resize(coarsest_scale + 1);
      }
  
      int fraction = 1;
      int cur_rows = 0, cur_cols = 0;
  
      for (int i = 0; i <= coarsest_scale; i++)
      {
          CV_TRACE_REGION("coarsest_scale_iteration");
          /* Avoid initializing the pyramid levels above the finest scale, as they won't be used anyway */
          if (i == finest_scale)
          {
              cur_rows = I0.rows / fraction;
              cur_cols = I0.cols / fraction;
              u_I0s[i].create(cur_rows, cur_cols, CV_8UC1);
              resize(I0, u_I0s[i], u_I0s[i].size(), 0.0, 0.0, INTER_AREA);
              u_I1s[i].create(cur_rows, cur_cols, CV_8UC1);
              resize(I1, u_I1s[i], u_I1s[i].size(), 0.0, 0.0, INTER_AREA);
  
              /* These buffers are reused in each scale so we initialize them once on the finest scale: */
              u_S.create(cur_rows / patch_stride, cur_cols / patch_stride, CV_32FC2);
              u_I0xx_buf.create(cur_rows / patch_stride, cur_cols / patch_stride, CV_32FC1);
              u_I0yy_buf.create(cur_rows / patch_stride, cur_cols / patch_stride, CV_32FC1);
              u_I0xy_buf.create(cur_rows / patch_stride, cur_cols / patch_stride, CV_32FC1);
              u_I0x_buf.create(cur_rows / patch_stride, cur_cols / patch_stride, CV_32FC1);
              u_I0y_buf.create(cur_rows / patch_stride, cur_cols / patch_stride, CV_32FC1);
  
              u_I0xx_buf_aux.create(cur_rows, cur_cols / patch_stride, CV_32FC1);
              u_I0yy_buf_aux.create(cur_rows, cur_cols / patch_stride, CV_32FC1);
              u_I0xy_buf_aux.create(cur_rows, cur_cols / patch_stride, CV_32FC1);
              u_I0x_buf_aux.create(cur_rows, cur_cols / patch_stride, CV_32FC1);
              u_I0y_buf_aux.create(cur_rows, cur_cols / patch_stride, CV_32FC1);
          }
          else if (i > finest_scale)
          {
              cur_rows = u_I0s[i - 1].rows / 2;
              cur_cols = u_I0s[i - 1].cols / 2;
              u_I0s[i].create(cur_rows, cur_cols, CV_8UC1);
              resize(u_I0s[i - 1], u_I0s[i], u_I0s[i].size(), 0.0, 0.0, INTER_AREA);
              u_I1s[i].create(cur_rows, cur_cols, CV_8UC1);
              resize(u_I1s[i - 1], u_I1s[i], u_I1s[i].size(), 0.0, 0.0, INTER_AREA);
          }
  
          if (i >= finest_scale)
          {
              u_I1s_ext[i].create(cur_rows + 2 * border_size, cur_cols + 2 * border_size, CV_8UC1);
              copyMakeBorder(u_I1s[i], u_I1s_ext[i], border_size, border_size, border_size, border_size, BORDER_REPLICATE);
              u_I0xs[i].create(cur_rows, cur_cols, CV_16SC1);
              u_I0ys[i].create(cur_rows, cur_cols, CV_16SC1);
              spatialGradient(u_I0s[i], u_I0xs[i], u_I0ys[i]);
              u_U[i].create(cur_rows, cur_cols, CV_32FC2);
              variational_refinement_processors[i]->setAlpha(variational_refinement_alpha);
              variational_refinement_processors[i]->setDelta(variational_refinement_delta);
              variational_refinement_processors[i]->setGamma(variational_refinement_gamma);
              variational_refinement_processors[i]->setSorIterations(5);
              variational_refinement_processors[i]->setFixedPointIterations(variational_refinement_iter);
  
              if (use_flow)
              {
                  UMat resized_flow;
                  resize(flow, resized_flow, Size(cur_cols, cur_rows));
                  float scale = 1.0f / fraction;
                  resized_flow.convertTo(u_initial_U[i], CV_32FC2, scale, 0.0f);
              }
          }
  
          fraction *= 2;
      }
  }
  
  bool DISOpticalFlowImpl::ocl_precomputeStructureTensor(UMat &dst_I0xx, UMat &dst_I0yy, UMat &dst_I0xy,
                                                         UMat &dst_I0x, UMat &dst_I0y, UMat &I0x, UMat &I0y)
  {
      CV_INSTRUMENT_REGION();
      CV_INSTRUMENT_REGION_OPENCL();
  
      size_t globalSizeX[] = {(size_t)h};
      size_t localSizeX[]  = {16};
  
  #if 0 // OpenCL debug
      u_I0xx_buf_aux = Scalar::all(0);
      u_I0yy_buf_aux = Scalar::all(0);
      u_I0xy_buf_aux = Scalar::all(0);
      u_I0x_buf_aux = Scalar::all(0);
      u_I0y_buf_aux = Scalar::all(0);
      dst_I0xx = Scalar::all(0);
      dst_I0yy = Scalar::all(0);
      dst_I0xy = Scalar::all(0);
      dst_I0x = Scalar::all(0);
      dst_I0y = Scalar::all(0);
  #endif
  
      String build_options = cv::format(
                  "-DDIS_PATCH_SIZE=%d -DDIS_PATCH_STRIDE=%d",
                  patch_size, patch_stride
              );
  
      ocl::Kernel kernelX("dis_precomputeStructureTensor_hor", ocl::video::dis_flow_oclsrc, build_options);
      kernelX.args(
          ocl::KernelArg::PtrReadOnly(I0x),
          ocl::KernelArg::PtrReadOnly(I0y),
          (int)w, (int)h, (int)ws,
          ocl::KernelArg::PtrWriteOnly(u_I0xx_buf_aux),
          ocl::KernelArg::PtrWriteOnly(u_I0yy_buf_aux),
          ocl::KernelArg::PtrWriteOnly(u_I0xy_buf_aux),
          ocl::KernelArg::PtrWriteOnly(u_I0x_buf_aux),
          ocl::KernelArg::PtrWriteOnly(u_I0y_buf_aux)
      );
      if (!kernelX.run(1, globalSizeX, localSizeX, false))
          return false;
  
      size_t globalSizeY[] = {(size_t)ws};
      size_t localSizeY[]  = {16};
  
      ocl::Kernel kernelY("dis_precomputeStructureTensor_ver", ocl::video::dis_flow_oclsrc, build_options);
      kernelY.args(
          ocl::KernelArg::PtrReadOnly(u_I0xx_buf_aux),
          ocl::KernelArg::PtrReadOnly(u_I0yy_buf_aux),
          ocl::KernelArg::PtrReadOnly(u_I0xy_buf_aux),
          ocl::KernelArg::PtrReadOnly(u_I0x_buf_aux),
          ocl::KernelArg::PtrReadOnly(u_I0y_buf_aux),
          (int)w, (int)h, (int)ws,
          ocl::KernelArg::PtrWriteOnly(dst_I0xx),
          ocl::KernelArg::PtrWriteOnly(dst_I0yy),
          ocl::KernelArg::PtrWriteOnly(dst_I0xy),
          ocl::KernelArg::PtrWriteOnly(dst_I0x),
          ocl::KernelArg::PtrWriteOnly(dst_I0y)
      );
      return kernelY.run(1, globalSizeY, localSizeY, false);
  }
  
  bool DISOpticalFlowImpl::ocl_calc(InputArray I0, InputArray I1, InputOutputArray flow)
  {
      CV_INSTRUMENT_REGION();
      // not pure OpenCV code: CV_INSTRUMENT_REGION_OPENCL();
  
      UMat I0Mat = I0.getUMat();
      UMat I1Mat = I1.getUMat();
      bool use_input_flow = false;
      if (flow.sameSize(I0) && flow.depth() == CV_32F && flow.channels() == 2)
          use_input_flow = true;
      coarsest_scale = min((int)(log(max(I0Mat.cols, I0Mat.rows) / (4.0 * patch_size)) / log(2.0) + 0.5), /* Original code search for maximal movement of width/4 */
                           (int)(log(min(I0Mat.cols, I0Mat.rows) / patch_size) / log(2.0)));              /* Deepest pyramid level greater or equal than patch*/
  
      if (coarsest_scale<0)
          CV_Error(cv::Error::StsBadSize, "The input image must have either width or height >= 12");
  
      if (coarsest_scale<finest_scale)
      {
          // choose the finest level based on coarsest level.
          // Refs: https://github.com/tikroeger/OF_DIS/blob/2c9f2a674f3128d3a41c10e41cc9f3a35bb1b523/run_dense.cpp#L239
          int original_img_width = I0.size().width;
          autoSelectPatchSizeAndScales(original_img_width);
      }
  
      ocl_prepareBuffers(I0Mat, I1Mat, flow, use_input_flow);
      u_U[coarsest_scale].setTo(0.0f);
  
      for (int i = coarsest_scale; i >= finest_scale; i--)
      {
          CV_TRACE_REGION("coarsest_scale_iteration");
          w = u_I0s[i].cols;
          h = u_I0s[i].rows;
          ws = 1 + (w - patch_size) / patch_stride;
          hs = 1 + (h - patch_size) / patch_stride;
  
          if (!ocl_precomputeStructureTensor(u_I0xx_buf, u_I0yy_buf, u_I0xy_buf,
                                             u_I0x_buf, u_I0y_buf, u_I0xs[i], u_I0ys[i]))
              return false;
  
          if (!ocl_PatchInverseSearch(u_U[i], u_I0s[i], u_I1s_ext[i], u_I0xs[i], u_I0ys[i], 2, i))
              return false;
  
          if (!ocl_Densification(u_U[i], u_S, u_I0s[i], u_I1s[i]))
              return false;
  
          if (variational_refinement_iter > 0)
          {
              std::vector<Mat> U_channels;
              split(u_U[i], U_channels); CV_Assert(U_channels.size() == 2);
              variational_refinement_processors[i]->calcUV(u_I0s[i], u_I1s[i],
                      U_channels[0], U_channels[1]);
              merge(U_channels, u_U[i]);
          }
  
          if (i > finest_scale)
          {
              UMat resized;
              resize(u_U[i], resized, u_U[i - 1].size());
              multiply(resized, 2, u_U[i - 1]);
          }
      }
  
      UMat resized_flow;
      resize(u_U[finest_scale], resized_flow, I1Mat.size());
      multiply(resized_flow, 1 << finest_scale, flow);
  
      return true;
  }
  #endif
  
  void DISOpticalFlowImpl::calc(InputArray I0, InputArray I1, InputOutputArray flow)
  {
      CV_INSTRUMENT_REGION();
  
      CV_Assert(!I0.empty() && I0.depth() == CV_8U && I0.channels() == 1);
      CV_Assert(!I1.empty() && I1.depth() == CV_8U && I1.channels() == 1);
      CV_Assert(I0.sameSize(I1));
      CV_Assert(I0.isContinuous());
      CV_Assert(I1.isContinuous());
  
      CV_OCL_RUN(flow.isUMat() &&
                 (patch_size == 8) && (use_spatial_propagation == true),
                 ocl_calc(I0, I1, flow));
  
      Mat I0Mat = I0.getMat();
      Mat I1Mat = I1.getMat();
      bool use_input_flow = false;
      if (flow.sameSize(I0) && flow.depth() == CV_32F && flow.channels() == 2)
          use_input_flow = true;
      else
          flow.create(I1Mat.size(), CV_32FC2);
      Mat flowMat = flow.getMat();
      coarsest_scale = min((int)(log(max(I0Mat.cols, I0Mat.rows) / (4.0 * patch_size)) / log(2.0) + 0.5), /* Original code search for maximal movement of width/4 */
                           (int)(log(min(I0Mat.cols, I0Mat.rows) / patch_size) / log(2.0)));              /* Deepest pyramid level greater or equal than patch*/
  
      if (coarsest_scale<0)
          CV_Error(cv::Error::StsBadSize, "The input image must have either width or height >= 12");
  
      if (coarsest_scale<finest_scale)
      {
          // choose the finest level based on coarsest level.
          // Refs: https://github.com/tikroeger/OF_DIS/blob/2c9f2a674f3128d3a41c10e41cc9f3a35bb1b523/run_dense.cpp#L239
          int original_img_width = I0.size().width;
          autoSelectPatchSizeAndScales(original_img_width);
      }
  
      int num_stripes = getNumThreads();
  
      prepareBuffers(I0Mat, I1Mat, flowMat, use_input_flow);
      Ux[coarsest_scale].setTo(0.0f);
      Uy[coarsest_scale].setTo(0.0f);
  
      for (int i = coarsest_scale; i >= finest_scale; i--)
      {
          CV_TRACE_REGION("coarsest_scale_iteration");
          w = I0s[i].cols;
          h = I0s[i].rows;
          ws = 1 + (w - patch_size) / patch_stride;
          hs = 1 + (h - patch_size) / patch_stride;
  
          precomputeStructureTensor(I0xx_buf, I0yy_buf, I0xy_buf, I0x_buf, I0y_buf, I0xs[i], I0ys[i]);
          if (use_spatial_propagation)
          {
              /* Use a fixed number of stripes regardless the number of threads to make inverse search
               * with spatial propagation reproducible
               */
              parallel_for_(Range(0, 8), PatchInverseSearch_ParBody(*this, 8, hs, Sx, Sy, Ux[i], Uy[i], I0s[i],
                                                                    I1s_ext[i], I0xs[i], I0ys[i], 2, i));
          }
          else
          {
              parallel_for_(Range(0, num_stripes),
                            PatchInverseSearch_ParBody(*this, num_stripes, hs, Sx, Sy, Ux[i], Uy[i], I0s[i], I1s_ext[i],
                                                       I0xs[i], I0ys[i], 1, i));
          }
  
          parallel_for_(Range(0, num_stripes),
                        Densification_ParBody(*this, num_stripes, I0s[i].rows, Ux[i], Uy[i], Sx, Sy, I0s[i], I1s[i]));
          if (variational_refinement_iter > 0)
              variational_refinement_processors[i]->calcUV(I0s[i], I1s[i], Ux[i], Uy[i]);
  
          if (i > finest_scale)
          {
              resize(Ux[i], Ux[i - 1], Ux[i - 1].size());
              resize(Uy[i], Uy[i - 1], Uy[i - 1].size());
              Ux[i - 1] *= 2;
              Uy[i - 1] *= 2;
          }
      }
      Mat uxy[] = {Ux[finest_scale], Uy[finest_scale]};
      merge(uxy, 2, U);
      resize(U, flowMat, flowMat.size());
      flowMat *= 1 << finest_scale;
  }
  
  void DISOpticalFlowImpl::collectGarbage()
  {
      CV_INSTRUMENT_REGION();
  
      I0s.clear();
      I1s.clear();
      I1s_ext.clear();
      I0xs.clear();
      I0ys.clear();
      Ux.clear();
      Uy.clear();
      U.release();
      Sx.release();
      Sy.release();
      I0xx_buf.release();
      I0yy_buf.release();
      I0xy_buf.release();
      I0xx_buf_aux.release();
      I0yy_buf_aux.release();
      I0xy_buf_aux.release();
  
  #ifdef HAVE_OPENCL
      u_I0s.clear();
      u_I1s.clear();
      u_I1s_ext.clear();
      u_I0xs.clear();
      u_I0ys.clear();
      u_U.clear();
      u_S.release();
      u_I0xx_buf.release();
      u_I0yy_buf.release();
      u_I0xy_buf.release();
      u_I0xx_buf_aux.release();
      u_I0yy_buf_aux.release();
      u_I0xy_buf_aux.release();
  #endif
  
      for (int i = finest_scale; i <= coarsest_scale; i++)
          variational_refinement_processors[i]->collectGarbage();
      variational_refinement_processors.clear();
  }
  
  Ptr<DISOpticalFlow> DISOpticalFlow::create(int preset)
  {
      CV_INSTRUMENT_REGION();
  
      Ptr<DISOpticalFlow> dis = makePtr<DISOpticalFlowImpl>();
      dis->setPatchSize(8);
      if (preset == DISOpticalFlow::PRESET_ULTRAFAST)
      {
          dis->setFinestScale(2);
          dis->setPatchStride(4);
          dis->setGradientDescentIterations(12);
          dis->setVariationalRefinementIterations(0);
      }
      else if (preset == DISOpticalFlow::PRESET_FAST)
      {
          dis->setFinestScale(2);
          dis->setPatchStride(4);
          dis->setGradientDescentIterations(16);
          dis->setVariationalRefinementIterations(5);
      }
      else if (preset == DISOpticalFlow::PRESET_MEDIUM)
      {
          dis->setFinestScale(1);
          dis->setPatchStride(3);
          dis->setGradientDescentIterations(25);
          dis->setVariationalRefinementIterations(5);
      }
  
      return dis;
  }
  
  
  } // namespace