OpenCV4系列之影象梯度和邊緣檢測
在影象處理中,求解影象梯度是常用操作。
Sobel運算元
Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.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 ) C: void cvSobel(const CvArr* src, CvArr* dst, int xorder, int yorder, int aperture_size=3 )引數含義
src – 輸入影象
dst – 輸出結果,與輸入影象具有相同的尺寸和通道數
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時,輸出與輸入具有相同的資料型別。當輸入是8位元影象時,輸出結果將是截斷的導數值(in the case of 8-bit input images it will result in truncated derivatives)。
xorder – x方向求導階數
yorder – y方向求導階數
ksize – 卷積核的大小,只能是1/3/5/7之一(it must be 1, 3, 5, or 7)。
scale – 縮放尺度因子,預設無縮放
delta – 儲存之前加到上述結果上的偏移量。
borderType – 邊界插值方法,詳見附錄A-1。
Scharr運算元
Calculates the first x- or y- image derivative using Scharr operator.該運算元引數和 Sobel 運算元一致,與 Sobel 區別在於,Scharr 僅作用於大小為3的核心。具有和sobel運算元一樣的速度,但結果更為精確。
C++: void Scharr(InputArray src, OutputArray dst, int ddepth, int dx, int dy, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )引數含義
src – 輸入影象
dst – 輸出結果,與輸入影象具有相同的尺寸和通道數
ddepth – 輸出影象的資料型別,即矩陣中元素的一個通道的資料型別,這個值和 type 是相關的。例如 type 為 CV_16SC2,一個2通道的16位的有符號整數,depth是CV_16S
dx – dx=1 表示求 x 方向的一階梯度,dx=0 表示不求 x 方向
dy – 與上類似,dy=1 表示求 y 方向的一階梯度,dy=0 表示不求 y 方向
scale – 求導得到的值的縮放尺度因子,預設無縮放
delta – 在儲存之前加到求導值上的數值,可以用於將0以下的值調整到0以上。delta=0 時表示梯度為0處結果儲存為0;delta=m 時表示梯度為0處結果儲存為m
borderType – 表示影象四周畫素外插值方法,預設是 BORDER_DEFAULT,該引數解釋見附錄A-1。
附錄A-1
borderType 決定在影象發生幾何變換或者濾波操作(卷積)時邊沿畫素的處理方式
/* Various border types, image boundaries are denoted with '|' * BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh * BORDER_REFLECT: fedcba|abcdefgh|hgfedcb * BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba * BORDER_WRAP: cdefgh|abcdefgh|abcdefg * BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i' */
- BORDER_CONSTANT 邊沿畫素用 i 擴充套件,需要設定borderValue 指定 ' i ' 值,const cv::Scalar& borderValue = cv::Scalar(0);
- BORDER_REPLICATE,複製邊界畫素
- BORDER_REFLECT,對邊界對稱擴充套件,包含對稱軸處的元素
- BORDER_REFLECT_101,以邊界為對稱軸對稱擴充套件複製畫素,不包含對稱軸處的元素
參考資料
[1] Image Filtering
[2] OpenCV2:Mat屬性type,depth,step
[3] Sobel Derivatives
[4] opencv邊緣檢測sobel運算元
[5] python opencv學習(六)影象梯度計