Переглянути джерело

fix(alignment): fill rotation gaps black like the reference image; compare the whole template in Fast mode

The reference image is rotated in the browser (edit_reference.html), which
exports the uncovered canvas area as black. The firmware rotated the live
image with CRotateImage and filled the same area with white. The alignment
markers are cut from the reference image, so a marker that reaches into the
gap was compared against the opposite colour at its true position. With a
sideways mounted camera (InitialRotate around +-90 degrees) the gap is two
bands of about 80 px on each side of the frame, the squared difference of
255 per pixel there outweighs the real match, and the marker is found a few
pixels off wherever its black part lands on something dark. The image is
then slightly rotated and shifted and the digit ROIs cut half digits.

Rotate(), RotateAntiAliasing() and Translate() now fill with black, so
existing reference images and markers work as they are.

CFindTemplate::CalculateSimularities() (AlignmentAlgo = Fast) looped over
the size of the search area plus one instead of the template size. It read
past the end of the template buffer and compared pixels that are not part
of the marker. It now walks the template and rejects a stored position at
which the template would not fit into the image.

Fixes #4169

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Victor Gladkikh 1 місяць тому
батько
коміт
e044abb774

+ 16 - 7
code/components/jomjol_image_proc/CFindTemplate.cpp

@@ -62,15 +62,15 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
         oh_stop = height - tpl_height;
         oh_stop = height - tpl_height;
     oh = oh_stop - oh_start + 1;
     oh = oh_stop - oh_start + 1;
 
 
-    float avg, SAD;
-    int min, max;
+    float avg = 0, SAD = 0;
+    int min = 0, max = 0;
     bool isSimilar = false;
     bool isSimilar = false;
 
 
 //    ESP_LOGD(TAG, "FindTemplate 02");
 //    ESP_LOGD(TAG, "FindTemplate 02");
 
 
     if ((_ref->alignment_algo == 2) && (_ref->fastalg_x > -1) && (_ref->fastalg_y > -1))     // für Testzwecke immer Berechnen
     if ((_ref->alignment_algo == 2) && (_ref->fastalg_x > -1) && (_ref->fastalg_y > -1))     // für Testzwecke immer Berechnen
     {
     {
-        isSimilar = CalculateSimularities(rgb_template, _ref->fastalg_x, _ref->fastalg_y, ow, oh, min, avg, max, SAD, _ref->fastalg_SAD, _ref->fastalg_SAD_criteria);
+        isSimilar = CalculateSimularities(rgb_template, _ref->fastalg_x, _ref->fastalg_y, min, avg, max, SAD, _ref->fastalg_SAD, _ref->fastalg_SAD_criteria);
 /*#ifdef DEBUG_DETAIL_ON
 /*#ifdef DEBUG_DETAIL_ON
         std::string zw = "\t" + _ref->image_file + "\tt1_x_y:\t" + std::to_string(_ref->fastalg_x) + "\t" + std::to_string(_ref->fastalg_y);
         std::string zw = "\t" + _ref->image_file + "\tt1_x_y:\t" + std::to_string(_ref->fastalg_x) + "\t" + std::to_string(_ref->fastalg_y);
         zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(min) + "\t" + std::to_string(avg) + "\t" + std::to_string(max) + "\t"+ std::to_string(SAD);
         zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(min) + "\t" + std::to_string(avg) + "\t" + std::to_string(max) + "\t"+ std::to_string(SAD);
@@ -146,7 +146,7 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
 
 
 
 
     if (_ref->alignment_algo == 2)
     if (_ref->alignment_algo == 2)
-        CalculateSimularities(rgb_template, _ref->found_x, _ref->found_y, ow, oh, min, avg, max, SAD, _ref->fastalg_SAD, _ref->fastalg_SAD_criteria);
+        CalculateSimularities(rgb_template, _ref->found_x, _ref->found_y, min, avg, max, SAD, _ref->fastalg_SAD, _ref->fastalg_SAD_criteria);
 
 
 
 
 //    ESP_LOGD(TAG, "FindTemplate 07");
 //    ESP_LOGD(TAG, "FindTemplate 07");
@@ -175,7 +175,9 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
 
 
 
 
 
 
-bool CFindTemplate::CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _starty, int _sizex, int _sizey, int &min, float &avg, int &max, float &SAD, float _SADold, float _SADcrit)
+// Compares the template with the image at (_startx, _starty) and reports whether the match is still as
+// good as the stored one. Used by the "Fast" alignment to skip the full search.
+bool CFindTemplate::CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _starty, int &min, float &avg, int &max, float &SAD, float _SADold, float _SADcrit)
 {
 {
     int dif;
     int dif;
     int minDif = 255;
     int minDif = 255;
@@ -186,8 +188,15 @@ bool CFindTemplate::CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _
 
 
     int xouter, youter, _ch;
     int xouter, youter, _ch;
 
 
-    for (xouter = 0; xouter <= _sizex; xouter++)
-        for (youter = 0; youter <= _sizey; ++youter)
+    // The template has to lie completely inside the image at this position. The stored position comes
+    // from /config/align.txt and may belong to an older reference image.
+    if ((_startx < 0) || (_starty < 0) || ((_startx + tpl_width) > width) || ((_starty + tpl_height) > height))
+        return false;
+
+    // Walk the template, not the search area: the former loop ran over the search area size (plus one)
+    // and so read past the end of the template buffer and compared pixels that are not part of the marker.
+    for (xouter = 0; xouter < tpl_width; xouter++)
+        for (youter = 0; youter < tpl_height; ++youter)
         {
         {
             stbi_uc* p_org = rgb_image + (channels * ((youter + _starty) * width + (xouter + _startx)));
             stbi_uc* p_org = rgb_image + (channels * ((youter + _starty) * width + (xouter + _startx)));
             stbi_uc* p_tpl = _rgb_tmpl + (channels * (youter * tpl_width + xouter));
             stbi_uc* p_tpl = _rgb_tmpl + (channels * (youter * tpl_width + xouter));

+ 1 - 1
code/components/jomjol_image_proc/CFindTemplate.h

@@ -36,7 +36,7 @@ class CFindTemplate : public CImageBasis
 
 
         bool FindTemplate(RefInfo *_ref);
         bool FindTemplate(RefInfo *_ref);
 
 
-        bool CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _starty, int _sizex, int _sizey, int &min, float &avg, int &max, float &SAD, float _SADold, float _SADcrit);
+        bool CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _starty, int &min, float &avg, int &max, float &SAD, float _SADold, float _SADcrit);
 };
 };
 
 
 #endif //CFINDTEMPLATE_H
 #endif //CFINDTEMPLATE_H

+ 9 - 3
code/components/jomjol_image_proc/CRotateImage.cpp

@@ -4,6 +4,12 @@
 
 
 static const char *TAG = "C ROTATE IMG";
 static const char *TAG = "C ROTATE IMG";
 
 
+// Value written to target pixels whose source lies outside the image (the gaps left by a rotation or
+// translation). It has to be black: the reference image is rotated in the browser (edit_reference.html),
+// which exports the uncovered canvas area as black. The alignment markers are cut from that reference,
+// so a marker that reaches into the gap would otherwise be matched against the wrong colour (#4169).
+static const stbi_uc FILL_VALUE = 0;
+
 CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_temp, bool _flip) : CImageBasis(_name)
 CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_temp, bool _flip) : CImageBasis(_name)
 {
 {
     rgb_image = _org->rgb_image;
     rgb_image = _org->rgb_image;
@@ -104,7 +110,7 @@ void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
             else
             else
             {
             {
                 for (int _channels = 0; _channels < channels; ++_channels)
                 for (int _channels = 0; _channels < channels; ++_channels)
-                    p_target[_channels] = 255;
+                    p_target[_channels] = FILL_VALUE;
             }
             }
         }
         }
 
 
@@ -226,7 +232,7 @@ void CRotateImage::RotateAntiAliasing(float _angle, int _centerx, int _centery)
             else
             else
             {
             {
                 for (int _channels = 0; _channels < channels; ++_channels)
                 for (int _channels = 0; _channels < channels; ++_channels)
-                    p_target[_channels] = 255;
+                    p_target[_channels] = FILL_VALUE;
             }
             }
         }
         }
 
 
@@ -299,7 +305,7 @@ void CRotateImage::Translate(int _dx, int _dy)
             else
             else
             {
             {
                 for (int _channels = 0; _channels < channels; ++_channels)
                 for (int _channels = 0; _channels < channels; ++_channels)
-                    p_target[_channels] = 255;
+                    p_target[_channels] = FILL_VALUE;
             }
             }
         }
         }