易妖游戏网
您的当前位置:首页opencv--边缘检测

opencv--边缘检测

来源:易妖游戏网

1 边缘检测原理

边缘检测是图像处理和计算机视觉中的基本问题,边缘检测的目的是标识数字图像中亮度变化明显的点。图像属性中的显著变化通常反映了属性的重要事件和变化。
边缘的表现形式如下所示:

  • 基于搜索:通过寻找图像一阶导数中的最大值来检测边界,然后利用计算结果估计边缘的局部方向,通常采用梯度的方向,并利用此方向找到局部梯度模的最大值,代表算法有:Sobel算子和Scharr算子。
  • 基于零穿越:通过寻找图像二阶导数零穿越来寻找边界,代表算法是Laplacian算子。

2 Sobel检测算子

Sobel检测算法较简单,在实际应用中效率比canny检测算法效率更高,但检测精度不如canny准确。Sobel检测算子是高斯平滑和微分操作的结合体,因此其抗噪声能力很强,用途较多。

2.1 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(x1)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(x1)
假设要处理的图像为 I I I,在两个方向(水平和垂直)上求导:

  • 水平变化:将图像 I I I与奇数大小的模板进行卷积,结果为 G x G_x Gx。例如:当模板大小为3时, G x G_x Gx为:
    G x = ∣ − 1 0 + 1 − 2 0 + 2 − 1 0 + 1 ∣ ∗ I G_x = \begin{vmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{vmatrix} * I Gx=121000+1+2+1I
  • 垂直变化:将图像 I I I与奇数大小的模板进行卷积,结果为 G y G_y Gy。例如:当模板大小为3时, G y G_y Gy为:
    G y = ∣ − 1 − 2 − 1 0 0 0 + 1 + 2 + 1 ∣ ∗ I G_y = \begin{vmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{vmatrix} * I Gy=10+120+210+1I
    在图像的每一点,结合以上两个结果求出:
    G = G x 2 + G y 2 G = \sqrt{G_x^{2} + G_y^{2}} G=Gx2+Gy2
    之后统计极大值所在的位置,就是图像的边缘 ( x l , y l ) (x_l, y_l) (xl,yl)

注意:当内核大小为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=3103000+3+10+3I
垂直方向:
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+3100+1030+3I

2.2 应用

利用OpenCV进行sobel边缘检测的API:Sobel_x_or_y = cv2.Sobel(src, ddepth, dx, dy, dst, ksize, scale, delta, borderType)
参数:

  • src:传入的图像
  • ddepth: 图像的深度
  • dx和dy: 指求导的阶数,0表示这个方向上没有求导,取值为0、1。
  • ksize: 是Sobel算子的大小,即卷积核的大小,必须为奇数1、3、5、7,默认为3。注意:如果ksize=-1,就演变成为3x3的Scharr算子。
  • scale:缩放导数的比例常数,默认情况为没有伸缩系数
  • borderType:图像边界的模式,默认值为cv2.BORDER_DEFAULT。

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)

3 Laplacian算子

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=x22src+y22src
不连续函数的二阶导数为:
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(x1)
那么使用的卷积核应为:
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=010141010
API接口:laplacian = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])
参数:

  • Src: 需要处理的图像,
  • Ddepth: 图像的深度,-1表示采用的是原图像相同的深度,目标图像的深度必须大于等于原图像的深度;
  • ksize:算子的大小,即卷积核的大小,必须为1,3,5,7。
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()

4. Canny边缘检测

Canny 边缘检测算法是一种非常流行的边缘检测算法,是 John F. Canny 于 1986年提出的,被认为是最优的边缘检测算法。

4.1 原理

Canny边缘检测算法是由4步构成,分别是:

4.2应用

在OpenCV中要实现Canny检测使用的API:canny = cv2.Canny(image, threshold1, threshold2)
参数:

  • image:灰度图
  • threshold1: minval,较小的阈值将间断的边缘连接起来
  • threshold2: maxval,较大的阈值检测图像中明显的边缘
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()

5 算子比较

因篇幅问题不能全部显示,请点此查看更多更全内容