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()
相关推荐
苹果二14 分钟前
【案例说明】能源行业中融合知识图谱、LLM与AI Agent的知识工程实践
人工智能·ai智能体·pem·phm·能源行业·知识工程·pqm
陈童学哦15 分钟前
NestJS集成LangChain两条路:封装库躺平 vs 手动硬撸
人工智能
markvivv16 分钟前
【译】适合在RTX 5090、DGX Spark或类似机器上可运行的最佳新模型是什么?
大数据·人工智能·spark
caimouse17 分钟前
ReactOS 窗口系统分析(31):TextOutW 文本输出全链路 — 从用户函数到显示缓冲区的旅程
网络·人工智能·计算机视觉
sunneo18 分钟前
每周GitCode开源项目精选
人工智能
zhangfeng113320 分钟前
HiDevLab vCANNLab(昇腾两个云端WebIDE)安装codebuddy
人工智能·ai编程·算子开发
leeyi20 分钟前
Agent 间 Transfer 交接:用户在不同 Agent 间无缝切换(第93篇-E79)
人工智能·aigc·agent
很楠爱上22 分钟前
从“AI 看合同”到可举证的合同决策链:CounterClause(对薄) 的架构设计与工程实践
人工智能·经验分享·python·学习·agent
Java后端的Ai之路23 分钟前
02、Python普通工厂模式
开发语言·人工智能·python·设计模式·普通工厂模式
尋找記憶的魚25 分钟前
CV总结《一》
图像处理·神经网络·opencv