37 lines
1.5 KiB
C#
37 lines
1.5 KiB
C#
namespace OcrDaemon;
|
|
|
|
using System.Drawing;
|
|
using OpenCvSharp;
|
|
using OpenCvSharp.Extensions;
|
|
|
|
static class ImagePreprocessor
|
|
{
|
|
/// <summary>
|
|
/// 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 → 2x upscale
|
|
/// </summary>
|
|
public static Bitmap PreprocessForOcr(Bitmap src)
|
|
{
|
|
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
|
|
// Kernel size 25x25 captures text strokes, suppresses dim background text
|
|
using var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(25, 25));
|
|
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);
|
|
|
|
// 2x upscale for better LSTM recognition
|
|
using var upscaled = new Mat();
|
|
Cv2.Resize(binary, upscaled, new OpenCvSharp.Size(binary.Width * 2, binary.Height * 2),
|
|
interpolation: InterpolationFlags.Cubic);
|
|
|
|
return BitmapConverter.ToBitmap(upscaled);
|
|
}
|
|
}
|