【图像处理】RGB图像边缘检测

版权声明:【fishing-pan:https://blog.csdn.net/u013921430转载请注明出处】 https://blog.csdn.net/u013921430/article/details/82931206

【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)
   或者二阶差分对边缘区域进行判断;
Δ 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)
   其中一阶差分可以判断边缘是否存在,二阶差分还可以根据正负号判断像素点在图像边缘亮的一侧还是暗的一侧。
   其他的边缘检测方法还包括一些梯度算子,例如Prewitt算子、Sobel算子,Canny算子,LOG边缘检测算子等,在此不做说明。

彩色图边缘检测

   RGB 图像使用三个通道存储像素信息,我们可以将这三个通道的信息看作是一个矢量,而矢量是不存在梯度的概念的,我们无法直接将上诉方法或算子直接用于RGB 图像,而且RGB图像单个通道的梯度信息又无法反映整体的梯度信息。
   在《数字图像处理》(冈萨雷斯)中提到了一种针对彩色图像的边缘检测方法,这种方法由 Di Zenzo 等人在1986年提出,下面就一起看看这种方法如何得出。

Di Zenzo’s gradient operator

   在图像多通道图像 f ( x , y ) f(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 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}
   对 f ( x + ε c o s θ , y + ε s i n θ ) f(x+\varepsilon cos\theta,y+\varepsilon sin\theta) 进行二元泰勒展开;
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}
   其中 m m 表示图像通道数目,为了方便表述使用 f i x \frac{\partial f_{i}}{\partial x} 代替 f i ( x , y ) x \frac{\partial f_{i}(x,y)}{\partial x} ,则有;
Δ 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}
  重新定义一个函数 f ( θ ) f(\theta) ,令
f ( θ ) = 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} f(\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}
   进一步舍去式子中的 ε \varepsilon 项,令
f ( θ ) = 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 f(\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}
   为了进一步方便表述;令
E = i = 1 m f i x 2 ; F = i = 1 m f i y 2 ; G = 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}; G=\sum_{i=1}^{m}\frac{\partial f_{i}}{\partial x}\frac{\partial f_{i}}{\partial y}
f ( θ ) = c o s θ 2 E + s i n θ 2 F + 2 s i n θ c o s θ G f(\theta)=cos\theta^{2}E+sin\theta^{2}F+2sin\theta cos\theta G
   现在 θ \theta 成为了式子中唯一的变量,再回到边缘的定义上,边缘的方向是图像像素梯度最大的方向。也就是说梯度的方向 θ m a x \theta_{max} 会使 f ( θ ) f(\theta) 取最大值,则;
θ m a x = f ( θ ) a r g m a x \theta_{max}=\underset{argmax}{f(\theta )}
   对 f ( θ ) f(\theta) 进行求导;
f ( θ ) = E s i n 2 θ + 2 F c o s 2 θ + G s i n 2 θ f(\theta )^{'}=-Esin2\theta + 2F cos2\theta + G sin2\theta
   令 f ( θ ) = 0 f(\theta )^{'}=0 ,得;
t a n   2 θ m a x = 2 F E G θ m a x = 1 2 a r c t a n ( 2 F E G + k π ) tan ~2\theta_{max} =\frac{2F}{E-G}\\ \theta_{max}=\frac{1}{2}arctan(\frac{2F}{E-G}+k\pi)
   很明显 f ( θ ) f(\theta ) 是一个以 π \pi 为周期的周期函数,如果只考虑区间 [ 0 , π ) \left [ 0 ,\pi\right ) ,且 θ m a x \theta_{max} 落到该区间内,则会有另一个让 f ( θ ) f(\theta ) 取极值的解也落在该区域内,这个值是 θ m a x + π 2 \theta_{max}+ \frac{\pi}{2} 或者 θ m a x π 2 \theta_{max}-\frac{\pi}{2} 。但是不论如何这两个解有一个让 f ( θ ) f(\theta ) 取极大值,另一个让其取极小值,两个角度相差 90°。
  
   说到这里大家应该都明白了,两个角度对应的方向是相互垂直的,一个是垂直于边缘的方向,也就是边缘的法向,此时梯度取最大值。另一个是平行于边缘的方向,是边缘的切向,此时梯度取极小值。
  

RGB图像的边缘检测

   在RGB图像中, 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}
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}
   其中 r \overset{\rightarrow }{r} g \overset{\rightarrow }{g} b \overset{\rightarrow }{b} 分别代表不同颜色分量的单位向量,则
g x x = E = u T u = R x 2 + G x 2 + B x 2 g_{xx}=E=\overset{\rightarrow }{u}^{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}
g y y = F = v T v = R y 2 + G y 2 + B y 2 g_{yy}=F=\overset{\rightarrow }{v}^{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}
g x y = G = u T v = R x R y + G x G y + B x B y g_{xy}=G=\overset{\rightarrow }{u}^{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}
   在利用Di Zenzo 提出的方法求得 θ m a x \theta_{max} 后,将以上符号带入到 f ( θ ) f(\theta) ,可以计算出像素点梯度大小为
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}}
   进而可以根据梯度大小进行边缘检测。

参考资料

  1. S. Di Zenzo, A note on the gradient of a multi-image, Computer Vision, Graphics, and Image Processing 33 (1) (1986) 116–125.
  2. 《数字图像处理》 (冈萨雷斯)

猜你喜欢

转载自blog.csdn.net/u013921430/article/details/82931206
今日推荐