1- // Copyright (c) Six Labors.
1+ // Copyright (c) Six Labors.
22// Licensed under the Apache License, Version 2.0.
33
4- using SixLabors . ImageSharp . Processing . Processors ;
54using SixLabors . ImageSharp . Processing . Processors . Convolution ;
65
76namespace SixLabors . ImageSharp . Processing
@@ -12,144 +11,230 @@ namespace SixLabors.ImageSharp.Processing
1211 public static class DetectEdgesExtensions
1312 {
1413 /// <summary>
15- /// Detects any edges within the image. Uses the <see cref="SobelProcessor"/> filter
16- /// operating in grayscale mode.
14+ /// Detects any edges within the image.
15+ /// Uses the <see cref="KnownEdgeDetectorKernels.Sobel"/> kernel operating in grayscale mode.
1716 /// </summary>
1817 /// <param name="source">The image this method extends.</param>
1918 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2019 public static IImageProcessingContext DetectEdges ( this IImageProcessingContext source ) =>
21- DetectEdges ( source , new SobelProcessor ( true ) ) ;
20+ DetectEdges ( source , KnownEdgeDetectorKernels . Sobel ) ;
2221
2322 /// <summary>
24- /// Detects any edges within the image. Uses the <see cref="SobelProcessor"/> filter
25- /// operating in grayscale mode.
23+ /// Detects any edges within the image.
24+ /// Uses the <see cref="KnownEdgeDetectorKernels.Sobel"/> kernel operating in grayscale mode.
2625 /// </summary>
2726 /// <param name="source">The image this method extends.</param>
2827 /// <param name="rectangle">
2928 /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
3029 /// </param>
3130 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
32- public static IImageProcessingContext DetectEdges ( this IImageProcessingContext source , Rectangle rectangle ) =>
33- DetectEdges ( source , rectangle , new SobelProcessor ( true ) ) ;
31+ public static IImageProcessingContext DetectEdges (
32+ this IImageProcessingContext source ,
33+ Rectangle rectangle ) =>
34+ DetectEdges ( source , KnownEdgeDetectorKernels . Sobel , rectangle ) ;
3435
3536 /// <summary>
36- /// Detects any edges within the image.
37+ /// Detects any edges within the image operating in grayscale mode .
3738 /// </summary>
3839 /// <param name="source">The image this method extends.</param>
39- /// <param name="filter ">The filter for detecting edges .</param>
40+ /// <param name="kernel ">The 2D edge detector kernel .</param>
4041 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
4142 public static IImageProcessingContext DetectEdges (
4243 this IImageProcessingContext source ,
43- EdgeDetectionOperators filter ) =>
44- DetectEdges ( source , GetProcessor ( filter , true ) ) ;
44+ EdgeDetector2DKernel kernel ) =>
45+ DetectEdges ( source , kernel , true ) ;
4546
4647 /// <summary>
47- /// Detects any edges within the image.
48+ /// Detects any edges within the image using a <see cref="EdgeDetector2DKernel"/> .
4849 /// </summary>
4950 /// <param name="source">The image this method extends.</param>
50- /// <param name="filter">The filter for detecting edges.</param>
51- /// <param name="grayscale">Whether to convert the image to grayscale first. Defaults to true.</param>
51+ /// <param name="kernel">The 2D edge detector kernel.</param>
52+ /// <param name="grayscale">
53+ /// Whether to convert the image to grayscale before performing edge detection.
54+ /// </param>
5255 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5356 public static IImageProcessingContext DetectEdges (
5457 this IImageProcessingContext source ,
55- EdgeDetectionOperators filter ,
56- bool grayscale ) =>
57- DetectEdges ( source , GetProcessor ( filter , grayscale ) ) ;
58+ EdgeDetector2DKernel kernel ,
59+ bool grayscale )
60+ {
61+ var processor = new EdgeDetector2DProcessor ( kernel , grayscale ) ;
62+ source . ApplyProcessor ( processor ) ;
63+ return source ;
64+ }
5865
5966 /// <summary>
60- /// Detects any edges within the image.
67+ /// Detects any edges within the image operating in grayscale mode .
6168 /// </summary>
6269 /// <param name="source">The image this method extends.</param>
63- /// <param name="filter ">The filter for detecting edges .</param>
70+ /// <param name="kernel ">The 2D edge detector kernel .</param>
6471 /// <param name="rectangle">
6572 /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
6673 /// </param>
67- /// <param name="grayscale">Whether to convert the image to grayscale first. Defaults to true.</param>
6874 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
6975 public static IImageProcessingContext DetectEdges (
7076 this IImageProcessingContext source ,
71- EdgeDetectionOperators filter ,
72- Rectangle rectangle ,
73- bool grayscale = true ) =>
74- DetectEdges ( source , rectangle , GetProcessor ( filter , grayscale ) ) ;
77+ EdgeDetector2DKernel kernel ,
78+ Rectangle rectangle ) =>
79+ DetectEdges ( source , kernel , true , rectangle ) ;
7580
7681 /// <summary>
77- /// Detects any edges within the image.
82+ /// Detects any edges within the image using a <see cref="EdgeDetector2DKernel"/> .
7883 /// </summary>
7984 /// <param name="source">The image this method extends.</param>
80- /// <param name="filter">The filter for detecting edges.</param>
85+ /// <param name="kernel">The 2D edge detector kernel.</param>
86+ /// <param name="grayscale">
87+ /// Whether to convert the image to grayscale before performing edge detection.
88+ /// </param>
89+ /// <param name="rectangle">
90+ /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
91+ /// </param>
8192 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
82- private static IImageProcessingContext DetectEdges ( this IImageProcessingContext source , IImageProcessor filter )
93+ public static IImageProcessingContext DetectEdges (
94+ this IImageProcessingContext source ,
95+ EdgeDetector2DKernel kernel ,
96+ bool grayscale ,
97+ Rectangle rectangle )
8398 {
84- return source . ApplyProcessor ( filter ) ;
99+ var processor = new EdgeDetector2DProcessor ( kernel , grayscale ) ;
100+ source . ApplyProcessor ( processor , rectangle ) ;
101+ return source ;
85102 }
86103
87104 /// <summary>
88- /// Detects any edges within the image.
105+ /// Detects any edges within the image operating in grayscale mode .
89106 /// </summary>
90107 /// <param name="source">The image this method extends.</param>
91- /// <param name="rectangle">
92- /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
108+ /// <param name="kernel">The edge detector kernel.</param>
109+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
110+ public static IImageProcessingContext DetectEdges (
111+ this IImageProcessingContext source ,
112+ EdgeDetectorKernel kernel ) =>
113+ DetectEdges ( source , kernel , true ) ;
114+
115+ /// <summary>
116+ /// Detects any edges within the image using a <see cref="EdgeDetectorKernel"/>.
117+ /// </summary>
118+ /// <param name="source">The image this method extends.</param>
119+ /// <param name="kernel">The edge detector kernel.</param>
120+ /// <param name="grayscale">
121+ /// Whether to convert the image to grayscale before performing edge detection.
93122 /// </param>
94- /// <param name="filter">The filter for detecting edges.</param>
95123 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
96- private static IImageProcessingContext DetectEdges (
124+ public static IImageProcessingContext DetectEdges (
97125 this IImageProcessingContext source ,
98- Rectangle rectangle ,
99- IImageProcessor filter )
126+ EdgeDetectorKernel kernel ,
127+ bool grayscale )
100128 {
101- source . ApplyProcessor ( filter , rectangle ) ;
129+ var processor = new EdgeDetectorProcessor ( kernel , grayscale ) ;
130+ source . ApplyProcessor ( processor ) ;
102131 return source ;
103132 }
104133
105- private static IImageProcessor GetProcessor ( EdgeDetectionOperators filter , bool grayscale )
106- {
107- IImageProcessor processor ;
108-
109- switch ( filter )
110- {
111- case EdgeDetectionOperators . Kayyali :
112- processor = new KayyaliProcessor ( grayscale ) ;
113- break ;
114-
115- case EdgeDetectionOperators . Kirsch :
116- processor = new KirschProcessor ( grayscale ) ;
117- break ;
118-
119- case EdgeDetectionOperators . Laplacian3x3 :
120- processor = new Laplacian3x3Processor ( grayscale ) ;
121- break ;
122-
123- case EdgeDetectionOperators . Laplacian5x5 :
124- processor = new Laplacian5x5Processor ( grayscale ) ;
125- break ;
126-
127- case EdgeDetectionOperators . LaplacianOfGaussian :
128- processor = new LaplacianOfGaussianProcessor ( grayscale ) ;
129- break ;
130-
131- case EdgeDetectionOperators . Prewitt :
132- processor = new PrewittProcessor ( grayscale ) ;
133- break ;
134+ /// <summary>
135+ /// Detects any edges within the image operating in grayscale mode.
136+ /// </summary>
137+ /// <param name="source">The image this method extends.</param>
138+ /// <param name="kernel">The edge detector kernel.</param>
139+ /// <param name="rectangle">
140+ /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
141+ /// </param>
142+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
143+ public static IImageProcessingContext DetectEdges (
144+ this IImageProcessingContext source ,
145+ EdgeDetectorKernel kernel ,
146+ Rectangle rectangle ) =>
147+ DetectEdges ( source , kernel , true , rectangle ) ;
134148
135- case EdgeDetectionOperators . RobertsCross :
136- processor = new RobertsCrossProcessor ( grayscale ) ;
137- break ;
149+ /// <summary>
150+ /// Detects any edges within the image using a <see cref="EdgeDetectorKernel"/>.
151+ /// </summary>
152+ /// <param name="source">The image this method extends.</param>
153+ /// <param name="kernel">The edge detector kernel.</param>
154+ /// <param name="grayscale">
155+ /// Whether to convert the image to grayscale before performing edge detection.
156+ /// </param>
157+ /// <param name="rectangle">
158+ /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
159+ /// </param>
160+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
161+ public static IImageProcessingContext DetectEdges (
162+ this IImageProcessingContext source ,
163+ EdgeDetectorKernel kernel ,
164+ bool grayscale ,
165+ Rectangle rectangle )
166+ {
167+ var processor = new EdgeDetectorProcessor ( kernel , grayscale ) ;
168+ source . ApplyProcessor ( processor , rectangle ) ;
169+ return source ;
170+ }
138171
139- case EdgeDetectionOperators . Robinson :
140- processor = new RobinsonProcessor ( grayscale ) ;
141- break ;
172+ /// <summary>
173+ /// Detects any edges within the image operating in grayscale mode.
174+ /// </summary>
175+ /// <param name="source">The image this method extends.</param>
176+ /// <param name="kernel">Thecompass edge detector kernel.</param>
177+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
178+ public static IImageProcessingContext DetectEdges (
179+ this IImageProcessingContext source ,
180+ EdgeDetectorCompassKernel kernel ) =>
181+ DetectEdges ( source , kernel , true ) ;
142182
143- case EdgeDetectionOperators . Scharr :
144- processor = new ScharrProcessor ( grayscale ) ;
145- break ;
183+ /// <summary>
184+ /// Detects any edges within the image using a <see cref="EdgeDetectorCompassKernel"/>.
185+ /// </summary>
186+ /// <param name="source">The image this method extends.</param>
187+ /// <param name="kernel">Thecompass edge detector kernel.</param>
188+ /// <param name="grayscale">
189+ /// Whether to convert the image to grayscale before performing edge detection.
190+ /// </param>
191+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
192+ public static IImageProcessingContext DetectEdges (
193+ this IImageProcessingContext source ,
194+ EdgeDetectorCompassKernel kernel ,
195+ bool grayscale )
196+ {
197+ var processor = new EdgeDetectorCompassProcessor ( kernel , grayscale ) ;
198+ source . ApplyProcessor ( processor ) ;
199+ return source ;
200+ }
146201
147- default :
148- processor = new SobelProcessor ( grayscale ) ;
149- break ;
150- }
202+ /// <summary>
203+ /// Detects any edges within the image operating in grayscale mode.
204+ /// </summary>
205+ /// <param name="source">The image this method extends.</param>
206+ /// <param name="kernel">Thecompass edge detector kernel.</param>
207+ /// <param name="rectangle">
208+ /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
209+ /// </param>
210+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
211+ public static IImageProcessingContext DetectEdges (
212+ this IImageProcessingContext source ,
213+ EdgeDetectorCompassKernel kernel ,
214+ Rectangle rectangle ) =>
215+ DetectEdges ( source , kernel , true , rectangle ) ;
151216
152- return processor ;
217+ /// <summary>
218+ /// Detects any edges within the image using a <see cref="EdgeDetectorCompassKernel"/>.
219+ /// </summary>
220+ /// <param name="source">The image this method extends.</param>
221+ /// <param name="kernel">Thecompass edge detector kernel.</param>
222+ /// <param name="grayscale">
223+ /// Whether to convert the image to grayscale before performing edge detection.
224+ /// </param>
225+ /// <param name="rectangle">
226+ /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
227+ /// </param>
228+ /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
229+ public static IImageProcessingContext DetectEdges (
230+ this IImageProcessingContext source ,
231+ EdgeDetectorCompassKernel kernel ,
232+ bool grayscale ,
233+ Rectangle rectangle )
234+ {
235+ var processor = new EdgeDetectorCompassProcessor ( kernel , grayscale ) ;
236+ source . ApplyProcessor ( processor , rectangle ) ;
237+ return source ;
153238 }
154239 }
155- }
240+ }
0 commit comments