namespace OcrDaemon; using System.Drawing; using OpenCvSharp; using OpenCvSharp.Extensions; static class ImagePreprocessor { /// /// Pre-process an image for OCR using morphological white top-hat filtering. /// Isolates bright tooltip text, suppresses dim background text visible through overlay. /// Pipeline: grayscale → morphological top-hat → Otsu binary → upscale /// public static Bitmap PreprocessForOcr(Bitmap src, int kernelSize = 25, int upscale = 2) { using var mat = BitmapConverter.ToMat(src); using var gray = new Mat(); Cv2.CvtColor(mat, gray, ColorConversionCodes.BGRA2GRAY); // Morphological white top-hat: isolates bright text on dark background using var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(kernelSize, kernelSize)); using var tophat = new Mat(); Cv2.MorphologyEx(gray, tophat, MorphTypes.TopHat, kernel); // Otsu binarization: automatic threshold, black text on white using var binary = new Mat(); Cv2.Threshold(tophat, binary, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu); // Upscale for better LSTM recognition if (upscale > 1) { using var upscaled = new Mat(); Cv2.Resize(binary, upscaled, new OpenCvSharp.Size(binary.Width * upscale, binary.Height * upscale), interpolation: InterpolationFlags.Cubic); return BitmapConverter.ToBitmap(upscaled); } return BitmapConverter.ToBitmap(binary); } }