1
- // Copyright (c) Six Labors.
1
+ // Copyright (c) Six Labors.
2
2
// Licensed under the Apache License, Version 2.0.
3
3
4
- using SixLabors . ImageSharp . Processing . Processors ;
5
4
using SixLabors . ImageSharp . Processing . Processors . Convolution ;
6
5
7
6
namespace SixLabors . ImageSharp . Processing
@@ -12,144 +11,230 @@ namespace SixLabors.ImageSharp.Processing
12
11
public static class DetectEdgesExtensions
13
12
{
14
13
/// <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.
17
16
/// </summary>
18
17
/// <param name="source">The image this method extends.</param>
19
18
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
20
19
public static IImageProcessingContext DetectEdges ( this IImageProcessingContext source ) =>
21
- DetectEdges ( source , new SobelProcessor ( true ) ) ;
20
+ DetectEdges ( source , KnownEdgeDetectorKernels . Sobel ) ;
22
21
23
22
/// <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.
26
25
/// </summary>
27
26
/// <param name="source">The image this method extends.</param>
28
27
/// <param name="rectangle">
29
28
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
30
29
/// </param>
31
30
/// <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 ) ;
34
35
35
36
/// <summary>
36
- /// Detects any edges within the image.
37
+ /// Detects any edges within the image operating in grayscale mode .
37
38
/// </summary>
38
39
/// <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>
40
41
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
41
42
public static IImageProcessingContext DetectEdges (
42
43
this IImageProcessingContext source ,
43
- EdgeDetectionOperators filter ) =>
44
- DetectEdges ( source , GetProcessor ( filter , true ) ) ;
44
+ EdgeDetector2DKernel kernel ) =>
45
+ DetectEdges ( source , kernel , true ) ;
45
46
46
47
/// <summary>
47
- /// Detects any edges within the image.
48
+ /// Detects any edges within the image using a <see cref="EdgeDetector2DKernel"/> .
48
49
/// </summary>
49
50
/// <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>
52
55
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
53
56
public static IImageProcessingContext DetectEdges (
54
57
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
+ }
58
65
59
66
/// <summary>
60
- /// Detects any edges within the image.
67
+ /// Detects any edges within the image operating in grayscale mode .
61
68
/// </summary>
62
69
/// <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>
64
71
/// <param name="rectangle">
65
72
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
66
73
/// </param>
67
- /// <param name="grayscale">Whether to convert the image to grayscale first. Defaults to true.</param>
68
74
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
69
75
public static IImageProcessingContext DetectEdges (
70
76
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 ) ;
75
80
76
81
/// <summary>
77
- /// Detects any edges within the image.
82
+ /// Detects any edges within the image using a <see cref="EdgeDetector2DKernel"/> .
78
83
/// </summary>
79
84
/// <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>
81
92
/// <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 )
83
98
{
84
- return source . ApplyProcessor ( filter ) ;
99
+ var processor = new EdgeDetector2DProcessor ( kernel , grayscale ) ;
100
+ source . ApplyProcessor ( processor , rectangle ) ;
101
+ return source ;
85
102
}
86
103
87
104
/// <summary>
88
- /// Detects any edges within the image.
105
+ /// Detects any edges within the image operating in grayscale mode .
89
106
/// </summary>
90
107
/// <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.
93
122
/// </param>
94
- /// <param name="filter">The filter for detecting edges.</param>
95
123
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
96
- private static IImageProcessingContext DetectEdges (
124
+ public static IImageProcessingContext DetectEdges (
97
125
this IImageProcessingContext source ,
98
- Rectangle rectangle ,
99
- IImageProcessor filter )
126
+ EdgeDetectorKernel kernel ,
127
+ bool grayscale )
100
128
{
101
- source . ApplyProcessor ( filter , rectangle ) ;
129
+ var processor = new EdgeDetectorProcessor ( kernel , grayscale ) ;
130
+ source . ApplyProcessor ( processor ) ;
102
131
return source ;
103
132
}
104
133
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 ) ;
134
148
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
+ }
138
171
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 ) ;
142
182
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
+ }
146
201
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 ) ;
151
216
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 ;
153
238
}
154
239
}
155
- }
240
+ }
0 commit comments