|
|
@@ -8,6 +8,7 @@
|
|
|
#include <esp_timer.h>
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
#include "freertos/task.h"
|
|
|
+#include <cstdint>
|
|
|
|
|
|
static const char* TAG = "C FIND TEMPL";
|
|
|
|
|
|
@@ -97,9 +98,11 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
// ESP_LOGD(TAG, "FindTemplate 04");
|
|
|
|
|
|
|
|
|
- double aktSAD;
|
|
|
- double sadSpan = (double)tpl_width * tpl_height * 255;
|
|
|
- double minSAD = sadSpan * sadSpan;
|
|
|
+ // Sum of squared differences in integer arithmetic: the ESP32 FPU is single precision only,
|
|
|
+ // so the former double/pow() version ran in software emulation and took several seconds per reference.
|
|
|
+ // The result is identical: the sum is exact either way and the search order / tie-breaking is unchanged.
|
|
|
+ uint64_t aktSAD;
|
|
|
+ uint64_t minSAD = UINT64_MAX;
|
|
|
|
|
|
RGBImageLock();
|
|
|
|
|
|
@@ -112,22 +115,30 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
// PSRAM-Zugriffe auf Core 0 verdrängen den WiFi-Stack. Spätestens alle 50 ms
|
|
|
// den Core abgeben, damit Beacons weiter verarbeitet werden (Tick = 10 ms).
|
|
|
int64_t nextYieldUs = esp_timer_get_time() + 50000;
|
|
|
+ const int tpl_row_len = tpl_width * channels;
|
|
|
|
|
|
for (xouter = ow_start; xouter <= ow_stop; xouter++)
|
|
|
for (youter = oh_start; youter <= oh_stop; ++youter)
|
|
|
{
|
|
|
aktSAD = 0;
|
|
|
- for (tpl_x = 0; tpl_x < tpl_width; tpl_x++)
|
|
|
- for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
|
|
|
+ // Walk the template row by row so that image accesses stay sequential in (PSRAM) memory
|
|
|
+ for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
|
|
|
+ {
|
|
|
+ const stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + xouter));
|
|
|
+ const stbi_uc* p_tpl = rgb_template + (tpl_y * tpl_row_len);
|
|
|
+ uint32_t rowSAD = 0;
|
|
|
+ for (tpl_x = 0; tpl_x < tpl_row_len; tpl_x += channels)
|
|
|
{
|
|
|
- stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + (xouter + tpl_x)));
|
|
|
- stbi_uc* p_tpl = rgb_template + (channels * (tpl_y * tpl_width + tpl_x));
|
|
|
for (_ch = 0; _ch < _anzchannels; ++_ch)
|
|
|
{
|
|
|
- int diff = (int)p_tpl[_ch] - (int)p_org[_ch];
|
|
|
- aktSAD += diff * diff;
|
|
|
+ int dif = p_tpl[tpl_x + _ch] - p_org[tpl_x + _ch];
|
|
|
+ rowSAD += dif * dif;
|
|
|
}
|
|
|
}
|
|
|
+ aktSAD += rowSAD;
|
|
|
+ if (aktSAD >= minSAD) // can no longer become the new minimum
|
|
|
+ break;
|
|
|
+ }
|
|
|
if (aktSAD < minSAD)
|
|
|
{
|
|
|
minSAD = aktSAD;
|