← All projects

Team project · Computer vision

TopoStitch: Topology-Aware Gap Completion for Fragmented ECG Traces

Python · OpenCV · scikit-image · SciPy · NumPy

Problem
Thresholded ECG traces break into disconnected fragments
Input
Prepared crops of scanned or photographed ECGs
Key idea
Tangent-aligned cubic bridges between matched endpoints
Guarantee
Every original foreground pixel is kept
Before and after on the full example crop: each color is one disconnected fragment; amber marks the only pixels the repair adds.
Figure 1. Before and after on the example crop. Each color is one connected fragment; amber marks the only pixels the repair adds.

1Problem

Scanned and photographed ECGs often come apart into disconnected fragments once they are thresholded, which blocks any later digitization of the signal. A repair must reconnect fragments that belong to the same trace, must not join fragments that do not, and must leave the observed trace untouched: added pixels should follow the trace’s own direction, width, and pixel grid.

Let \(M\) be the binary foreground mask, \(\mathcal{S}\) its one-pixel skeleton, and \(D\) its distance transform. The output is \(M\cup B\), where \(B\) is a set of bridge pixels chosen so that the number of 8-connected components is minimized subject to \(B\cap M=\varnothing\) and no bridge crossing a third fragment.

2Method

The six stages, shown on a real region of the example crop.
Figure 2. The six stages on a region of the example crop: binarize, skeletonize and label components, estimate endpoint tangents, score and match candidate pairs, draw cubic bridges, merge only new pixels.
Table 1. Pipeline.
StageOperation
1 BinarizeThreshold the prepared crop into the mask \(M\); no later step removes a pixel of \(M\).
2 SkeletonOne-pixel skeleton \(\mathcal{S}\) (scikit-image) and 8-connected components (OpenCV).
3 TangentsEndpoints of \(\mathcal{S}\), each with an outward direction and local width (Eq. 1).
4 MatchScore endpoint pairs from different fragments (Eq. 2); accept greedily with union-find.
5 BridgeJoin each accepted pair with a tangent-aligned cubic Bézier curve (Eq. 3).
6 MergeReject bridges that cross a third fragment or cover trace; snap to the pixel grid (Eq. 4); merge.

2.1 Endpoints and outward tangents

\[\mathcal{E}=\{\,\mathbf{p}\in\mathcal{S}:\ |N_8(\mathbf{p})\cap\mathcal{S}|=1\,\},\qquad \mathbf{u}=\frac{\mathbf{x}-\bar{\mathbf{r}}}{\|\mathbf{x}-\bar{\mathbf{r}}\|},\quad \bar{\mathbf{r}}=\frac{\sum_{\mathbf{q}\in Q}D(\mathbf{q})\,\mathbf{q}}{\sum_{\mathbf{q}\in Q}D(\mathbf{q})}\]

\(Q\) holds the skeleton points that a geodesic walk of length at most \(s=15\) px reaches at 70% or more of its range, so \(\mathbf{u}\) points from the trace body toward the gap. The local width is \(w=\max(1,\,2\,\mathrm{median}\,D-1)\).

2.2 Pair scoring and matching

\[\begin{aligned}\mathbf{d}&=\frac{\mathbf{x}_b-\mathbf{x}_a}{\|\mathbf{x}_b-\mathbf{x}_a\|},\qquad \theta_a=\arccos(\mathbf{u}_a^{\top}\mathbf{d}),\qquad \theta_b=\arccos(-\mathbf{u}_b^{\top}\mathbf{d}),\\ S(a,b)&=\|\mathbf{x}_b-\mathbf{x}_a\|+s\,(\theta_a+\theta_b)\end{aligned}\]

A pair is a candidate only if the gap is at most 65 px, \(\max(\theta_a,\theta_b)\le 110^\circ\), and \(\tfrac12(\theta_a+\theta_b)\le 70^\circ\). Candidates are accepted in order of increasing \(S\); each endpoint is used at most once, and union-find skips pairs whose fragments are already connected.

2.3 Tangent-aligned bridge

\[\begin{aligned}\mathbf{B}(t)&=(1-t)^3\mathbf{x}_a+3(1-t)^2t\,\mathbf{c}_a+3(1-t)t^2\,\mathbf{c}_b+t^3\mathbf{x}_b,\\ \mathbf{c}_{a,b}&=\mathbf{x}_{a,b}+h\,\mathbf{u}_{a,b},\qquad h=\min\!\big(\tfrac13\|\mathbf{x}_b-\mathbf{x}_a\|,\,s\big)\end{aligned}\]

The control points lie on the outward tangents, so the curve leaves \(\mathbf{x}_a\) and arrives at \(\mathbf{x}_b\) along the trace. The stroke width follows \(w(t)=w_a+(w_b-w_a)(3t^2-2t^3)\), so it meets each end without a thin neck (Figure 3).

Geometry of one accepted bridge.
Figure 3. Geometry of one accepted bridge: the control points sit on each outward tangent.

2.4 Matching the source pixel grid

\[\begin{aligned}R(P)&=\frac{\big|\sum_k \omega_k\,e^{2\pi i\,x_k/P}\big|}{\sum_k \omega_k},\qquad P^\star=\arg\max_{2\le P\le 12} R(P),\\ \phi&=\frac{P^\star}{2\pi}\arg\Big(\sum_k \omega_k\,e^{2\pi i\,x_k/P^\star}\Big)\end{aligned}\]

\(x_k\) are positions of foreground edges along one axis, weighted by their counts \(\omega_k\). When \(R(P^\star)\ge 0.78\) on both axes, bridges are resampled onto the detected block lattice (about 5 px on the example); with weaker evidence the native pixels are kept.

3Results

Table 2. Connected components left on the example crop (lower is better; 1 means the trace is fully reconnected).
MethodComponentsNote
Input10–
Row-wise constrained baseline10–
Iterative endpoint variant5–
Component-distance baseline1Adds broad connectors (global thickness estimate)
TopoStitch1Keeps every input pixel; bridge widths match both endpoints

Finding. TopoStitch reduces 10 components to 1 with 9 accepted bridges, none crossing another fragment. All original foreground pixels are preserved and 4,025 pixels are added. The geometry is covered by 13 synthetic regression tests.

Scope: the demonstration covers connectivity on prepared crops; time–voltage calibration and clinical validation are not part of this project.