sobel運算元是一種常用的邊緣檢測運算元,是一階的梯度演算法。對雜訊具有平滑作用,提供較為精確的邊緣方向資訊,邊緣定位精度不夠高。當對精度要求不是很高時,是一種較為常用的邊緣檢測方法。它進行處理的模板如下:
其中,gx是橫向的運算元,gy是縱向的運算元。
原影象記為f,則
gx = gx*f
gy = gy*f
gx =-1*f(x-1, y-1) + 0*f(x,y-1) + 1*f(x+1,y-1)+(-2)*f(x-1,y) + 0*f(x,y)+2*f(x+1,y)+(-1)*f(x-1,y+1) + 0*f(x,y+1) + 1*f(x+1,y+1)
gy =1* f(x-1, y-1) + 2*f(x,y-1)+ 1*f(x+1,y-1)+0*f(x-1,y) 0*f(x,y) + 0*f(x+1,y)+(-1)*f(x-1,y+1) + (-2)*f(x,y+1) + (-1)*f(x+1, y+1)
gx,gy代表利用模板對原影象卷積的結果。
對於原影象中的每乙個畫素,在3*3的模板中進行上述的卷積,得到gx、gy,則最後該畫素的灰度值近似為:
g = |gx|+|gy|
如果g大於某乙個閾值,則認定該點為乙個邊緣點。
上述的處理可以同時進行兩個方向的處理,當需要突出影象某乙個方向的邊緣資訊時,也可以只進行其中乙個方向的處理。
在opencv3.1.0中,sobel運算元在c++中的函式原型如下:
void sobel(inputarray src, outputarray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int bordertype=border_default )
函式引數解釋:
inputarray src:輸入的原影象,mat型別
outputarray dst:輸出的邊緣檢測結果影象,mat型,大小與原影象相同。
int ddepth:輸出影象的深度,針對不同的輸入影象,輸出目標影象有不同的深度,具體組合如下:
- 若src.depth() = cv_8u, 取ddepth =-1/cv_16s/cv_32f/cv_64f
- 若src.depth() = cv_16u/cv_16s, 取ddepth =-1/cv_32f/cv_64f
- 若src.depth() = cv_32f, 取ddepth =-1/cv_32f/cv_64f
- 若src.depth() = cv_64f, 取ddepth = -1/cv_64f
注:ddepth =-1時,代表輸出影象與輸入影象相同的深度。
int dx:int型別dx,x 方向上的差分階數,1或0
int dy:int型別dy,y 方向上的差分階數,1或0
其中,dx=1,dy=0,表示計算x方向的導數,檢測出的是垂直方向上的邊緣;dx=0,dy=1,表示計算y方向的導數,檢測出的是水平方向上的邊緣。
int ksize:為進行邊緣檢測時的模板大小為ksize*ksize,取值為1、3、5和7,其中預設值為3。特殊情況:ksize=1時,採用的模板為3*1或1*3。
當ksize=3時,sobel核心可能產生比較明顯的誤差,此時,可以使用 scharr 函式,該函式僅作用於大小為3的核心。具有跟sobel一樣的速度,但結果更精確,其核心為:
其呼叫格式為:
scharr( src_gray, grad_x, ddepth, 1, 0, 1, 0, border_default );
scharr( src_gray, grad_y, ddepth, 0, 1, 1, 0, border_default );
等價於:
/// 求 x方向梯度
sobel(src_gray,grad_x,ddepth, 1, 0, cv_scharr, scale, delta, border_default );
/// 求 y方向梯度
sobel(src_gray,grad_y,ddepth, 0, 1, cv_scharr, scale, delta, border_default );
double scale:預設1。
double delta:預設0。
int bordertype:預設值為border_default。
sobel演算法**實現過程為:
/// 求 x方向梯度
sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, border_default );
/// 求 y方向梯度
sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, border_default );
convertscaleabs( grad_x, abs_grad_x );
convertscaleabs( grad_y, abs_grad_y );
addweighted( dst_x, 0.5, dst_y, 0.5, 0, dst); //一種近似的估計
使用OPENCV中的Sobel函式找函式邊緣並顯示
mat mat blur mat blur in.clone gaussianblur in,mat blur,size blursize,blursize 0,0,border default 高斯平滑 mat mat gray if mat blur.channels 3 cvtcolor ma...
OpenCV學習筆記 Sobel運算元
該函式如下 使用擴充套件 sobel 運算元計算一階 二階 三階或混合影象差分 void cvsobel const cvarr src,cvarr dst,int xorder,int yorder,int aperture size 3 src 輸入影象.dst輸出影象.xorder x 方向上...
OpenCV學習筆記 Sobel運算元
該函式如下 使用擴充套件 sobel 運算元計算一階 二階 三階或混合影象差分 void cvsobel const cvarr src,cvarr dst,int xorder,int yorder,int aperture size 3 src 輸入影象.dst輸出影象.xorder x 方向上...