在opencv中scharr濾波器是配合sobel算子的運算而存在的。當(dāng)sobel內(nèi)核為3時,結(jié)果可能會產(chǎn)生比較明顯的誤差,針對這一問題,opencv提供了scharr函數(shù)。該函數(shù)只針對大小為3的核,并且運算速率和sobel函數(shù)一樣快,結(jié)果更加精確,但抗噪性不如sobel函數(shù)。
使用scharr濾波器計算x或y方向的圖像差分,它的參數(shù)變量和sobel一樣。
函數(shù):imgproc.scharr(mat src, mat dst, int ddepth, int dx, int dy, double scale, double delta, int bordertype)
參數(shù)說明:
src:源圖像
dst:檢測結(jié)果圖像
ddepth:輸出圖像的深度
dx:x方向上的差分階數(shù)
dy:y方向上的差分階數(shù)
scale:縮放因子
delta:結(jié)果存入輸出圖像前可選的delta值,默認(rèn)為0
bordertype:邊界模式,默認(rèn)border_default
示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static void main(string[] args) { system.loadlibrary(core.native_library_name); mat src = imgcodecs.imread( "f:\\2011031213205880528.jpg" ); mat dst = src.clone(); mat dstx = src.clone(); mat dsty = src.clone(); imgproc.gaussianblur(src, dst, new size( 3 , 3 ), 0 ); imgproc.cvtcolor(dst, dst, imgproc.color_rgb2gray); imgproc.scharr(dst, dstx, - 1 , 1 , 0 , 1 , 0 , core.border_default); imgcodecs.imwrite( "f:\\dstx.jpg" , dstx); imgproc.scharr(dst, dsty, - 1 , 0 , 1 , 1 , 0 , core.border_default); imgcodecs.imwrite( "f:\\dsty.jpg" , dsty); core.addweighted(dstx, 0.5 , dsty, 0.5 , 0 , dst); imgcodecs.imwrite( "f:\\dst.jpg" , dst); } |
源圖像:
x方向的scharr:
y方向的scharr:
合并梯度后:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/m1109048058/article/details/77412371