|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | + |
| 3 | +#ifndef IPTSD_CONTACTS_DETECTION_ALGORITHMS_SUPPRESSION_HPP |
| 4 | +#define IPTSD_CONTACTS_DETECTION_ALGORITHMS_SUPPRESSION_HPP |
| 5 | + |
| 6 | +#include <common/casts.hpp> |
| 7 | +#include <common/types.hpp> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <utility> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace iptsd::contacts::detection::suppression { |
| 14 | + |
| 15 | +/*! |
| 16 | + * Darken pixels around each maxima by a constant factor, while leaving maxima pixels unchanged. |
| 17 | + * |
| 18 | + * This is intended to "sharpen" peaks / deepen valleys between peaks so that cluster spanning |
| 19 | + * is less likely to connect neighboring contacts. |
| 20 | + * |
| 21 | + * Behavior: |
| 22 | + * - out is initialized as a copy of in |
| 23 | + * - for each maxima, all pixels within euclidean radius (excluding the center pixel) are reduced: |
| 24 | + * out(y,x) = min(out(y,x), in(y,x) * factor) |
| 25 | + * - maxima pixels are restored to their original values (exactly unchanged) |
| 26 | + * |
| 27 | + * Notes: |
| 28 | + * - If neighborhoods overlap, the result is still "at most factor" darkening (no stacking), |
| 29 | + * because we always compare against (in * factor), not (out * factor). |
| 30 | + */ |
| 31 | +template <class Derived> |
| 32 | +void darken_around_maximas(const DenseBase<Derived> &in, |
| 33 | + const std::vector<Point> &maximas, |
| 34 | + const Eigen::Index radius, |
| 35 | + const typename DenseBase<Derived>::Scalar factor, |
| 36 | + DenseBase<Derived> &out) |
| 37 | +{ |
| 38 | + using T = typename DenseBase<Derived>::Scalar; |
| 39 | + |
| 40 | + out = in; |
| 41 | + |
| 42 | + if (maximas.empty()) |
| 43 | + return; |
| 44 | + |
| 45 | + if (radius <= 0) |
| 46 | + return; |
| 47 | + |
| 48 | + // Clamp factor to sane range. (factor = 1 => no-op, factor = 0 => nuke neighbors to 0) |
| 49 | + const T f = std::clamp(factor, casts::to<T>(0), casts::to<T>(1)); |
| 50 | + if (f >= casts::to<T>(1)) |
| 51 | + return; |
| 52 | + |
| 53 | + const Eigen::Index cols = in.cols(); |
| 54 | + const Eigen::Index rows = in.rows(); |
| 55 | + |
| 56 | + // Precompute offsets in a disk (euclidean radius). |
| 57 | + const isize r = casts::to_signed(radius); |
| 58 | + const isize r2 = r * r; |
| 59 | + |
| 60 | + std::vector<std::pair<isize, isize>> offsets; |
| 61 | + offsets.reserve(static_cast<size_t>((2 * r + 1) * (2 * r + 1))); |
| 62 | + |
| 63 | + for (isize dy = -r; dy <= r; ++dy) { |
| 64 | + for (isize dx = -r; dx <= r; ++dx) { |
| 65 | + if (dx == 0 && dy == 0) |
| 66 | + continue; |
| 67 | + |
| 68 | + if (((dx * dx) + (dy * dy)) <= r2) |
| 69 | + offsets.emplace_back(dx, dy); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Darken neighbors. |
| 74 | + for (const Point &p : maximas) { |
| 75 | + const Eigen::Index cx = p.x(); |
| 76 | + const Eigen::Index cy = p.y(); |
| 77 | + |
| 78 | + // Skip invalid points defensively (shouldn't happen, but costs nothing). |
| 79 | + if (cx < 0 || cx >= cols || cy < 0 || cy >= rows) |
| 80 | + continue; |
| 81 | + |
| 82 | + for (const auto &[dx, dy] : offsets) { |
| 83 | + const isize nx_s = casts::to_signed(cx) + dx; |
| 84 | + const isize ny_s = casts::to_signed(cy) + dy; |
| 85 | + |
| 86 | + if (nx_s < 0 || ny_s < 0) |
| 87 | + continue; |
| 88 | + |
| 89 | + const Eigen::Index nx = casts::to_eigen(nx_s); |
| 90 | + const Eigen::Index ny = casts::to_eigen(ny_s); |
| 91 | + |
| 92 | + if (nx >= cols || ny >= rows) |
| 93 | + continue; |
| 94 | + |
| 95 | + const T target = in(ny, nx) * f; |
| 96 | + |
| 97 | + // Use min() against (in*factor) to prevent multi-maxima "stacking" darkness. |
| 98 | + out(ny, nx) = std::min(out(ny, nx), target); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Restore maxima pixels exactly. |
| 103 | + for (const Point &p : maximas) { |
| 104 | + const Eigen::Index x = p.x(); |
| 105 | + const Eigen::Index y = p.y(); |
| 106 | + |
| 107 | + if (x < 0 || x >= cols || y < 0 || y >= rows) |
| 108 | + continue; |
| 109 | + |
| 110 | + out(y, x) = in(y, x); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace iptsd::contacts::detection::suppression |
| 115 | + |
| 116 | +#endif // IPTSD_CONTACTS_DETECTION_ALGORITHMS_SUPPRESSION_HPP |
0 commit comments