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