边缘检测是图像处理和计算机视觉中的基本问题,边缘检测的目的是标识数字图像中亮度变化明显的点。图像属性中的显著变化通常反映了属性的重要事件和变化。
边缘的表现形式如下所示:
Sobel检测算法较简单,在实际应用中效率比canny检测算法效率更高,但检测精度不如canny准确。Sobel检测算子是高斯平滑和微分操作的结合体,因此其抗噪声能力很强,用途较多。
对于不连续的函数,一阶导数可写作:
f
′
(
x
)
=
f
(
x
)
−
f
(
x
−
1
)
或
f
′
(
x
)
=
f
(
x
+
1
)
−
f
(
x
)
f^{'}(x) = f(x) - f(x-1) \quad 或 \quad f^{'}(x) = f(x+1) - f(x)
f′(x)=f(x)−f(x−1)或f′(x)=f(x+1)−f(x)
所以有:
f
′
(
x
)
=
f
(
x
+
1
)
−
f
(
x
−
1
)
2
f^{'}(x) = \frac{f(x+1) - f(x-1)}{2}
f′(x)=2f(x+1)−f(x−1)
假设要处理的图像为
I
I
I,在两个方向(水平和垂直)上求导:
注意:当内核大小为3时,以上Sobel内核可能产生比较明显的误差, 为解决这一问题,可使用Scharr函数,但该函数仅作用于大小为3的内核。该函数的运算与Sobel函数一样快,但结果却更加精确,其计算方法为:
水平方向:
G
x
=
∣
−
3
0
+
3
−
10
0
+
10
−
3
0
+
3
∣
∗
I
G_x = \begin{vmatrix} -3 & 0 & +3 \\ -10 & 0 & +10 \\ -3 & 0 & +3 \end{vmatrix} * I
Gx=∣∣∣∣∣∣−3−10−3000+3+10+3∣∣∣∣∣∣∗I
垂直方向:
G
y
=
∣
−
3
−
10
−
3
0
0
0
+
3
+
10
+
3
∣
∗
I
G_y = \begin{vmatrix} -3 & -10 & -3 \\ 0 & 0 & 0 \\ +3 & +10 & +3 \end{vmatrix} * I
Gy=∣∣∣∣∣∣−30+3−100+10−30+3∣∣∣∣∣∣∗I
利用OpenCV进行sobel边缘检测的API:Sobel_x_or_y = cv2.Sobel(src, ddepth, dx, dy, dst, ksize, scale, delta, borderType)
参数:
Sobel函数求完导数后会有负值,还有会大于255的值。而原图像是uint8,即8位无符号数,所以Sobel建立的图像位数不够,会有截断。因此要使用16位有符号的数据类型,即cv2.CV_16S。处理完图像后,再使用cv2.convertScaleAbs()函数将其转回原来的uint8格式,否则图像无法显示。
Sobel算子是在两个方向计算的,最后还需要用cv2.addWeighted( )函数将其组合起来
Scale_abs = cv2.convertScaleAbs(x) # 格式转换函数
result = cv2.addWeighted(src1, alpha, src2, beta) # 图像混合
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 1 读取图像
img = cv.imread('./image/horse.jpg',0)
# 2 计算Sobel卷积结果
x = cv.Sobel(img, cv.CV_16S, 1, 0)
y = cv.Sobel(img, cv.CV_16S, 0, 1)
# 3 将数据进行转换
Scale_absX = cv.convertScaleAbs(x) # convert 转换 scale 缩放
Scale_absY = cv.convertScaleAbs(y)
# 4 结果合成
result = cv.addWeighted(Scale_absX, 0.5, Scale_absY, 0.5, 0)
# 5 图像显示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img,cmap=plt.cm.gray),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(result,cmap = plt.cm.gray),plt.title('Sobel滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
x = cv.Sobel(img, cv.CV_16S, 1, 0, ksize = -1)
y = cv.Sobel(img, cv.CV_16S, 0, 1, ksize = -1)
Laplacian是利用二阶导数来检测边缘。由于图像是二维数据,需要在两个方向上进行求导,如下是所示:
Δ
s
r
c
=
∂
2
∂
x
2
s
r
c
+
∂
2
∂
y
2
s
r
c
\Delta src = \frac{\partial^2}{\partial x^2}{src} + \frac{\partial^2}{\partial y^2}{src}
Δsrc=∂x2∂2src+∂y2∂2src
不连续函数的二阶导数为:
f
′
′
(
x
)
=
f
′
(
x
+
1
)
−
f
′
(
x
)
=
f
(
x
+
1
)
+
f
(
x
−
1
)
f^{''}(x) = f^{'}(x+1) - f^{'}(x) = f(x+1) + f(x-1)
f′′(x)=f′(x+1)−f′(x)=f(x+1)+f(x−1)
那么使用的卷积核应为:
k
e
r
n
e
l
=
∣
0
1
0
1
−
4
1
0
1
0
∣
kernel = \begin{vmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{vmatrix}
kernel=∣∣∣∣∣∣0101−41010∣∣∣∣∣∣
API接口:laplacian = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])
参数:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 1 读取图像
img = cv.imread('./image/horse.jpg',0)
# 2 laplacian转换
result = cv.Laplacian(img,cv.CV_16S)
Scale_abs = cv.convertScaleAbs(result)
# 3 图像展示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img,cmap=plt.cm.gray),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(Scale_abs,cmap = plt.cm.gray),plt.title('Laplacian检测后结果')
plt.xticks([]), plt.yticks([])
plt.show()
Canny 边缘检测算法是一种非常流行的边缘检测算法,是 John F. Canny 于 1986年提出的,被认为是最优的边缘检测算法。
Canny边缘检测算法是由4步构成,分别是:
在OpenCV中要实现Canny检测使用的API:canny = cv2.Canny(image, threshold1, threshold2)
参数:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 1 图像读取
img = cv.imread('./image/horse.jpg',0)
# 2 Canny边缘检测
lowThreshold = 0
max_lowThreshold = 100
canny = cv.Canny(img, lowThreshold, max_lowThreshold)
# 3 图像展示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img,cmap=plt.cm.gray),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(canny,cmap = plt.cm.gray),plt.title('Canny检测后结果')
plt.xticks([]), plt.yticks([])
plt.show()
因篇幅问题不能全部显示,请点此查看更多更全内容