0
点赞
收藏
分享

微信扫一扫

边缘检测

1.边缘检测用于表示图像中连读明显的点

边缘检测分为两种:一种是基于搜索,另外一种是基于零穿越‘

2.基于搜索:是求函数的一阶导数,基于零穿越试求二阶导数的零点’

3.Sobel算子Scahrr算子是基于搜索的边缘检测

Sobel算子API:Sobel_x_or_y = cv2.Sobel(src,ddepth,dx,dy,ksize,scale
,delta,borderType)
参数:

ddepth:图像深度

dx和dy:求导的阶数,0表示这个方向上面没有导数,取值为0、1

ksize:是Sobel算子的大小,即卷积核大小,必须为1,3,5,7,默认为3

如果ksize=-1,就演变成3*3的Scahrr算子

scale:缩放导数的比例常数,默认没有

boederType:图像便捷模式,默认值为cv2.BORDER_DEFAULT

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg',1)
#创建Sobel算子
Sobel_x = cv.Sobel(img,cv.CV_16S,1,0)
Sobel_y = cv.Sobel(img,cv.CV_16S,0,1)
#转换为uint8类型
Scale_AbsX = cv.convertScaleAbs(Sobel_x)
Scale_AbsY = cv.convertScaleAbs(Sobel_y)
res = cv.addWeighted(Scale_AbsX,0.5,Scale_AbsY,0.5,0)
plt.imshow(res,cmap=plt.cm.gray)
plt.show()
#创建Scharr算子
Sobel_x1 = cv.Sobel(img,cv.CV_16S,1,0,ksize=-1)
Sobel_y1 = cv.Sobel(img,cv.CV_16S,0,1,ksize=-1)
#转换
Scale_AbsX = cv.convertScaleAbs(Sobel_x1)
Scale_AbsY = cv.convertScaleAbs(Sobel_y1)
res1 = cv.addWeighted(Scale_AbsX,0.5,Scale_AbsY,0.5,0)
plt.imshow(res1,cmap=plt.cm.gray)
plt.show()

2.Laplacian算子是基于零穿越的边缘检测

API:laplacian = cv2.Laplacian(src,ddepth[,dst[,ksize[,scale[,selta
[,bordeType]]]]])

参数:

src:输入的图像

ddepth:图像深度,cv.CV.16S

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg', 0)
#拉普拉斯算子
res = cv.Laplacian(img,cv.CV_16S)
#转换为uint8
img_laplacian = cv.convertScaleAbs(res)
plt.imshow(img_laplacian,cmap=plt.cm.gray)
plt.show()

3.Canny算子

Canny边缘检测算法由四步组成:

1)噪声去除:5*5高斯滤波器取出去除噪声

2)计算图像梯度

3)非极大值抑制

4)滞后阈值:设计两个阈值,maxVal和minVal,灰度梯高于maxVal

被认为是真的边界,低于minVal呗抛弃,如果在之间就是边界点相连

2.API:canny:cv2.Canny(image,threshold1,threshold2)
参数:

threshold1:minval,较小的阈值将间断的边缘连接起来

threshold2:maxval,较大的阈值检测图像中明显的边缘

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg', 0)
#Canny边缘检测
res = cv.Canny(img,0,100)#阈值
plt.imshow(res,cmap=plt.cm.gray)
plt.show()

 



举报

相关推荐

0 条评论