【Python+OpenCV入门学习】十六、边缘检测

本篇文章介绍图像的边缘检测,如Sobel,Scharr,Laplacian,Canny等。

主要学习cv.Sobel(),cv.Scharr(),cv.Canny()等函数的使用。

环境:Windows 7(64)   Python 3.6    OpenCV 3.4.2

一、边缘检测

1.1相关函数介绍

Sobel()函数形式如下:

dst = cv.Sobel( src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]] )

功能:使用Sobel算子对图像进行处理。

参数:

src:输入图像。

dst:输出图像。

ddepth:输出图像的深度。

dx:给1,x方向进行计算

dy:给1,y方向进行计算

ksize:Sobel算子的大小

scale:缩放因子

delta:将处理的图像加上delta

borderType:边界填充类型

Scharr()函数形式如下:

dst = cv.Scharr( src, ddepth, dx, dy[, dst[, scale[, delta[, borderType]]]] )

功能:使用Scharr算子对图像进行处理。

参数:

src:输入图像。

dst:输出图像。

ddepth:输出图像的深度。

dx:给1,x方向进行计算

dy:给1,y方向进行计算

scale:缩放因子

delta:将处理的图像加上delta

borderType:边界填充类型

Laplacian()函数形式如下:

dst = cv.Laplacian( src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]] )

功能:使用Laplacian算子对图像进行处理。

参数:

src:输入图像。

dst:输出图像。

ddepth:输出图像的深度。

dx:给1,x方向进行计算

dy:给1,y方向进行计算

ksize:Sobel算子的大小

scale:缩放因子

delta:将处理的图像加上delta

borderType:边界填充类型

Canny()函数形式如下:

edges = cv.Canny( image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]] )

功能:使用Canny算子对图像进行处理。

参数:

src:输入图像。8bit图像

edges:输出图像。

threshold1:阈值1

threshold2:阈值2

apertureSize:孔径尺寸,默认3

L2gradient:是否使用L2范数,默认false

1.2 编程测试

代码如下:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('3.png',cv.IMREAD_GRAYSCALE)

#sobel dx
DstSobel = cv.Sobel(img, cv.CV_8U, 1, 0, ksize = 5)
#scharr dx
DstScharr = cv.Scharr(img, cv.CV_8U, 1, 0)
#Laplacian 
DstLaplacian = cv.Laplacian(img, cv.CV_8U, ksize = 5)
#Canny
DstCanny = cv.Canny(img, 100, 200)

#显示
plt.subplot(321),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(322),plt.imshow(DstSobel),plt.title('Sobel')
plt.xticks([]), plt.yticks([])
plt.subplot(323),plt.imshow(DstScharr),plt.title('Scharr')
plt.xticks([]), plt.yticks([])
plt.subplot(324),plt.imshow(DstLaplacian),plt.title('Laplacian')
plt.xticks([]), plt.yticks([])
plt.subplot(325),plt.imshow(DstCanny),plt.title('Canny')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果图如下:

                              

猜你喜欢

转载自blog.csdn.net/qq_18995069/article/details/88978714
今日推荐