diff --git a/tools/OcrDaemon/Program.cs b/tools/OcrDaemon/Program.cs index cb8cc2d..44d49aa 100644 --- a/tools/OcrDaemon/Program.cs +++ b/tools/OcrDaemon/Program.cs @@ -280,11 +280,25 @@ void HandleDiffOcr(Request req, TesseractEngine engine) int maxX = Math.Min(bestColEnd + pad, w - 1); int maxY = Math.Min(bestRowEnd + pad, h - 1); - // Trim 5px from left/right/bottom to remove tooltip border/shadow artifacts - int trim = 5; - minX = Math.Min(minX + trim, maxX); - maxX = Math.Max(maxX - trim, minX); - maxY = Math.Max(maxY - trim, minY); + // Dynamic right-edge trim: if the rightmost columns are much sparser than + // the tooltip body, trim them. This handles the ~5% of cases where ambient + // noise extends the detected region slightly on the right. + int colSpan = maxX - minX + 1; + if (colSpan > 100) + { + // Compute median column density in the middle 50% of the range + int q1 = minX + colSpan / 4; + int q3 = minX + colSpan * 3 / 4; + long midSum = 0; + int midCount = 0; + for (int x = q1; x <= q3; x++) { midSum += colCounts[x]; midCount++; } + double avgMidDensity = (double)midSum / midCount; + double cutoff = avgMidDensity * 0.3; // column must have >=30% of avg density + + // Trim from right while below cutoff + while (maxX > minX + 100 && colCounts[maxX] < cutoff) + maxX--; + } int rw = maxX - minX + 1; int rh = maxY - minY + 1;