OpenCV4系列之圖像梯度和邊緣檢測

在圖像處理中,求解圖像梯度是經常使用操做。html

Sobel算子python

Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.

Sobel 算子是一種離散性差分算子,用來計算圖像像素值的一階、二階、三階或混合梯度。在圖像的任何一點使用此算子,將會產生對應的灰度矢量或是其法矢量。web

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 – 輸入圖像spa

dst – 輸出結果,與輸入圖像具備相同的尺寸和通道數.net

ddepth – 輸出圖像的數據類型。支持如下數據類型組合code

  • 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)。htm

xorder – x方向求導階數blog

yorder – y方向求導階數ci

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學習(六)圖像梯度計算

相關文章
相關標籤/搜索