圖像梯度
- 圖像梯度,圖像邊界等
- 使用到的函數(shù)有: cv2.Sobel(), cv2.Scharr(), cv2.Laplacian() 等
- 原理:梯度簡單來說就是求導。OpenCV 提供了三種不同的梯度濾波器,或者說高通濾波器: Sobel,Scharr 和 Laplacian。Sobel, Scharr 其實就是求一階或二階導數(shù)。 Scharr 是對 Sobel(使用小的卷積核求解求解梯度角度時)的優(yōu)化。 Laplacian 是求二階導數(shù)。
Sobel 算子和 Scharr 算子
Sobel 算子是高斯平滑與微分操作的結合體,所以它的抗噪聲能力很好。你可以設定求導的方向( xorder 或 yorder)。還可以設定使用的卷積核的大?。?ksize)。如果 ksize=-1,會使用 3x3 的 Scharr 濾波器,它的的效果要比 3x3 的 Sobel 濾波器好(而且速度相同,所以在使用 3x3 濾波器時應該盡量使用 Scharr 濾波器)。 3x3 的 Scharr 濾波器卷積核如下:
X
方向
=
[
?
3
0
3
?
10
0
10
?
3
0
3
]
,
Y
方向
=
[
?
3
?
10
?
3
0
0
0
3
10
3
]
X方向=\left[ \begin{matrix} -3&0&3\\-10&0&10\\-3&0&3 \end{matrix}\right],Y方向=\left[ \begin{matrix} -3&-10&-3\\0&0&0\\3&10&3\end{matrix}\right]
X方向=
??3?10?3?000?3103?
?,Y方向=
??303??10010??303?
?
Laplacian 算子
拉普拉斯算子可以使用二階導數(shù)的形式定義,可假設其離散實現(xiàn)類似于二階 Sobel 導數(shù),事實上, OpenCV 在計算拉普拉斯算子時直接調(diào)用 Sobel 算子。
拉普拉斯濾波器使用的卷積核:
k
e
r
n
e
l
=
[
0
1
0
1
?
4
1
0
1
0
]
kernel=\left[ \begin{matrix} 0&1&0\\1&-4&1\\0&1&0\end{matrix}\right]
kernel=
?010?1?41?010?
?
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('./resource/opencv/image/sudoku.png', cv2.IMREAD_GRAYSCALE)
# 拉普拉斯 cv2.CV_64F 輸出圖像的深度(數(shù)據(jù)類型),可以使用-1, 與原圖像保持一致 np.uint8
laplacian = cv2.Laplacian(img, cv2.CV_64F, ksize=3)
laplacian = cv2.convertScaleAbs(laplacian)
# 索貝爾 X方向, 參數(shù) 1,0 為只在 x 方向求一階導數(shù),最大可以求 2 階導數(shù)
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobelx = cv2.convertScaleAbs(sobelx)
# 索貝爾 Y方向, 參數(shù) 0,1 為只在 y 方向求一階導數(shù),最大可以求 2 階導數(shù)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
sobely = cv2.convertScaleAbs(sobely)
# Scharr X方向,
scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharrx = cv2.convertScaleAbs(scharrx)
# Scharr Y方向,
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharry = cv2.convertScaleAbs(scharry)
plt.subplot(321), plt.imshow(img, cmap='gray'), plt.title('Origin'), plt.xticks([]), plt.yticks([])
plt.subplot(322), plt.imshow(laplacian, cmap='gray'), plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(323), plt.imshow(sobelx, cmap='gray'), plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(324), plt.imshow(sobely, cmap='gray'), plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.subplot(325), plt.imshow(scharrx, cmap='gray'), plt.title('Scharr X'), plt.xticks([]), plt.yticks([])
plt.subplot(326), plt.imshow(scharry, cmap='gray'), plt.title('Scharr Y'), plt.xticks([]), plt.yticks([])
plt.show()
文章來源:http://www.zghlxwxcb.cn/news/detail-636591.html
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('./resource/opencv/image/box2.png')
# output dtype = cv2.CV_8U
sobelx8u = cv2.Sobel(img, cv2.CV_8U, 1,0, ksize=5)
# 也可以將參數(shù)設置為-1
sobelx8u_n = cv2.Sobel(img, -1, 1, 0, ksize=5)
# output dtype = cv2.CV64F,
sobelx64f = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
abs_sobel64f = np.absolute(sobelx64f)
sobel_8u = np.uint8(abs_sobel64f)
# Scharr X方向
scharrx = cv2.Scharr(img,cv2.CV_64F,1,0)
# Scharr Y方向
scharry = cv2.Scharr(img,cv2.CV_64F,0,1)
plt.subplot(2,3,1), plt.imshow(img, cmap='gray'), plt.title('original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,3,2), plt.imshow(sobelx8u, cmap='gray'), plt.title('Sobel CV_8U'), plt.xticks([]), plt.yticks([])
plt.subplot(2,3,3), plt.imshow(sobel_8u, cmap='gray'), plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([])
plt.subplot(2,3,4), plt.imshow(scharrx, cmap='gray'), plt.title('Scharr X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,3,5), plt.imshow(scharry, cmap='gray'), plt.title('Scharr Y'), plt.xticks([]), plt.yticks([])
plt.show()
文章來源地址http://www.zghlxwxcb.cn/news/detail-636591.html
到了這里,關于Python-OpenCV中的圖像處理-圖像梯度的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!