【fishing-pan:https://blog.csdn.net/u013921430 转载请注明出处】
灰度图边缘检测
在学习图像处理时,首先接触到的就是灰度图像的边缘检测,这是图像处理最基础的也是最重要的一环,熟悉图像边缘检测有助于我们学习其他的数字图像处理方法。由于图像的边缘区域会存在明显的像素值阶跃,因此边缘检测主要是通过获得图像灰度梯度,进而通过梯度大小和变化来判断图像边缘的。
因此,我们可以通过一阶差分;
Δ
f
x
(
x
,
y
)
=
f
(
x
+
1
,
y
)
−
f
(
x
,
y
)
Δ
f
y
(
x
,
y
)
=
f
(
x
,
y
+
1
)
−
f
(
x
,
y
)
\Delta f_{x}(x,y)=f(x+1,y)-f(x,y)\\ \Delta f_{y}(x,y)=f(x,y+1)-f(x,y)
Δfx(x,y)=f(x+1,y)−f(x,y)Δfy(x,y)=f(x,y+1)−f(x,y)
或者二阶差分对边缘区域进行判断;
Δ
f
x
x
(
x
,
y
)
=
f
(
x
+
1
,
y
)
+
f
(
x
−
1
,
y
)
−
2
f
(
x
,
y
)
Δ
f
y
y
(
x
,
y
)
=
f
(
x
,
y
+
1
)
+
f
(
x
,
y
−
1
)
−
2
f
(
x
,
y
)
\Delta f_{xx}(x,y)=f(x+1,y)+f(x-1,y)-2f(x,y)\\ \Delta f_{yy}(x,y)=f(x,y+1)+f(x,y-1)-2f(x,y)
Δfxx(x,y)=f(x+1,y)+f(x−1,y)−2f(x,y)Δfyy(x,y)=f(x,y+1)+f(x,y−1)−2f(x,y)
其中一阶差分可以判断边缘是否存在,二阶差分还可以根据正负号判断像素点在图像边缘亮的一侧还是暗的一侧。
其他的边缘检测方法还包括一些梯度算子,例如Prewitt算子、Sobel算子,Canny算子,LOG边缘检测算子等,在此不做说明。
彩色图边缘检测
RGB 图像使用三个通道存储像素信息,我们可以将这三个通道的信息看作是一个矢量,而矢量是不存在梯度的概念的,我们无法直接将上诉方法或算子直接用于RGB 图像,而且RGB图像单个通道的梯度信息又无法反映整体的梯度信息。
在《数字图像处理》(冈萨雷斯)中提到了一种针对彩色图像的边缘检测方法,这种方法由 Di Zenzo 等人在1986年提出,下面就一起看看这种方法如何得出。
Di Zenzo’s gradient operator
在图像多通道图像
f
(
x
,
y
)
f(x,y)
f(x,y) 中的某一点
P
(
x
,
y
)
P(x,y)
P(x,y) 处,假设其梯度方向为
θ
\theta
θ
Δ
f
=
∥
f
(
x
+
ε
c
o
s
θ
,
y
+
ε
s
i
n
θ
)
−
f
(
x
,
y
)
∥
\Delta f=\left \| f(x+\varepsilon cos\theta,y+\varepsilon sin\theta) -f(x,y)\right \|
Δf=∥f(x+εcosθ,y+εsinθ)−f(x,y)∥
为了便于计算,将计算绝对值换为计算平方,令
Δ
f
2
=
∥
f
(
x
+
ε
c
o
s
θ
,
y
+
ε
s
i
n
θ
)
−
f
(
x
,
y
)
∥
2
\Delta f^{2}=\left \| f(x+\varepsilon cos\theta,y+\varepsilon sin\theta) -f(x,y)\right \|^{2}
Δf2=∥f(x+εcosθ,y+εsinθ)−f(x,y)∥2
对
f
(
x
+
ε
c
o
s
θ
,
y
+
ε
s
i
n
θ
)
f(x+\varepsilon cos\theta,y+\varepsilon sin\theta)
f(x+εcosθ,y+εsinθ)进行二元泰勒展开;
f
(
x
+
ε
c
o
s
θ
,
y
+
ε
s
i
n
θ
)
=
f
(
x
,
y
)
+
∑
i
=
1
m
(
ε
c
o
s
θ
⋅
∂
f
i
(
x
,
y
)
∂
x
+
ε
s
i
n
θ
⋅
∂
f
i
(
x
,
y
)
∂
y
)
+
o
n
≈
f
(
x
,
y
)
+
∑
i
=
1
m
(
ε
c
o
s
θ
⋅
∂
f
i
(
x
,
y
)
∂
x
+
ε
⋅
s
i
n
θ
⋅
∂
f
i
(
x
,
y
)
∂
y
)
\begin{aligned} f(x+\varepsilon cos\theta,y+\varepsilon sin\theta)&=f(x,y)+\sum_{i=1}^{m}(\varepsilon cos\theta\cdot\frac{\partial f_{i}(x,y)}{\partial x} +\varepsilon sin\theta\cdot\frac{\partial f_{i}(x,y)}{\partial y} )+o^{n}\\ &\approx f(x,y)+\sum_{i=1}^{m}(\varepsilon cos\theta\cdot\frac{\partial f_{i}(x,y)}{\partial x} +\varepsilon\cdot sin\theta\cdot\frac{\partial f_{i}(x,y)}{\partial y} ) \end{aligned}
f(x+εcosθ,y+εsinθ)=f(x,y)+i=1∑m(εcosθ⋅∂x∂fi(x,y)+εsinθ⋅∂y∂fi(x,y))+on≈f(x,y)+i=1∑m(εcosθ⋅∂x∂fi(x,y)+ε⋅sinθ⋅∂y∂fi(x,y))
其中
m
m
m表示图像通道数目,为了方便表述使用
∂
f
i
∂
x
\frac{\partial f_{i}}{\partial x}
∂x∂fi代替
∂
f
i
(
x
,
y
)
∂
x
\frac{\partial f_{i}(x,y)}{\partial x}
∂x∂fi(x,y),而在求导时各个通道之间是相互独立的,则有;
Δ
f
2
≈
∑
i
=
1
m
(
ε
c
o
s
θ
⋅
∂
f
i
∂
x
+
ε
s
i
n
θ
⋅
∂
f
i
∂
y
)
2
\Delta f^{2}\approx\sum_{i=1}^{m}(\varepsilon cos\theta\cdot\frac{\partial f_{i}}{\partial x} +\varepsilon sin\theta\cdot\frac{\partial f_{i}}{\partial y} )^{2}
Δf2≈i=1∑m(εcosθ⋅∂x∂fi+εsinθ⋅∂y∂fi)2
重新定义一个函数
G
(
θ
)
G(\theta)
G(θ),令
G
(
θ
)
=
∑
i
=
1
m
(
ε
c
o
s
θ
⋅
∂
f
i
∂
x
+
ε
s
i
n
θ
⋅
∂
f
i
∂
y
)
2
=
ε
2
(
c
o
s
θ
2
∑
i
=
1
m
∥
∂
f
i
∂
x
∥
2
+
s
i
n
θ
2
∑
i
=
1
m
∥
∂
f
i
∂
y
∥
2
+
2
s
i
n
θ
c
o
s
θ
∑
i
=
1
m
∂
f
i
∂
x
∂
f
i
∂
y
)
\begin{aligned} G(\theta)&=\sum_{i=1}^{m}(\varepsilon cos\theta\cdot\frac{\partial f_{i}}{\partial x} +\varepsilon sin\theta\cdot\frac{\partial f_{i}}{\partial y} )^{2}\\ &=\varepsilon ^{2}(cos\theta^{2}\sum_{i=1}^{m}\left \|\frac{\partial f_{i}}{\partial x}\right \|^{2}+sin\theta^{2}\sum_{i=1}^{m}\left \|\frac{\partial f_{i}}{\partial y}\right \|^{2}+2sin\theta cos\theta \sum_{i=1}^{m}\frac{\partial f_{i}}{\partial x}\frac{\partial f_{i}}{\partial y}) \end{aligned}
G(θ)=i=1∑m(εcosθ⋅∂x∂fi+εsinθ⋅∂y∂fi)2=ε2(cosθ2i=1∑m∥∥∥∥∂x∂fi∥∥∥∥2+sinθ2i=1∑m∥∥∥∥∂y∂fi∥∥∥∥2+2sinθcosθi=1∑m∂x∂fi∂y∂fi)
进一步舍去式子中的
ε
\varepsilon
ε 项,令
G
(
θ
)
=
c
o
s
θ
2
∑
i
=
1
m
∥
∂
f
i
∂
x
∥
2
+
s
i
n
θ
2
∑
i
=
1
m
∥
∂
f
i
∂
y
∥
2
+
2
s
i
n
θ
c
o
s
θ
∑
i
=
1
m
∂
f
i
∂
x
∂
f
i
∂
y
G(\theta)=cos\theta^{2}\sum_{i=1}^{m}\left \|\frac{\partial f_{i}}{\partial x}\right \|^{2}+sin\theta^{2}\sum_{i=1}^{m}\left \|\frac{\partial f_{i}}{\partial y}\right \|^{2}+2sin\theta cos\theta \sum_{i=1}^{m}\frac{\partial f_{i}}{\partial x}\frac{\partial f_{i}}{\partial y}
G(θ)=cosθ2i=1∑m∥∥∥∥∂x∂fi∥∥∥∥2+sinθ2i=1∑m∥∥∥∥∂y∂fi∥∥∥∥2+2sinθcosθi=1∑m∂x∂fi∂y∂fi
为了进一步方便表述;令
E
=
∑
i
=
1
m
∥
∂
f
i
∂
x
∥
2
;
F
=
∑
i
=
1
m
∥
∂
f
i
∂
y
∥
2
;
H
=
∑
i
=
1
m
∂
f
i
∂
x
∂
f
i
∂
y
E=\sum_{i=1}^{m}\left \| \frac{\partial f_{i}}{\partial x} \right \|^{2}; F=\sum_{i=1}^{m}\left \|\frac{\partial f_{i}}{\partial y}\right \|^{2}; H=\sum_{i=1}^{m}\frac{\partial f_{i}}{\partial x}\frac{\partial f_{i}}{\partial y}
E=i=1∑m∥∥∥∥∂x∂fi∥∥∥∥2;F=i=1∑m∥∥∥∥∂y∂fi∥∥∥∥2;H=i=1∑m∂x∂fi∂y∂fi
G
(
θ
)
=
c
o
s
θ
2
E
+
s
i
n
θ
2
F
+
2
s
i
n
θ
c
o
s
θ
H
G(\theta)=cos\theta^{2}E+sin\theta^{2}F+2sin\theta cos\theta H
G(θ)=cosθ2E+sinθ2F+2sinθcosθH
现在
θ
\theta
θ成为了式子中唯一的变量,再回到边缘的定义上,边缘的方向是图像像素梯度最大的方向。也就是说梯度的方向
θ
m
a
x
\theta_{max}
θmax 会使
G
(
θ
)
G(\theta)
G(θ) 取最大值,则;
θ
m
a
x
=
G
(
θ
)
a
r
g
m
a
x
\theta_{max}=\underset{argmax}{G(\theta )}
θmax=argmaxG(θ)
对
G
(
θ
)
G(\theta)
G(θ) 进行求导;
G
(
θ
)
′
=
−
E
s
i
n
2
θ
+
F
s
i
n
2
θ
+
2
H
c
o
s
2
θ
G(\theta )^{'}=-Esin2\theta +F sin2\theta+2 H cos2\theta
G(θ)′=−Esin2θ+Fsin2θ+2Hcos2θ
令
G
(
θ
)
′
=
0
G(\theta )^{'}=0
G(θ)′=0,得;
t
a
n
2
θ
m
a
x
=
2
H
E
−
F
θ
m
a
x
=
1
2
a
r
c
t
a
n
(
2
H
E
−
F
+
k
π
)
tan ~2\theta_{max} =\frac{2H}{E-F}\\ \theta_{max}=\frac{1}{2}arctan(\frac{2H}{E-F}+k\pi)
tan 2θmax=E−F2Hθmax=21arctan(E−F2H+kπ)
很明显
G
(
θ
)
G(\theta )
G(θ) 是一个以
π
\pi
π 为周期的周期函数,如果只考虑区间
[
0
,
π
)
\left [ 0 ,\pi\right )
[0,π),且
θ
m
a
x
\theta_{max}
θmax 落到该区间内,则会有另一个让
G
(
θ
)
G(\theta )
G(θ)取极值的解也落在该区域内,这个值是
θ
m
a
x
+
π
2
\theta_{max}+ \frac{\pi}{2}
θmax+2π或者
θ
m
a
x
−
π
2
\theta_{max}-\frac{\pi}{2}
θmax−2π。但是不论如何这两个解有一个让
G
(
θ
)
G(\theta )
G(θ)取极大值,另一个让其取极小值,两个角度相差 90°。
说到这里大家应该都明白了,两个角度对应的方向是相互垂直的,一个是垂直于边缘的方向,也就是边缘的法向,此时梯度取最大值。另一个是平行于边缘的方向,是边缘的切向,此时梯度取极小值。
RGB图像的边缘检测
在RGB图像中,
m
=
3
m=3
m=3,再令;
u
→
=
∂
R
∂
x
r
→
+
∂
G
∂
x
g
→
+
∂
B
∂
x
b
→
\overset{\rightarrow }{u}=\frac{\partial R}{\partial x}\overset{\rightarrow }{r}+\frac{\partial G}{\partial x}\overset{\rightarrow }{g}+\frac{\partial B}{\partial x}\overset{\rightarrow }{b}
u→=∂x∂Rr→+∂x∂Gg→+∂x∂Bb→
v
→
=
∂
R
∂
y
r
→
+
∂
G
∂
y
g
→
+
∂
B
∂
y
b
→
\overset{\rightarrow }{v}=\frac{\partial R}{\partial y}\overset{\rightarrow }{r}+\frac{\partial G}{\partial y}\overset{\rightarrow }{g}+\frac{\partial B}{\partial y}\overset{\rightarrow }{b}
v→=∂y∂Rr→+∂y∂Gg→+∂y∂Bb→
其中
r
→
\overset{\rightarrow }{r}
r→、
g
→
\overset{\rightarrow }{g}
g→、
b
→
\overset{\rightarrow }{b}
b→分别代表不同颜色分量的单位向量,则
g
x
x
=
E
=
u
→
T
u
→
=
∥
∂
R
∂
x
∥
2
+
∥
∂
G
∂
x
∥
2
+
∥
∂
B
∂
x
∥
2
g_{xx}=E=\overset{\rightarrow }{u}^{\tiny{T}}\overset{\rightarrow }{u}=\left \| \frac{\partial R}{\partial x} \right \|^{2}+\left \| \frac{\partial G}{\partial x} \right \|^{2}+\left \| \frac{\partial B}{\partial x} \right \|^{2}
gxx=E=u→Tu→=∥∥∥∥∂x∂R∥∥∥∥2+∥∥∥∥∂x∂G∥∥∥∥2+∥∥∥∥∂x∂B∥∥∥∥2
g
y
y
=
F
=
v
→
T
v
→
=
∥
∂
R
∂
y
∥
2
+
∥
∂
G
∂
y
∥
2
+
∥
∂
B
∂
y
∥
2
g_{yy}=F=\overset{\rightarrow }{v}^{\tiny{T}}\overset{\rightarrow }{v}=\left \| \frac{\partial R}{\partial y} \right \|^{2}+\left \| \frac{\partial G}{\partial y} \right \|^{2}+\left \| \frac{\partial B}{\partial y} \right \|^{2}
gyy=F=v→Tv→=∥∥∥∥∂y∂R∥∥∥∥2+∥∥∥∥∂y∂G∥∥∥∥2+∥∥∥∥∂y∂B∥∥∥∥2
g
x
y
=
H
=
u
→
T
v
→
=
∂
R
∂
x
∂
R
∂
y
+
∂
G
∂
x
∂
G
∂
y
+
∂
B
∂
x
∂
B
∂
y
g_{xy}=H=\overset{\rightarrow }{u}^{\tiny{T}}\overset{\rightarrow }{v}=\frac{\partial R}{\partial x}\frac{\partial R}{\partial y}+\frac{\partial G}{\partial x}\frac{\partial G}{\partial y}+\frac{\partial B}{\partial x}\frac{\partial B}{\partial y}
gxy=H=u→Tv→=∂x∂R∂y∂R+∂x∂G∂y∂G+∂x∂B∂y∂B
在利用Di Zenzo 提出的方法求得
θ
m
a
x
\theta_{max}
θmax 后,将以上符号带入到
G
(
θ
)
G(\theta)
G(θ),可以计算出像素点梯度大小为
G
(
θ
)
=
{
1
2
[
(
g
x
x
+
g
y
y
)
+
(
g
x
x
−
g
y
y
)
c
o
s
2
θ
m
a
x
+
2
g
x
y
s
i
n
2
θ
m
a
x
]
}
1
2
G(\theta)=\left \{ \frac{1}{2}\left [ (g_{xx}+g_{yy}) +(g_{xx}-g_{yy})cos~2\theta_{max} +2g_{xy}sin~2\theta_{max} \right ]\right \}^{\frac{1}{2}}
G(θ)={21[(gxx+gyy)+(gxx−gyy)cos 2θmax+2gxysin 2θmax]}21
进而可以根据梯度大小进行边缘检测。
参考资料
- S. Di Zenzo, A note on the gradient of a multi-image, Computer Vision, Graphics, and Image Processing 33 (1) (1986) 116–125.
- 《数字图像处理》 (冈萨雷斯)