OpenCV-Python实战(11)——边缘检测

一、Sobel 算子

通过 X 梯度核与 Y 梯度核求得图像在,水平与垂直方向的梯度。

python 复制代码
img = cv2.Sobel(src=*,ddepth=*,dx=*,dy=*,ksize=*,scale=*,delta=*,borderType=*)

**img:**目标图像。

**src:**原始图像。

**ddepth:**目标图像深度,-1 代表与原始图像深度相同。

**dx、dy:**x或y 轴方向的求导阶数,可以为:0、1、3 等。0 表示不求导。

**ksize:**Soble核大小。

**scale:**导数计算的缩放系数,默认为:1。

**delta:**常数项,默认为:0。

**borderType:**边界样式,使用默认即可。

python 复制代码
import cv2

img = cv2.imread('jin.png')
dst_x = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
# 取梯度的绝对值
dst_x = cv2.convertScaleAbs(dst_x)
dst_y = cv2.convertScaleAbs(dst_y)

dst = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)

cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
python 复制代码
import cv2

img = cv2.imread('Lena.png')[::2,::2,:]
dst_x = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
# 取梯度的绝对值
dst_x = cv2.convertScaleAbs(dst_x)
dst_y = cv2.convertScaleAbs(dst_y)

dst = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)

cv2.imshow('img',img)
cv2.imshow('Sobel',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

二、Scharr 算子

python 复制代码
img = cv2.Scharr(src=*,ddepth=*,dx=*,dy=*,ksize=*,scale=*,delta=*,borderType=*)

**img:**目标图像。

**src:**原始图像。

**ddepth:**目标图像深度,-1 代表与原始图像深度相同。

**dx、dy:**x或y 轴方向的求导阶数,可以为:0、1、3 等。0 表示不求导。

**ksize:**Soble核大小。

**scale:**导数计算的缩放系数,默认为:1。

**delta:**常数项,默认为:0。

**borderType:**边界样式,使用默认即可。

python 复制代码
import cv2

img = cv2.imread('Lena.png')[::2,::2,:]
cv2.imshow('img',img)
# Sobel 算子
dst_x = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
dst_x = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
dst_y = cv2.convertScaleAbs(dst_y)
dst_Sobel = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)
cv2.imshow('Sobel',dst_Sobel)

# Scharr 算子
dst_x = cv2.Scharr(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Scharr(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
dst_x = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
dst_y = cv2.convertScaleAbs(dst_y)
dst_Scharr = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)
cv2.imshow('Scharr',dst_Scharr)

cv2.waitKey(0)
cv2.destroyAllWindows()

三、Laplacian 算子

python 复制代码
img = cv2.Laplacian(src=*,ddepth=*,ksize=*,scale=*,delta=*,borderType=*)

**img:**目标图像。

**src:**原始图像。

**ddepth:**目标图像深度,-1 代表与原始图像深度相同。

**ksize:**Soble核大小。

**scale:**导数计算的缩放系数,默认为:1。

**delta:**常数项,默认为:0。

**borderType:**边界样式,使用默认即可。

python 复制代码
import cv2

img = cv2.imread('Lena.png')[::2,::2,:]
cv2.imshow('img',img)
# Sobel 算子
dst_x = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
dst_x = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
dst_y = cv2.convertScaleAbs(dst_y)
dst_Sobel = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)
cv2.imshow('Sobel',dst_Sobel)

# Sobel 算子
dst_x = cv2.Scharr(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Scharr(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
dst_x = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
dst_y = cv2.convertScaleAbs(dst_y)
dst_Scharr = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)
cv2.imshow('Scharr',dst_Scharr)

# Laplacian 算子
dst = cv2.Laplacian(src=img,ddepth=cv2.CV_32F,ksize=3)
dst_Laplacian = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
cv2.imshow('Laplacian',dst_Laplacian)

cv2.waitKey(0)
cv2.destroyAllWindows()

四、Canny 边缘检测

python 复制代码
img = cv2.Canny(image=*,edges=*,threshold1=*,threshold2=*,apertureSize=*,L2gradient=False)

**img:**目标图像。

**image:**原始图像。

edges:边缘数。

threshold1、threshold2:minVal 和 maxVal。

apertureSize:运算符大小。

L2gradient:梯度公式:默认为False,;如果为Ture则:

python 复制代码
import cv2

img = cv2.imread('Lena.png')[::2,::2,:]
cv2.imshow('img',img)
# Sobel 算子
dst_x = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Sobel(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
dst_x = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
dst_y = cv2.convertScaleAbs(dst_y)
dst_Sobel = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)
cv2.imshow('Sobel',dst_Sobel)

# Sobel 算子
dst_x = cv2.Scharr(src=img,ddepth=cv2.CV_32F,dx=1,dy=0)
dst_y = cv2.Scharr(src=img,ddepth=cv2.CV_32F,dx=0,dy=1)
dst_x = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
dst_y = cv2.convertScaleAbs(dst_y)
dst_Scharr = cv2.addWeighted(dst_x,0.5,dst_y,0.5,0)
cv2.imshow('Scharr',dst_Scharr)

# Laplacian 算子
dst = cv2.Laplacian(src=img,ddepth=cv2.CV_32F,ksize=3)
dst_Laplacian = cv2.convertScaleAbs(dst_x) # 取梯度的绝对值
cv2.imshow('Laplacian',dst_Laplacian)

# Canny 算子
dst_Canny = cv2.Canny(image=img,threshold1=50,threshold2=100)
cv2.imshow('Canny',dst_Canny)

cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
zxsz_com_cn14 小时前
设备健康管理大数据平台:工业智能化的核心数据引擎
运维·人工智能
算家计算14 小时前
破5亿用户!国产AI模型成功逆袭,成为AI普及浪潮主角
人工智能·开源·资讯
Jolie_Liang14 小时前
国内金融领域元宇宙金融特殊需求与技术挑战研究报告
人工智能·元宇宙
算家计算14 小时前
SAIL-VL2本地部署教程:2B/8B参数媲美大规模模型,为轻量级设备量身打造的多模态大脑
人工智能·开源·aigc
Costrict14 小时前
解锁新阵地!CoStrict 现已支持 JetBrains 系列 IDE
大数据·ide·人工智能·深度学习·自然语言处理·ai编程·visual studio
姚家湾14 小时前
MAC mini /绿联NAS 上安装本地AFFiNE
人工智能·affine
Python智慧行囊15 小时前
图像处理-opencv(二)-形态学
人工智能·计算机视觉
阿里云大数据AI技术15 小时前
云栖实录|阿里云 Milvus:AI 时代的专业级向量数据库
大数据·人工智能·搜索引擎
机器之心15 小时前
太强了!DeepSeek刚刚开源新模型,用视觉方式压缩一切
人工智能·openai
救救孩子把15 小时前
18-机器学习与大模型开发数学教程-第1章 1-10 本章总结与习题
人工智能·数学·机器学习