Professional Documents
Culture Documents
2016 Digital Image Processing 407 427
2016 Digital Image Processing 407 427
at position u = (u, v). The local edge strength of the grayscale image
is then taken as
%
Egray (u) = ∇I(u) = Ix2 (u) + Iy2 (u), (16.3)
The local edge strength is calculated separately for each color channel
which yields a vector
⎛ ⎞ ⎛ ⎞
ER (u) ∇IR (u)
E(u) = ⎝EG (u)⎠ = ⎝∇IG (u)⎠ (16.6)
EB (u) ∇IB (u)
⎛ ⎞
[IR,x
2
(u) + IR,y
2
(u)]1/2
= ⎝[IG,x (16.7)
⎜ 2 ⎟
(u) + IG,y
2
(u)]1/2 ⎠
[IB,x (u) + IB,y (u)]
2 2 1/2
392
for each image position u. These vectors could be combined into 16.1 Monochromatic
a new color image E = (ER , EG , EB ), although such a “color edge Techniques
image” has no particularly useful interpretation.1 Finally, a scalar
quantity of combined edge strength (C) over all color planes can be
obtained, for example, by calculating the Euclidean (L2 ) norm of
E as
2 2
1/2
C2 (u) = E(u)2 = ER (u) + EG (u) + EB2 (u)
2 2 2 2 2 2
1/2
= IR,x + IR,y + IG,x + IG,y + IB,x + IB,y (16.8)
(coordinates (u) are omitted in the second line) or, using the L1
norm,
C∞ (u) = E(u)∞ = max (|ER (u)| , |EG (u)| , |EB (u)|) . (16.10)
m,y (u)
I
Φcol (u) = tan−1 , (16.11)
Im,x (u)
Fig. 16.1
Color edge enhancement (a) (b)
with monochromatic meth-
ods. Original color image (a)
and corresponding grayscale
image (b); edge magnitude
from the grayscale image (c).
Color edge magnitude calcu-
lated with different norms:
L1 (d), L2 (e), and L∞ (f). sRGB Y′
The images in (c–f) are in-
verted for better viewing.
(c) (d)
Egray C1
(e) (f)
C2 C∞
image and then applying the Sobel operator3 (Fig. 16.2(b)). The
edge magnitude in all examples is normalized; it is shown inverted
and contrast-enhanced to increase the visibility of low-contrast edges.
As expected and apparent from the examples, even simple monochro-
matic techniques applied to color images perform better than edge
detection on the corresponding grayscale images. In particular, edges
between color regions of similar brightness are not detectable in this
way, so using color information for edge detection is generally more
powerful than relying on intensity alone. Among the simple color
techniques, the maximum channel edge strength C∞ (Eqn. (16.10))
seems to give the most consistent results with the fewest edges getting
lost.
However, none of the monochromatic detection techniques can
be expected to work reliably under these circumstances. While the
threshold for binarizing the edge magnitude could be tuned manu-
ally to give more pleasing results on specific images, it is difficult
in practice to achieve consistently good results over a wide range of
images. Methods for determining the optimal edge threshold dynam-
3
See Chapter 6, Sec. 6.3.1.
394
1: MonochromaticColorEdge(I) 16.2 Edges in
Input: I = (IR , IG , IB ), an RGB color image of size M ×N . Re- Vector-Valued Images
turns a pair of maps (E2 , Φ) for edge magnitude and orientation.
- −1 0 1
. Alg. 16.1
2: HxS ← 1
8
· −2 0 2 Monochromatic color edge
−1 0 1 operator. A pair of Sobel-
- −1 −2 −1 . type filter kernels (HxS , HyS )
3: HyS ← 1
8
· 0 0 0 ⊲ x/y gradient kernels is used to estimate the local
1 2 1 x/y gradients of each compo-
nent of the RGB input image
4: (M, N ) ← Size(I) I. Color edge magnitude is
5: Create maps E, Φ : M ×N → R ⊲ edge magnitude/orientation calculated as the L2 norm of
the color gradient vector (see
6: IR,x ← IR ∗HxS , IR,y ← IR ∗HyS ⊲ apply gradient filters Eqn. (16.8)). The procedure
7: IG,x ← IG ∗HxS , IG,y ← IG ∗HyS returns a pair of maps, hold-
ing the edge magnitude E2
8: IB,x ← IB ∗HxS , IB,y ← IB ∗HyS and the edge orientation Φ,
respectively.
9: for all image coordinates u ∈ M ×N do
10: (rx , gx , bx ) ← (IR,x (u), IG,x (u), IB,x (u))
11: (ry , gy , by ) ← (IR,y (u), IG,y (u), IB,y (u))
12: e2R ← rx2 + ry2
13: e2G ← gx2 + gy2
14: e2B ← b2x + b2y
15: e2max ← e2R ⊲ find maximum gradient channel
16: cx ← rx , cy ← ry
17: if e2G > e2max then
18: e2max ← e2G , cx ← gx , cy ← gy
19: if e2B > e2max then
20: e2max ← e2B , cx ← bx , cy ← by
21: E(u) ← + e2R
+ e2G e2B ⊲ edge magnitude (L2 norm)
22: Φ(u) ← ArcTan(cx , cy ) ⊲ edge orientation
23: return (E, Φ).
ically, that is, depending on the image content, have been proposed,
typically based on the statistical variability of the color gradients.
Additional details can be found in [84, 171, 192].
4
See Sec. C.2 in the Appendix for some general properties of vector fields.
395
16 Edge Detection in
Color Images
(c)
(d)
(e)
396
16.2.1 Multi-Dimensional Gradients 16.2 Edges in
Vector-Valued Images
As noted in the previous section, the gradient of a scalar image I at
a specific position u is defined as
# $
∂I
∂x (u)
∇I(u) = ∂I , (16.12)
∂y (u)
that is, the vector of the partial derivatives of the function I in the
x- and y-direction, respectively.5 Obviously, the gradient of a scalar
image is a 2D vector field.
In the case of a color image I = (IR , IG , IB ), we can treat the three
color channels as separate scalar images and obtain their gradients
analogously as
# ∂I $ # ∂I $ # ∂I $
∂x (u) ∂x (u) ∂x (u)
R G B
(16.13)
which is equivalent to what we did in Eqn. (16.5). Before we can take
the next steps, we need to introduce a standard tool for the analysis
of vector fields.
⎜ ⊺
⎟ ⎜ ∂I ∂IG
⎟
⎝(∇IG ) (u)⎠ = ⎝ ∂x (u) ∂y (u)⎠ = I x (u) I y (u) ,
JI (u) = ⎜ ⎟ ⎜ G ⎟
(∇IB )⊺ (u) ∂IB ∂IB
∂x (u) ∂y (u) (16.14)
with ∇IR , ∇IG , ∇IB as defined in Eqn. (16.13). We see that the 2D
gradient vectors (∇IR )⊺ , (∇IG )⊺ , (∇IB )⊺ constitute the rows of the
resulting 3 × 2 matrix JI . The two 3D column vectors of this matrix,
⎛ ∂I ⎞ ⎛ ∂I ⎞
∂x (u) ∂y (u)
R R
∂I ⎜ ∂I ⎟ ∂I ⎜ ∂I ⎟
I x (u) = (u) = ⎜ ∂x
G
(u)⎟ , I y (u) = (u) = ⎜ G
∂y (u)⎟
⎠,
∂x ⎝ ⎠ ∂y ⎝
∂IB ∂I
∂x (u) ∂y (u)
B
(16.15)
are the partial derivatives of the color components along the x- and y-
axes, respectively. At a given position u, the total amount of change
over all three color channels in the horizontal direction can be quanti-
fied by the norm of the corresponding column vector I x (u). Anal-
ogously, I y (u) gives the total amount of change over all three color
channels along the vertical axis.
5
Of course, images are discrete functions and the partial derivatives are
estimated from finite differences (see Sec. C.3.1 in the Appendix).
6
See also Sec. C.2.1 in the Appendix.
397
16 Edge Detection in 16.2.3 Squared Local Contrast
Color Images
Now that we can quantify the change along the horizontal and vertical
axes at any position u, the next task is to find out the direction of
the maximum change to find the angle of the edge normal, which
we then use to derive the local edge strength. How can we calculate
the gradient in some direction θ other than horizontal and vertical?
For this purpose, we use the product of the unit vector oriented at
angle θ,
! "
cos(θ)
eθ = , (16.16)
sin(θ)
Fig. 16.3
∇IR Local image gradients and lo-
cal contrast. In case of a scalar
∇I (grayscale) image I (a), the lo-
∇IB cal gradient ∇I defines a single
∇IG plane that is tangential to the
image function I at position
u = (u, v). In case of an RGB
color image I = (IR , IG , IB )
(b), the local gradients ∇IR ,
∇IG , ∇IB for each color
channel define three tangent
(a) (b) planes. The vertical axes in
graphs (c, d) show the corre-
sponding
local contrast values
Sθ (I, u) (see Eqns. (16.18)
and (16.19)) for all possible
directions θ = 0, . . . , 2π.
Sθ (u) Sθ (u)
(c) (d)
or, equivalently, any multiple of q 1 .10 Thus the rate of change along
the vector q 1 is the same as in the opposite direction −q1 , and it
follows that the local contrast Sθ (I, u) at orientation θ is the same
at orientation θ + kπ (for any k ∈ Z).11 As usual, the unit vec-
tor corresponding to q 1 is obtained by scaling q 1 by its magnitude,
that is,
1
q̂ 1 = ·q . (16.29)
q 1 1
An alternative method, proposed in [60], is to calculate the unit
eigenvector q̂ 1 = (x̂1 , ŷ1 )⊺ in the form
% % ⊺
q̂ 1 = 1+α
2 , sgn(C)· 1−α ,
2
(16.30)
with α = (A − B)/ (A − B)2 + 4 C 2 , directly from the matrix ele-
ments A, B, C defined in Eqn. (16.25).
While q 1 (the eigenvector associated with the greater eigenvalue
of M) points in the direction of maximum change, the second eigen-
vector q 2 (associated with λ2 ) is orthogonal to q 1 , that is, has the
same direction as the local edge tangent.
9
See Sec. B.4 in the Appendix for details.
10
The eigenvalues of a matrix are unique, but the corresponding eigenvec-
tors are not.
11
Thus the orientation of maximum change is inherently ambiguous [60].
400
16.2.5 Color Edge Orientation 16.2 Edges in
Vector-Valued Images
The local orientation of the edge (i.e., the normal to the edge tangent)
at a given position u can be obtained directly from the associated
eigenvector q 1 (u) = (qx (u), qy (u))⊺ using the relation
qx (u) 2·C
tan(θ1 (u)) = = , (16.31)
qy (u) A − B + (A − B)2 + 4 · C 2
which can be simplified12 to
2·C
tan(2 · θ1 (u)) = . (16.32)
A−B
Unless both A = B and C = 0 (in which case the edge orientation
is undetermined) the angle of maximum local contrast or color edge
orientation can be calculated as
1 2·C 1
θ1 (u) = · tan−1 = · ArcTan(A − B, 2 · C). (16.33)
2 A−B 2
The above steps are summarized in Alg. 16.2, which is a color edge
operator based on the first derivatives of the image function (see Sec.
16.5 for the corresponding Java implementation). It is similar to the
algorithm proposed by Di Zenzo [63] but uses the eigenvalues of the
local structure matrix for calculating edge magnitude and orientation,
as suggested in [59] (see Eqn. (16.24)).
Results of the monochromatic edge operator in Alg. 16.1 and the
Di Zenzo-Cumani multi-gradient operator in Alg. 16.2 are compared
in Fig. 16.4. The synthetic test image in Fig. 16.4(a) has constant
luminance (brightness) and thus no gray-value operator should be
able to detect edges in this image. The local edge strength E(u)
produced by the two operators is very similar (Fig. 16.4(b)). The
vectors in Fig. 16.4(c–f) show the orientation of the edge tangents
that are normals to the direction of maximum color contrast, Φ(u).
The length of each tangent vector is proportional to the local edge
strength E(u).
Figure 16.5 shows two examples of applying the Di Zenzo-Cumani-
style color edge operator (Alg. 16.2) to real images. Note that the
multi-gradient edge magnitude (calculated from the eigenvalue λ1 in
Eqn. (16.27)) in Fig. 16.5(b) is virtually identical to the monochro-
matic edge magnitude Emag under the L2 norm in Fig. 16.2(d). The
larger difference to the result for the L∞ norm in Fig. 16.2(e) is shown
in Fig. 16.5(c).
Thus, considering only edge magnitude, the Di Zenzo-Cumani op-
erator has no significant advantage over the simpler, monochromatic
operator in Sec. 16.1. However, if edge orientation is important (as
in the color version of the Canny operator described in Sec. 16.3), the
Di Zenzo-Cumani technique is certainly more reliable and consistent.
but here, with Ix , Iy not being vectors but scalar values, we get C 2 =
(Ix ·Iy )2 = Ix2 ·Iy2 , such that (A−B)2 +4C 2 = (A+B)2 , and therefore
λ1,2 (u) = A + B ± (A + B) / 2 . (16.37)
is simply the squared L2 norm of the local gradient vector, while the
smaller eigenvalue λ2 is always zero. Thus, for a grayscale image, the
402
Original image Color edge strength
16.2 Edges in
Vector-Valued Images
Fig. 16.4
Results from the monochro-
matic (Alg. 16.1) and the
Di Zenzo-Cumani color edge
operators (Alg. 16.2). The
original color image (a) has
constant luminance, that is,
the intensity gradient is zero
and thus a simple grayscale
operator would not detect
any edges at all. The local
edge strength E(u) is almost
identical for both color edge
operators (b). Edge tangent
orientation vectors (normal to
(a) (b) Φ(u)) for the monochromatic
and multi-gradient operators
Monochromatic operator Di Zenzo-Cumani operator (c, d); enlarged details in (e, f).
(c) (d)
(e) (f)
maximum edge strength λ1 (u) = ∇I(u)2 is equivalent to the
magnitude of the local intensity gradient.13 The fact that λ2 = 0
indicates that the local contrast in the orthogonal direction (i.e.,
along the edge tangent) is zero (see Fig. 16.3(c)).
To calculate the local edge orientation, at position u we use Eqn.
(16.31) to get
13
See Eqns. (6.5) and (6.13) in Chapter 6, Sec. 6.2.
403
16 Edge Detection in
Color Images
Fig. 16.5
Results of Di Zenzo-Cumani
color edge operator (Alg.
16.2) on real images. Orig-
inal image (a) and inverted
color edge magnitude (b).
The images in (c) show
the differences to the edge (a)
magnitude returned by the
monochromatic operator (Alg.
16.1, using the L∞ norm).
(b)
(c)
Alg. 16.3
1: ColorCannyEdgeDetector(I, σ, thi , tlo ) Canny edge detector for color
Input: I = (IR , IG , IB ), an RGB color image of size M × N ; images. Structure and pa-
σ, radius of Gaussian filter H G,σ ; thi , tlo , hysteresis thresholds rameters are identical to the
grayscale version in Alg. 6.1
(thi > tlo ). Returns a binary edge image of size M ×N . (p. 135). In the algorithm be-
low, edge magnitude (Emag )
2: I¯R ← IR ∗ H G,σ ⊲ blur components with Gaussian of width σ and orientation (Ex , Ey ) are
3: I¯G ← IG ∗ H G,σ obtained from the gradients of
4: I¯B ← IB ∗ H G,σ the individual color channels
(as described in Sec. 16.2.1).
5: Hx∇ ← [−0.5 0 0.5 ] ⊲ x gradient filter
⊺
6: Hy∇ ← [−0.5 0 0.5 ] ⊲ y gradient filter
7: I¯R,x ← I¯R ∗ Hx∇ , I¯R,y ← I¯R ∗ Hy∇
8: I¯G,x ← I¯G ∗ Hx∇ , I¯G,y ← I¯G ∗ Hy∇
9: I¯B,x ← I¯B ∗ Hx∇ , I¯B,y ← I¯B ∗ Hy∇
10: (M, N ) ← Size(I)
11: Create maps:
12: Emag , Enms , Ex , Ey : M ×N → R
13: Ebin : M ×N → {0, 1}
14: for all image coordinates u ∈ M ×N do
15: (rx , gx , bx ) ← (IR,x (u), IG,x (u), IB,x (u))
16: (ry , gy , by ) ← (IR,y (u), IG,y (u), IB,y (u))
17: A ← rx2 + gx2 + b2x ,
18: B ← ry2 + gy2 + b2y
19: C ← rx ·ry + gx ·gy + bx ·by
1/2
20: D ← (A−B)2 + 4C 2
1/2 √
21: Emag (u) ← 0.5 · A + B + D ⊲ λ1 , Eq. 16.27
22: Ex (u) ← A − B + D ⊲ q 1 , Eq. 16.28
23: Ey (u) ← 2C
24: Enms (u) ← 0
25: Ebin (u) ← 0
26: for u ← 1, . . . , M −2 do
27: for v ← 1, . . . , N −2 do
28: u ← (u, v)
29: dx ← Ex (u)
30: dy ← Ey (u)
31: s ← GetOrientationSector(dx , dy ) ⊲ Alg. 6.2
32: if IsLocalMax(Emag , u, s, tlo ) then ⊲ Alg. 6.2
33: Enms (u) ← Emag (u)
34: for u ← 1, . . . , M −2 do
35: for v ← 1, . . . , N −2 do
36: u ← (u, v)
37: if (Enms (u) ≥ thi ∧ Ebin (u) = 0) then
38: TraceAndThreshold(Enms , Ebin , u, v, tlo ) ⊲ Alg. 6.2
39: return Ebin .
405
16 Edge Detection in In the pre-processing step, each of the three color channels is
Color Images individually smoothed by a Gaussian filter of width σ, before cal-
culating the gradient vectors (Alg. 16.3, lines 2–9). As in Alg. 16.2,
the color edge magnitude is calculated as the squared local contrast,
obtained from the dominant eigenvalue of the structure matrix M
(Eqns. (16.24)–(16.27)). The local gradient vector (Ex , Ey ) is cal-
culated from the elements A, B, C, of the matrix M, as given in
Eqn. (16.28). The corresponding steps are found in Alg. 16.3, lines
14–22. The remaining steps, including non-maximum suppression,
edge tracing and thresholding, are exactly the same as in Alg. 6.1.
Results from the grayscale and color version of the Canny edge
detector are compared in Figs. 16.6 and 16.7 for varying values of
σ and thi , respectively. In all cases, the gradient magnitude was
normalized and the threshold values thi , tlo are given as a percent-
age of the maximum edge magnitude. Evidently, the color detector
gives the more consistent results, particularly at color edges with low
intensity difference.
For comparison, Fig. 16.8 shows the results of applying the
monochromatic Canny operator separately to each color channel and
subsequently merging the edge pixels into a combined edge map, as
mentioned at the beginning of this section. We see that this leads
to multiple responses and cluttered edges, since maximum gradient
positions in the different color channels are generally not collocated.
In summary, the Canny edge detector is superior to simpler
schemes based on first-order gradients and global thresholding, in
terms of extracting clean and well-located edges that are immedi-
ately useful for subsequent processing. The results in Figs. 16.6 and
16.7 demonstrate that the use of color gives additional improvements
over the grayscale approach, since edges with insufficient brightness
gradients can still be detected from local color differences. Essential
for the good performance of the color Canny edge detector, how-
ever, is the reliable calculation of the gradient direction, based on
the multi-dimensional local contrast formulation given in Sec. 16.2.3.
Quite a few variations of Canny detectors for color images have been
proposed in the literature, including the one attributed to Kanade
(in [140]), which is similar to the algorithm described here.
Fig. 16.6
Canny grayscale vs. color
version. Results from the
grayscale (left) and the color
version (right) of the Canny
operator for different values of
σ (thi = 20%, tlo = 5% of max.
edge magnitude).
(a) (b)
407
Canny (grayscale) Canny (color)
16 Edge Detection in
Color Images
Fig. 16.7
Canny grayscale vs. color
version. Results from the
grayscale (left) and the
color version (right) of the
Canny operator for different
threshold values thi , given
in % of max. edge magni-
tude (tlo = 5%, σ = 2.0).
(a) (b)
408
σ = 2.0 σ = 5.0
16.4 Other Color Edge
Operators
Fig. 16.8
Scalar vs. vector-based color
Canny operator. Results from
the scalar Canny operator ap-
plied separately to each color
channel (a, b). Channel edges
are shown in corresponding
colors, with mixed colors indi-
cating that edge points were
(a) (b) detected in multiple channels
(e.g., yellow marks overlapping
points from the red and the
green channel). A black pixel
indicates that an edge point
was detected in all three color
channels. Channel edges com-
bined into a joint edge map
(c, d). For comparison, the re-
sult of the vector-based color
Canny operator (e, f). Com-
mon parameter settings are
σ = 2.0 and 5.0, thi = 20%,
tlo = 5% of max. edge magni-
(c) (d) tude.
(e) (f)
409
16 Edge Detection in 16.5 Java Implementation
Color Images
The following Java implementations of the algorithms described in
this chapter can be found in the source code section15 of the book’s
website. The common (abstract) super-class for all color edge de-
tectors is ColorEdgeDetector, which mainly provides the following
methods:
FloatProcessor getEdgeMagnitude ()
Returns the resulting edge magnitude map E(u) as a Float-
Processor object.
FloatProcessor getEdgeOrientation ()
Returns the resulting edge orientation map Φ(u) as a Float-
Processor object, with values in the range [−π, π].
The following edge detectors are defined as concrete sub-classes of
ColorEdgeDetector:
GrayscaleEdgeDetector: Implements an edge detector that uses
only the intensity (brightness) of the supplied color image.
MonochromaticEdgeDetector: Implements the monochromatic color
edge detector described in Alg. 16.1.
DiZenzoCumaniEdgeDetector: Implements the Di Zenzo-Cumani ty-
pe color edge detector described in Alg. 16.2.
CannyEdgeDetector: Implements the canny edge detector for gray-
scale and color images described in Alg. 16.3. This class defines
the additional methods
ByteProcessor getEdgeBinary(),
List<List<java.awt.Point>> getEdgeTraces().
Program 16.1 shows a complete example for the use of the class
CannyEdgeDetector in the context of an ImageJ plugin.
15
Package imagingbook.pub.color.edge.
410
1 import ij.ImagePlus; 16.5 Java
2 import ij.plugin.filter.PlugInFilter; Implementation
3 import ij.process.ByteProcessor;
4 import ij.process.FloatProcessor; Prog. 16.1
5 import ij.process.ImageProcessor; Use of the CannyEdgeDetector
6 import imagingbook.pub.coloredge.CannyEdgeDetector; class in an ImageJ plugin. A
parameter object (params) is
7 created in line 20, subsequently
8 import java.awt.Point; configured (in lines 22–24)
and finally used to construct
9 import java.util.List; a CannyEdgeDetector object in
10 line 27. Note that edge detec-
11 public class Canny_Edge_Demo implements PlugInFilter { tion is performed within the
constructor method. Lines 29–
12 33 demonstrate how different
13 public int setup(String arg0, ImagePlus imp) { types of edge detection results
14 return DOES_ALL + NO_CHANGES; can be retrieved. The binary
edge map eBin is displayed in
15 } line 35. As indicated in the
16 setup() method (by returning
DOES_ALL), this plugin works
17 public void run(ImageProcessor ip) { with any type of image.
18
19 CannyEdgeDetector.Parameters params =
20 new CannyEdgeDetector.Parameters();
21
22 params.gSigma = 3.0f; // σ of Gaussian
23 params.hiThr = 20.0f; // 20% of max. edge magnitude
24 params.loThr = 5.0f; // 5% of max. edge magnitude
25
26 CannyEdgeDetector detector =
27 new CannyEdgeDetector(ip, params);
28
29 FloatProcessor eMag = detector.getEdgeMagnitude();
30 FloatProcessor eOrt = detector.getEdgeOrientation();
31 ByteProcessor eBin = detector.getEdgeBinary();
32 List<List<Point>> edgeTraces =
33 detector.getEdgeTraces();
34
35 (new ImagePlus("Canny Edges", eBin)).show();
36
37 // process edge detection results ...
38 }
39 }
411