@@ -259,43 +259,38 @@ std::pair<std::string, float> rapidstamp_engine::read_layout(const RsImage& crop
259259 const int grids[4 ][2 ] = {{2 , 1 }, {3 , 1 }, {4 , 1 }, {2 , 2 }};
260260 for (const auto & item : grids) {
261261 auto decoded = recognize (grid_unroll (crop, item[0 ], item[1 ], config.rec_h , config.rec_w ));
262- if (decoded.second > best.second ) best = decoded;
262+ float penalty = 0 .05f * std::abs (utf8_len (decoded.first ) - item[0 ] * item[1 ]);
263+ best = prefer_reading (best, {decoded.first , decoded.second - penalty});
263264 }
264265 return best;
265266 }
266267 RsImage source = layout == " circle" ? pad_to_square (crop, 255 ) : crop;
267- struct Reading {
268- float start;
269- std::string text;
270- float score;
268+ std::vector<float > starts;
269+ auto push_unique = [&](float start) {
270+ float wrapped = std::fmod (start, 360 .f );
271+ if (wrapped < 0 .f ) wrapped += 360 .f ;
272+ int key = static_cast <int >(std::lround (wrapped)) % 360 ;
273+ if (key < 0 ) key += 360 ;
274+ for (float existing : starts) {
275+ int other = static_cast <int >(std::lround (existing)) % 360 ;
276+ if (other < 0 ) other += 360 ;
277+ if (other == key) return ;
278+ }
279+ starts.push_back (wrapped);
271280 };
272- std::vector<Reading> readings;
273- int steps = std::max (1 , config.angle_steps );
274- auto push = [&](float start) {
281+ // Eight starts. A 15 degree grid lets a rotated fragment outscore the title.
282+ int steps = config.angle_steps >= 8 ? 8 : std::max (1 , config.angle_steps );
283+ for (int step = 0 ; step < steps; ++step) push_unique (step * (360 .f / static_cast <float >(steps)));
284+ push_unique (config.start_deg );
285+ float guess = 0 .f ;
286+ float gap = 0 .f ;
287+ if (estimate_ring_start (source, config.r_in , config.r_out , guess, gap) && gap >= 40 .f ) push_unique (guess);
288+ std::pair<std::string, float > best{" " , -1 .f };
289+ for (float start : starts) {
275290 auto decoded = recognize (polar_unwrap (source, config.rec_w , config.rec_h , config.r_in , config.r_out , start,
276291 config.span_deg ));
277- readings.push_back (Reading{start, decoded.first , decoded.second });
278- };
279- for (int step = 0 ; step < steps; ++step) push (step * (360 .f / static_cast <float >(steps)));
280- push (config.start_deg );
281- std::vector<Reading> ranked = readings;
282- std::sort (ranked.begin (), ranked.end (), [](const Reading& a, const Reading& b) { return a.score > b.score ; });
283- int refined = 0 ;
284- std::vector<int > seen;
285- for (const Reading& item : ranked) {
286- int key = static_cast <int >(std::lround (item.start )) % 360 ;
287- if (key < 0 ) key += 360 ;
288- if (std::find (seen.begin (), seen.end (), key) != seen.end ()) continue ;
289- seen.push_back (key);
290- refined += 1 ;
291- if (refined > 2 ) break ;
292- for (int delta = -12 ; delta <= 12 ; delta += 3 ) {
293- if (delta == 0 ) continue ;
294- push (item.start + static_cast <float >(delta));
295- }
292+ best = prefer_reading (best, decoded);
296293 }
297- std::pair<std::string, float > best{" " , -1 .f };
298- for (const Reading& item : readings) best = prefer_reading (best, {item.text , item.score });
299294 return best;
300295}
301296
0 commit comments