Răsfoiți Sursa

Merge pull request #4179 from alfeg/fix/alignment-rotation-fill

fix(alignment): fill rotation gaps black like the reference image; fix Fast-mode template comparison
SybexXx 1 lună în urmă
părinte
comite
55de906ce2

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

@@ -62,15 +62,15 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
         oh_stop = height - tpl_height;
     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;
 
 //    ESP_LOGD(TAG, "FindTemplate 02");
 
     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
         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);
@@ -146,7 +146,7 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
 
 
     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");
@@ -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 minDif = 255;
@@ -186,8 +188,15 @@ bool CFindTemplate::CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _
 
     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_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 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

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

@@ -4,6 +4,12 @@
 
 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)
 {
     rgb_image = _org->rgb_image;
@@ -104,7 +110,7 @@ void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
             else
             {
                 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
             {
                 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
             {
                 for (int _channels = 0; _channels < channels; ++_channels)
-                    p_target[_channels] = 255;
+                    p_target[_channels] = FILL_VALUE;
             }
         }