1 /*
2 Copyright (c) 2011-2021 Timur Gafarov, Oleg Baharev
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Detect edges on an image
31  *
32  * Copyright: Timur Gafarov 2011-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.image.filters.edgedetect;
37 
38 import std.math;
39 import dlib.math.vector;
40 import dlib.image.image;
41 import dlib.image.color;
42 import dlib.image.arithmetics;
43 import dlib.image.filters.contrast;
44 import dlib.image.filters.boxblur;
45 import dlib.image.filters.morphology;
46 import dlib.image.filters.convolution;
47 
48 /// Difference of Gaussians
49 SuperImage edgeDetectDoG(SuperImage src, SuperImage outp, int radius1, int radius2, float amount, bool inv = true)
50 {
51     if (outp is null)
52         outp = src.dup;
53 
54     auto blurred1 = boxBlur(src, outp, radius1);
55     SuperImage outp2 = outp.dup;
56     auto blurred2 = boxBlur(src, outp2, radius2);
57 
58     auto mask = subtract(blurred1, blurred2, outp, 1.0f);
59     outp2.free();
60     auto highcon = contrast(mask, mask, amount, ContrastMethod.AverageImage);
61 
62     if (inv)
63         return invert(highcon, highcon);
64     else
65         return highcon;
66 }
67 
68 /// ditto
69 SuperImage edgeDetectDoG(SuperImage src, int radius1, int radius2, float amount, bool inv = true)
70 {
71     return edgeDetectDoG(src, null, radius1, radius2, amount, inv);
72 }
73 
74 /// Morphologic edge detection
75 SuperImage edgeDetectGradient(SuperImage src, SuperImage outp)
76 {
77     if (outp is null)
78         outp = src.dup;
79 
80     return gradient(src, outp);
81 }
82 
83 /// ditto
84 SuperImage edgeDetectGradient(SuperImage src)
85 {
86     return edgeDetectGradient(src, null);
87 }
88 
89 /// Laplace edge detection
90 SuperImage edgeDetectLaplace(SuperImage src, SuperImage outp)
91 {
92     if (outp is null)
93         outp = src.dup;
94 
95     return convolve(src, outp, Kernel.Laplace, 3, 3, 1.0f, 0.0f, false);
96 }
97 
98 /// ditto
99 SuperImage edgeDetectLaplace(SuperImage src)
100 {
101     return edgeDetectLaplace(src, null);
102 }
103 
104 /// Sobel edge detection
105 SuperImage edgeDetectSobel(SuperImage src, SuperImage outp, float normFactor = 1.0f / 8.0f)
106 {
107     if (outp is null)
108         outp = src.dup;
109     
110     enum float[3][3] sobelHorizontal = [
111         [-1,  0,  1],
112         [-2,  0,  2],
113         [-1,  0,  1],
114     ];
115     
116     enum float[3][3] sobelVertical = [
117         [-1, -2, -1],
118         [ 0,  0,  0],
119         [ 1,  2,  1],
120     ];
121     
122     foreach(window, x, y; src.windows(3, 3))
123     {
124         Color4f hor = Color4f(0, 0, 0);
125         Color4f ver = Color4f(0, 0, 0);
126         foreach(ref Color4f pixel, x, y; window)
127         {
128             hor += pixel * sobelHorizontal[y][x];
129             ver += pixel * sobelVertical[y][x];
130         }
131         
132         float magnitude = sqrt(hor.xyz.lengthsqr + ver.xyz.lengthsqr) * normFactor;
133         Color4f res = Color4f(magnitude, magnitude, magnitude, 1.0f);
134         res.a = 1.0f;
135         outp[x, y] = res;
136     }
137     
138     return outp;
139 }
140 
141 /// ditto
142 SuperImage edgeDetectSobel(SuperImage src, float normFactor = 1.0f / 8.0f)
143 {
144     return edgeDetectSobel(src, null, normFactor);
145 }