/
flow.c
749 lines (662 loc) · 27.3 KB
/
flow.c
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
/****************************************************************************
*
* Copyright (C) 2013 PX4 Development Team. All rights reserved.
* Author: Petri Tanskanen <tpetri@inf.ethz.ch>
* Lorenz Meier <lm@inf.ethz.ch>
* Samuel Zihlmann <samuezih@ee.ethz.ch>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions 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.
* 3. Neither the name PX4 nor the names of its contributors may 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include "no_warnings.h"
#include "mavlink_bridge_header.h"
#include <mavlink.h>
#include "dcmi.h"
#include "debug.h"
#define __INLINE inline
#define __ASM asm
#include "core_cm4_simd.h"
#define FRAME_SIZE global_data.param[PARAM_IMAGE_WIDTH]
#define SEARCH_SIZE global_data.param[PARAM_MAX_FLOW_PIXEL] // maximum offset to search: 4 + 1/2 pixels
#define TILE_SIZE 8 // x & y tile size
#define NUM_BLOCKS 5 // x & y number of tiles to check
#define sign(x) (( x > 0 ) - ( x < 0 ))
uint8_t compute_flow(uint8_t *image1, uint8_t *image2, float x_rate, float y_rate, float z_rate, float *pixel_flow_x, float *pixel_flow_y);
// compliments of Adam Williams
#define ABSDIFF(frame1, frame2) \
({ \
int result = 0; \
asm volatile( \
"mov %[result], #0\n" /* accumulator */ \
\
"ldr r4, [%[src], #0]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #0]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #4]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #4]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 1)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 1)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 1 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 1 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 2)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 2)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 2 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 2 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 3)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 3)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 3 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 3 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 4)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 4 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 4 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 5)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 5)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 5 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 5 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 6)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 6)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 6 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 6 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
"ldr r4, [%[src], #(64 * 7)]\n" /* read data from address + offset*/ \
"ldr r5, [%[dst], #(64 * 7)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
"ldr r4, [%[src], #(64 * 7 + 4)]\n" /* read data from address + offset */ \
"ldr r5, [%[dst], #(64 * 7 + 4)]\n" \
"usada8 %[result], r4, r5, %[result]\n" /* difference */ \
\
: [result] "+r" (result) \
: [src] "r" (frame1), [dst] "r" (frame2) \
: "r4", "r5" \
); \
\
result; \
})
/**
* @brief Computes the Hessian at a pixel location
*
* The hessian (second order partial derivatives of the image) is
* a measure of the salience of the image at the appropriate
* box filter scale. It allows to judge wether a pixel
* location is suitable for optical flow calculation.
*
* @param image the array holding pixel data
* @param x location of the pixel in x
* @param y location of the pixel in y
*
* @return gradient magnitude
*/
static inline uint32_t compute_hessian_4x6(uint8_t *image, uint16_t x, uint16_t y, uint16_t row_size)
{
// candidate for hessian calculation:
uint16_t off1 = y*row_size + x; // First row of ones
uint16_t off2 = (y+1)*row_size + x; // Second row of ones
uint16_t off3 = (y+2)*row_size + x; // Third row of minus twos
uint16_t off4 = (y+3)*row_size + x; // Third row of minus twos
uint16_t off5 = (y+4)*row_size + x; // Third row of minus twos
uint16_t off6 = (y+5)*row_size + x; // Third row of minus twos
uint32_t magnitude;
// Uncentered for max. performance:
// center pixel is in brackets ()
// 1 1 1 1
// 1 1 1 1
// -2 (-2) -2 -2
// -2 -2 -2 -2
// 1 1 1 1
// 1 1 1 1
magnitude = __UADD8(*((uint32_t*) &image[off1 - 1]), *((uint32_t*) &image[off2 - 1]));
magnitude -= 2*__UADD8(*((uint32_t*) &image[off3 - 1]), *((uint32_t*) &image[off4 - 1]));
magnitude += __UADD8(*((uint32_t*) &image[off5 - 1]), *((uint32_t*) &image[off6 - 1]));
return magnitude;
}
/**
* @brief Compute the average pixel gradient of all horizontal and vertical steps
*
* TODO compute_diff is not appropriate for low-light mode images
*
* @param image ...
* @param offX x coordinate of upper left corner of 8x8 pattern in image
* @param offY y coordinate of upper left corner of 8x8 pattern in image
*/
static inline uint32_t compute_diff(uint8_t *image, uint16_t offX, uint16_t offY, uint16_t row_size)
{
/* calculate position in image buffer */
uint16_t off = (offY + 2) * row_size + (offX + 2); // we calc only the 4x4 pattern
uint32_t acc;
/* calc row diff */
acc = __USAD8 (*((uint32_t*) &image[off + 0 + 0 * row_size]), *((uint32_t*) &image[off + 0 + 1 * row_size]));
acc = __USADA8(*((uint32_t*) &image[off + 0 + 1 * row_size]), *((uint32_t*) &image[off + 0 + 2 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image[off + 0 + 2 * row_size]), *((uint32_t*) &image[off + 0 + 3 * row_size]), acc);
/* we need to get columns */
uint32_t col1 = (image[off + 0 + 0 * row_size] << 24) | image[off + 0 + 1 * row_size] << 16 | image[off + 0 + 2 * row_size] << 8 | image[off + 0 + 3 * row_size];
uint32_t col2 = (image[off + 1 + 0 * row_size] << 24) | image[off + 1 + 1 * row_size] << 16 | image[off + 1 + 2 * row_size] << 8 | image[off + 1 + 3 * row_size];
uint32_t col3 = (image[off + 2 + 0 * row_size] << 24) | image[off + 2 + 1 * row_size] << 16 | image[off + 2 + 2 * row_size] << 8 | image[off + 2 + 3 * row_size];
uint32_t col4 = (image[off + 3 + 0 * row_size] << 24) | image[off + 3 + 1 * row_size] << 16 | image[off + 3 + 2 * row_size] << 8 | image[off + 3 + 3 * row_size];
/* calc column diff */
acc = __USADA8(col1, col2, acc);
acc = __USADA8(col2, col3, acc);
acc = __USADA8(col3, col4, acc);
return acc;
}
/**
* @brief Compute SAD distances of subpixel shift of two 8x8 pixel patterns.
*
* @param image1 ...
* @param image2 ...
* @param off1X x coordinate of upper left corner of pattern in image1
* @param off1Y y coordinate of upper left corner of pattern in image1
* @param off2X x coordinate of upper left corner of pattern in image2
* @param off2Y y coordinate of upper left corner of pattern in image2
* @param acc array to store SAD distances for shift in every direction
*/
static inline uint32_t compute_subpixel(uint8_t *image1, uint8_t *image2, uint16_t off1X, uint16_t off1Y, uint16_t off2X, uint16_t off2Y, uint32_t *acc, uint16_t row_size)
{
/* calculate position in image buffer */
uint16_t off1 = off1Y * row_size + off1X; // image1
uint16_t off2 = off2Y * row_size + off2X; // image2
uint32_t s0, s1, s2, s3, s4, s5, s6, s7, t1, t3, t5, t7;
for (uint16_t i = 0; i < 8; i++)
{
acc[i] = 0;
}
/*
* calculate for each pixel in the 8x8 field with upper left corner (off1X / off1Y)
* every iteration is one line of the 8x8 field.
*
* + - + - + - + - + - + - + - + - +
* | | | | | | | | |
* + - + - + - + - + - + - + - + - +
*
*
*/
for (uint16_t i = 0; i < 8; i++)
{
/*
* first column of 4 pixels:
*
* + - + - + - + - + - + - + - + - +
* | x | x | x | x | | | | |
* + - + - + - + - + - + - + - + - +
*
* the 8 s values are from following positions for each pixel (X):
* + - + - + - +
* + 5 7 +
* + - + 6 + - +
* + 4 X 0 +
* + - + 2 + - +
* + 3 1 +
* + - + - + - +
*
* variables (s1, ...) contains all 4 results (32bit -> 4 * 8bit values)
*
*/
/* compute average of two pixel values */
s0 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 1 + (i+0) * row_size])));
s1 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i+1) * row_size]), *((uint32_t*) &image2[off2 + 1 + (i+1) * row_size])));
s2 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 0 + (i+1) * row_size])));
s3 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i+1) * row_size]), *((uint32_t*) &image2[off2 - 1 + (i+1) * row_size])));
s4 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i+0) * row_size]), *((uint32_t*) &image2[off2 - 1 + (i+0) * row_size])));
s5 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i-1) * row_size]), *((uint32_t*) &image2[off2 - 1 + (i-1) * row_size])));
s6 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 0 + (i-1) * row_size])));
s7 = (__UHADD8(*((uint32_t*) &image2[off2 + 0 + (i-1) * row_size]), *((uint32_t*) &image2[off2 + 1 + (i-1) * row_size])));
/* these 4 t values are from the corners around the center pixel */
t1 = (__UHADD8(s0, s1));
t3 = (__UHADD8(s3, s4));
t5 = (__UHADD8(s4, s5));
t7 = (__UHADD8(s7, s0));
/*
* finally we got all 8 subpixels (s0, t1, s2, t3, s4, t5, s6, t7):
* + - + - + - +
* | | | |
* + - 5 6 7 - +
* | 4 X 0 |
* + - 3 2 1 - +
* | | | |
* + - + - + - +
*/
/* fill accumulation vector */
acc[0] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), s0, acc[0]);
acc[1] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), t1, acc[1]);
acc[2] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), s2, acc[2]);
acc[3] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), t3, acc[3]);
acc[4] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), s4, acc[4]);
acc[5] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), t5, acc[5]);
acc[6] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), s6, acc[6]);
acc[7] = __USADA8 ((*((uint32_t*) &image1[off1 + 0 + i * row_size])), t7, acc[7]);
/*
* same for second column of 4 pixels:
*
* + - + - + - + - + - + - + - + - +
* | | | | | x | x | x | x |
* + - + - + - + - + - + - + - + - +
*
*/
s0 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 5 + (i+0) * row_size])));
s1 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i+1) * row_size]), *((uint32_t*) &image2[off2 + 5 + (i+1) * row_size])));
s2 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 4 + (i+1) * row_size])));
s3 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i+1) * row_size]), *((uint32_t*) &image2[off2 + 3 + (i+1) * row_size])));
s4 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 3 + (i+0) * row_size])));
s5 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i-1) * row_size]), *((uint32_t*) &image2[off2 + 3 + (i-1) * row_size])));
s6 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i+0) * row_size]), *((uint32_t*) &image2[off2 + 4 + (i-1) * row_size])));
s7 = (__UHADD8(*((uint32_t*) &image2[off2 + 4 + (i-1) * row_size]), *((uint32_t*) &image2[off2 + 5 + (i-1) * row_size])));
t1 = (__UHADD8(s0, s1));
t3 = (__UHADD8(s3, s4));
t5 = (__UHADD8(s4, s5));
t7 = (__UHADD8(s7, s0));
acc[0] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), s0, acc[0]);
acc[1] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), t1, acc[1]);
acc[2] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), s2, acc[2]);
acc[3] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), t3, acc[3]);
acc[4] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), s4, acc[4]);
acc[5] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), t5, acc[5]);
acc[6] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), s6, acc[6]);
acc[7] = __USADA8 ((*((uint32_t*) &image1[off1 + 4 + i * row_size])), t7, acc[7]);
}
return 0;
}
/**
* @brief Compute SAD of two 8x8 pixel windows.
*
* @param image1 ...
* @param image2 ...
* @param off1X x coordinate of upper left corner of pattern in image1
* @param off1Y y coordinate of upper left corner of pattern in image1
* @param off2X x coordinate of upper left corner of pattern in image2
* @param off2Y y coordinate of upper left corner of pattern in image2
*/
static inline uint32_t compute_sad_8x8(uint8_t *image1, uint8_t *image2, uint16_t off1X, uint16_t off1Y, uint16_t off2X, uint16_t off2Y, uint16_t row_size)
{
/* calculate position in image buffer */
uint16_t off1 = off1Y * row_size + off1X; // image1
uint16_t off2 = off2Y * row_size + off2X; // image2
uint32_t acc;
acc = __USAD8 (*((uint32_t*) &image1[off1 + 0 + 0 * row_size]), *((uint32_t*) &image2[off2 + 0 + 0 * row_size]));
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 0 * row_size]), *((uint32_t*) &image2[off2 + 4 + 0 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 1 * row_size]), *((uint32_t*) &image2[off2 + 0 + 1 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 1 * row_size]), *((uint32_t*) &image2[off2 + 4 + 1 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 2 * row_size]), *((uint32_t*) &image2[off2 + 0 + 2 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 2 * row_size]), *((uint32_t*) &image2[off2 + 4 + 2 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 3 * row_size]), *((uint32_t*) &image2[off2 + 0 + 3 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 3 * row_size]), *((uint32_t*) &image2[off2 + 4 + 3 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 4 * row_size]), *((uint32_t*) &image2[off2 + 0 + 4 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 4 * row_size]), *((uint32_t*) &image2[off2 + 4 + 4 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 5 * row_size]), *((uint32_t*) &image2[off2 + 0 + 5 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 5 * row_size]), *((uint32_t*) &image2[off2 + 4 + 5 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 6 * row_size]), *((uint32_t*) &image2[off2 + 0 + 6 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 6 * row_size]), *((uint32_t*) &image2[off2 + 4 + 6 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 0 + 7 * row_size]), *((uint32_t*) &image2[off2 + 0 + 7 * row_size]), acc);
acc = __USADA8(*((uint32_t*) &image1[off1 + 4 + 7 * row_size]), *((uint32_t*) &image2[off2 + 4 + 7 * row_size]), acc);
return acc;
}
/**
* @brief Computes pixel flow from image1 to image2
*
* Searches the corresponding position in the new image (image2) of max. 64 pixels from the old image (image1)
* and calculates the average offset of all.
*
* @param image1 previous image buffer
* @param image2 current image buffer (new)
* @param x_rate gyro x rate
* @param y_rate gyro y rate
* @param z_rate gyro z rate
*
* @return quality of flow calculation
*/
uint8_t compute_flow(uint8_t *image1, uint8_t *image2, float x_rate, float y_rate, float z_rate, float *pixel_flow_x, float *pixel_flow_y) {
/* constants */
const int16_t winmin = -SEARCH_SIZE;
const int16_t winmax = SEARCH_SIZE;
const uint16_t hist_size = 2*(winmax-winmin+1)+1;
/* variables */
uint16_t pixLo = SEARCH_SIZE + 1;
uint16_t pixHi = FRAME_SIZE - (SEARCH_SIZE + 1) - TILE_SIZE;
uint16_t pixStep = (pixHi - pixLo) / NUM_BLOCKS + 1;
uint16_t i, j;
uint32_t acc[8]; // subpixels
uint16_t histx[hist_size]; // counter for x shift
uint16_t histy[hist_size]; // counter for y shift
int8_t dirsx[64]; // shift directions in x
int8_t dirsy[64]; // shift directions in y
uint8_t subdirs[64]; // shift directions of best subpixels
float meanflowx = 0.0f;
float meanflowy = 0.0f;
uint16_t meancount = 0;
float histflowx = 0.0f;
float histflowy = 0.0f;
/* initialize with 0 */
for (j = 0; j < hist_size; j++) { histx[j] = 0; histy[j] = 0; }
/* iterate over all patterns
*/
for (j = pixLo; j < pixHi; j += pixStep)
{
for (i = pixLo; i < pixHi; i += pixStep)
{
/* test pixel if it is suitable for flow tracking */
uint32_t diff = compute_diff(image1, i, j, (uint16_t) global_data.param[PARAM_IMAGE_WIDTH]);
if (diff < global_data.param[PARAM_BOTTOM_FLOW_FEATURE_THRESHOLD])
{
continue;
}
uint32_t dist = 0xFFFFFFFF; // set initial distance to "infinity"
int8_t sumx = 0;
int8_t sumy = 0;
int8_t ii, jj;
uint8_t *base1 = image1 + j * (uint16_t) global_data.param[PARAM_IMAGE_WIDTH] + i;
for (jj = winmin; jj <= winmax; jj++)
{
uint8_t *base2 = image2 + (j+jj) * (uint16_t) global_data.param[PARAM_IMAGE_WIDTH] + i;
for (ii = winmin; ii <= winmax; ii++)
{
// uint32_t temp_dist = compute_sad_8x8(image1, image2, i, j, i + ii, j + jj, (uint16_t) global_data.param[PARAM_IMAGE_WIDTH]);
uint32_t temp_dist = ABSDIFF(base1, base2 + ii);
if (temp_dist < dist)
{
sumx = ii;
sumy = jj;
dist = temp_dist;
}
}
}
/* acceptance SAD distance threshhold */
if (dist < global_data.param[PARAM_BOTTOM_FLOW_VALUE_THRESHOLD])
{
meanflowx += (float) sumx;
meanflowy += (float) sumy;
compute_subpixel(image1, image2, i, j, i + sumx, j + sumy, acc, (uint16_t) global_data.param[PARAM_IMAGE_WIDTH]);
uint32_t mindist = dist; // best SAD until now
uint8_t mindir = 8; // direction 8 for no direction
for(uint8_t k = 0; k < 8; k++)
{
if (acc[k] < mindist)
{
// SAD becomes better in direction k
mindist = acc[k];
mindir = k;
}
}
dirsx[meancount] = sumx;
dirsy[meancount] = sumy;
subdirs[meancount] = mindir;
meancount++;
/* feed histogram filter*/
uint8_t hist_index_x = 2*sumx + (winmax-winmin+1);
if (subdirs[i] == 0 || subdirs[i] == 1 || subdirs[i] == 7) hist_index_x += 1;
if (subdirs[i] == 3 || subdirs[i] == 4 || subdirs[i] == 5) hist_index_x += -1;
uint8_t hist_index_y = 2*sumy + (winmax-winmin+1);
if (subdirs[i] == 5 || subdirs[i] == 6 || subdirs[i] == 7) hist_index_y += -1;
if (subdirs[i] == 1 || subdirs[i] == 2 || subdirs[i] == 3) hist_index_y += 1;
histx[hist_index_x]++;
histy[hist_index_y]++;
}
}
}
/* create flow image if needed (image1 is not needed anymore)
* -> can be used for debugging purpose
*/
// if (global_data.param[PARAM_USB_SEND_VIDEO] )//&& global_data.param[PARAM_VIDEO_USB_MODE] == FLOW_VIDEO)
// {
//
// for (j = pixLo; j < pixHi; j += pixStep)
// {
// for (i = pixLo; i < pixHi; i += pixStep)
// {
//
// uint32_t diff = compute_diff(image1, i, j, (uint16_t) global_data.param[PARAM_IMAGE_WIDTH]);
// if (diff > global_data.param[PARAM_BOTTOM_FLOW_FEATURE_THRESHOLD])
// {
// image1[j * ((uint16_t) global_data.param[PARAM_IMAGE_WIDTH]) + i] = 255;
// }
//
// }
// }
// }
/* evaluate flow calculation */
if (meancount > 10)
{
meanflowx /= meancount;
meanflowy /= meancount;
int16_t maxpositionx = 0;
int16_t maxpositiony = 0;
uint16_t maxvaluex = 0;
uint16_t maxvaluey = 0;
/* position of maximal histogram peek */
for (j = 0; j < hist_size; j++)
{
if (histx[j] > maxvaluex)
{
maxvaluex = histx[j];
maxpositionx = j;
}
if (histy[j] > maxvaluey)
{
maxvaluey = histy[j];
maxpositiony = j;
}
}
/* check if there is a peak value in histogram */
if (1) //(histx[maxpositionx] > meancount / 6 && histy[maxpositiony] > meancount / 6)
{
if (FLOAT_AS_BOOL(global_data.param[PARAM_BOTTOM_FLOW_HIST_FILTER]))
{
/* use histogram filter peek value */
uint16_t hist_x_min = maxpositionx;
uint16_t hist_x_max = maxpositionx;
uint16_t hist_y_min = maxpositiony;
uint16_t hist_y_max = maxpositiony;
/* x direction */
if (maxpositionx > 1 && maxpositionx < hist_size-2)
{
hist_x_min = maxpositionx - 2;
hist_x_max = maxpositionx + 2;
}
else if (maxpositionx == 0)
{
hist_x_min = maxpositionx;
hist_x_max = maxpositionx + 2;
}
else if (maxpositionx == hist_size-1)
{
hist_x_min = maxpositionx - 2;
hist_x_max = maxpositionx;
}
else if (maxpositionx == 1)
{
hist_x_min = maxpositionx - 1;
hist_x_max = maxpositionx + 2;
}
else if (maxpositionx == hist_size-2)
{
hist_x_min = maxpositionx - 2;
hist_x_max = maxpositionx + 1;
}
/* y direction */
if (maxpositiony > 1 && maxpositiony < hist_size-2)
{
hist_y_min = maxpositiony - 2;
hist_y_max = maxpositiony + 2;
}
else if (maxpositiony == 0)
{
hist_y_min = maxpositiony;
hist_y_max = maxpositiony + 2;
}
else if (maxpositiony == hist_size-1)
{
hist_y_min = maxpositiony - 2;
hist_y_max = maxpositiony;
}
else if (maxpositiony == 1)
{
hist_y_min = maxpositiony - 1;
hist_y_max = maxpositiony + 2;
}
else if (maxpositiony == hist_size-2)
{
hist_y_min = maxpositiony - 2;
hist_y_max = maxpositiony + 1;
}
float hist_x_value = 0.0f;
float hist_x_weight = 0.0f;
float hist_y_value = 0.0f;
float hist_y_weight = 0.0f;
for (uint8_t h = hist_x_min; h < hist_x_max+1; h++)
{
hist_x_value += (float) (h*histx[h]);
hist_x_weight += (float) histx[h];
}
for (uint8_t h = hist_y_min; h<hist_y_max+1; h++)
{
hist_y_value += (float) (h*histy[h]);
hist_y_weight += (float) histy[h];
}
histflowx = (hist_x_value/hist_x_weight - (winmax-winmin+1)) / 2.0f ;
histflowy = (hist_y_value/hist_y_weight - (winmax-winmin+1)) / 2.0f;
}
else
{
/* use average of accepted flow values */
uint32_t meancount_x = 0;
uint32_t meancount_y = 0;
for (uint8_t h = 0; h < meancount; h++)
{
float subdirx = 0.0f;
if (subdirs[h] == 0 || subdirs[h] == 1 || subdirs[h] == 7) subdirx = 0.5f;
if (subdirs[h] == 3 || subdirs[h] == 4 || subdirs[h] == 5) subdirx = -0.5f;
histflowx += (float)dirsx[h] + subdirx;
meancount_x++;
float subdiry = 0.0f;
if (subdirs[h] == 5 || subdirs[h] == 6 || subdirs[h] == 7) subdiry = -0.5f;
if (subdirs[h] == 1 || subdirs[h] == 2 || subdirs[h] == 3) subdiry = 0.5f;
histflowy += (float)dirsy[h] + subdiry;
meancount_y++;
}
histflowx /= meancount_x;
histflowy /= meancount_y;
}
/* compensate rotation */
/* calculate focal_length in pixel */
const float focal_length_px = (global_data.param[PARAM_FOCAL_LENGTH_MM]) / (4.0f * 6.0f) * 1000.0f; //original focal lenght: 12mm pixelsize: 6um, binning 4 enabled
/*
* gyro compensation
* the compensated value is clamped to
* the maximum measurable flow value (param BFLOW_MAX_PIX) +0.5
* (sub pixel flow can add half pixel to the value)
*
* -y_rate gives x flow
* x_rates gives y_flow
*/
if (FLOAT_AS_BOOL(global_data.param[PARAM_BOTTOM_FLOW_GYRO_COMPENSATION]))
{
if(fabsf(y_rate) > global_data.param[PARAM_GYRO_COMPENSATION_THRESHOLD])
{
/* calc pixel of gyro */
float y_rate_pixel = y_rate * (get_time_between_images() / 1000000.0f) * focal_length_px;
float comp_x = histflowx + y_rate_pixel;
/* clamp value to maximum search window size plus half pixel from subpixel search */
if (comp_x < (-SEARCH_SIZE - 0.5f))
*pixel_flow_x = (-SEARCH_SIZE - 0.5f);
else if (comp_x > (SEARCH_SIZE + 0.5f))
*pixel_flow_x = (SEARCH_SIZE + 0.5f);
else
*pixel_flow_x = comp_x;
}
else
{
*pixel_flow_x = histflowx;
}
if(fabsf(x_rate) > global_data.param[PARAM_GYRO_COMPENSATION_THRESHOLD])
{
/* calc pixel of gyro */
float x_rate_pixel = x_rate * (get_time_between_images() / 1000000.0f) * focal_length_px;
float comp_y = histflowy - x_rate_pixel;
/* clamp value to maximum search window size plus/minus half pixel from subpixel search */
if (comp_y < (-SEARCH_SIZE - 0.5f))
*pixel_flow_y = (-SEARCH_SIZE - 0.5f);
else if (comp_y > (SEARCH_SIZE + 0.5f))
*pixel_flow_y = (SEARCH_SIZE + 0.5f);
else
*pixel_flow_y = comp_y;
}
else
{
*pixel_flow_y = histflowy;
}
/* alternative compensation */
// /* compensate y rotation */
// *pixel_flow_x = histflowx + y_rate_pixel;
//
// /* compensate x rotation */
// *pixel_flow_y = histflowy - x_rate_pixel;
} else
{
/* without gyro compensation */
*pixel_flow_x = histflowx;
*pixel_flow_y = histflowy;
}
}
else
{
*pixel_flow_x = 0.0f;
*pixel_flow_y = 0.0f;
return 0;
}
}
else
{
*pixel_flow_x = 0.0f;
*pixel_flow_y = 0.0f;
return 0;
}
/* calc quality */
uint8_t qual = (uint8_t)(meancount * 255 / (NUM_BLOCKS*NUM_BLOCKS));
return qual;
}