mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.9 KiB
49 lines
1.9 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using SixLabors.ImageSharp.Primitives;
|
|
using SixLabors.ImageSharp.Processing.Processors.Filters;
|
|
using SixLabors.Primitives;
|
|
|
|
namespace SixLabors.ImageSharp.Processing.Processors.Convolution
|
|
{
|
|
/// <summary>
|
|
/// Defines a processor that detects edges within an image using a single two dimensional matrix.
|
|
/// </summary>
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
internal class EdgeDetectorProcessor<TPixel> : ImageProcessor<TPixel>
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="EdgeDetectorProcessor{TPixel}"/> class.
|
|
/// </summary>
|
|
/// <param name="kernelXY">The 2d gradient operator.</param>
|
|
/// <param name="grayscale">Whether to convert the image to grayscale before performing edge detection.</param>
|
|
public EdgeDetectorProcessor(in DenseMatrix<float> kernelXY, bool grayscale)
|
|
{
|
|
this.KernelXY = kernelXY;
|
|
this.Grayscale = grayscale;
|
|
}
|
|
|
|
public bool Grayscale { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the 2d gradient operator.
|
|
/// </summary>
|
|
public DenseMatrix<float> KernelXY { get; }
|
|
|
|
/// <inheritdoc/>
|
|
protected override void BeforeFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
|
|
{
|
|
if (this.Grayscale)
|
|
{
|
|
new GrayscaleBt709Processor(1F).ApplyToFrame(source, sourceRectangle, configuration);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
|
|
=> new ConvolutionProcessor<TPixel>(this.KernelXY, true).Apply(source, sourceRectangle, configuration);
|
|
}
|
|
}
|