This repository has been archived on 2024-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
thesis-src/Ocr/Ocr.Tesseract.Screenshots/ScreenshotProcessor.cs
T
Simon Gruber db6e403c01 fixed GUI tool
2023-10-18 21:22:58 +02:00

65 lines
1.5 KiB
C#

using ImageMagick;
using Ocr.Tesseract.Extensions;
using Ocr.Tesseract.Screenshots.Configuration;
using System.Collections.Generic;
using System.Linq;
namespace Ocr.Tesseract.Screenshots;
/// <inheritdoc />
public class ScreenshotProcessor : ImageProcessor
{
/// <inheritdoc />
public ScreenshotProcessor(ScreenshotProcessorConfiguration configuration)
: base(configuration)
{
}
/// <inheritdoc cref="System.Diagnostics.Process"/>
public override IEnumerable<MagickImage> Process(MagickImage image)
{
var tImage = image.CloneImage();
if (Configuration.EnableResizing)
{
tImage = tImage
.ResizeImage(
2f,
FilterType.Lanczos2Sharp,
PixelInterpolateMethod.Mesh
)
.Resample(300, DensityUnit.PixelsPerInch);
yield return tImage.CloneImage();
}
if (Configuration.EnableThresholding)
{
tImage = tImage
.NormalizeImage()
.RemoveAlpha(MagickColors.White)
.ToGrayscale()
.ThresholdAdaptive(Configuration.ThresholdWidth, Configuration.ThresholdHeight)
.ToBinary();
}
if (Configuration.Border > 0)
{
tImage = tImage.AddBorder(Configuration.Border, MagickColors.White);
}
yield return tImage;
yield return tImage.CloneImage().NegateColors();
}
#region Overrides of Processor<MagickImage,IMagickImageValueProcessorSettings>
/// <inheritdoc />
public override IEnumerable<MagickImage> Process(IEnumerable<MagickImage> inputs)
{
return inputs.SelectMany(Process);
}
#endregion
}