351 {
352 std::vector<uint8_t> mask;
353 const std::array<int, 4> xywh = n->create_binary_image(mask);
354 const int w = xywh[2];
355 const int h = xywh[3];
356 if (w <= 0 || h <= 0)
357 return 0.0f;
358
359
360
361 const int pw = w + 2;
362 const int ph = h + 2;
363 constexpr float INF = 1e20f;
364
365 std::vector<float> grid(static_cast<size_t>(pw) * ph);
366 for (int y = 0; y < ph; ++y) {
367 for (int x = 0; x < pw; ++x) {
368 const bool inside = x >= 1 && x <= w && y >= 1 && y <= h &&
369 mask[static_cast<size_t>(y - 1) * w + (x - 1)];
370 grid[static_cast<size_t>(y) * pw + x] = inside ? INF : 0.0f;
371 }
372 }
373
374
375 std::vector<float> in, out(std::max(pw, ph));
376 in.resize(ph);
377 for (int x = 0; x < pw; ++x) {
378 for (int y = 0; y < ph; ++y)
379 in[y] = grid[static_cast<size_t>(y) * pw + x];
380 dt_1d(in, out, ph);
381 for (int y = 0; y < ph; ++y)
382 grid[static_cast<size_t>(y) * pw + x] = out[y];
383 }
384 in.resize(pw);
385 float max_d2 = 0.0f;
386 for (int y = 0; y < ph; ++y) {
387 for (int x = 0; x < pw; ++x)
388 in[x] = grid[static_cast<size_t>(y) * pw + x];
389 dt_1d(in, out, pw);
390 for (int x = 0; x < pw; ++x)
391 if (out[x] > max_d2)
392 max_d2 = out[x];
393 }
394 return std::sqrt(max_d2);
395}