python实现canny边缘检测
作者:凉拌豌豆尖 发布时间:2022-03-05 00:24:08
canny边缘检测原理
canny边缘检测共有5部分组成,下边我会分别来介绍。
1 高斯模糊(略)
2 计算梯度幅值和方向。
可选用的模板:soble算子、Prewitt算子、Roberts模板等等;
一般采用soble算子,OpenCV也是如此,利用soble水平和垂直算子与输入图像卷积计算dx、dy:
进一步可以得到图像梯度的幅值:
为了简化计算,幅值也可以作如下近似:
角度为:
如下图表示了中心点的梯度向量、方位角以及边缘方向(任一点的边缘与梯度向量正交) :
θ = θm = arctan(dy/dx)(边缘方向)
α = θ + 90= arctan(dy/dx) + 90(梯度方向)
3、根据角度对幅值进行非极大值抑制
划重点:是沿着梯度方向对幅值进行非极大值抑制,而非边缘方向,这里初学者容易弄混。
例如:3*3区域内,边缘可以划分为垂直、水平、45°、135°4个方向,同样,梯度反向也为四个方向(与边缘方向正交)。因此为了进行非极大值,将所有可能的方向量化为4个方向,如下图:
即梯度方向分别为
α = 90
α = 45
α = 0
α = -45
非极大值抑制即为沿着上述4种类型的梯度方向,比较3*3邻域内对应邻域值的大小:
在每一点上,领域中心 x 与沿着其对应的梯度方向的两个像素相比,若中心像素为最大值,则保留,否则中心置0,这样可以抑制非极大值,保留局部梯度最大的点,以得到细化的边缘。
4、用双阈值算法检测和连接边缘
1选取系数TH和TL,比率为2:1或3:1。(一般取TH=0.3或0.2,TL=0.1);
2 将小于低阈值的点抛弃,赋0;将大于高阈值的点立即标记(这些点为确定边缘 点),赋1或255;
3将小于高阈值,大于低阈值的点使用8连通区域确定(即:只有与TH像素连接时才会被接受,成为边缘点,赋 1或255)
python 实现
import cv2
import numpy as np
m1 = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
m2 = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
from matplotlib import pyplot as plt
# 第一步:完成高斯平滑滤波
img = cv2.imread("B9064CF1D57871735CE11A0F368DCF27.jpg", 0)
sobel = cv2.Canny(img, 50, 100)
cv2.namedWindow('5', 0)
cv2.resizeWindow("5", 640, 480)
cv2.imshow("5", sobel) # 角度值灰度图
img = cv2.GaussianBlur(img, (3, 3), 2)
# 第二步:完成一阶有限差分计算,计算每一点的梯度幅值与方向
img1 = np.zeros(img.shape, dtype="uint8") # 与原图大小相同
theta = np.zeros(img.shape, dtype="float") # 方向矩阵原图像大小
img = cv2.copyMakeBorder(img, 1, 1, 1, 1, borderType=cv2.BORDER_REPLICATE)
rows, cols = img.shape
for i in range(1, rows - 1):
for j in range(1, cols - 1):
Gy = [np.sum(m2 * img[i - 1:i + 2, j - 1:j + 2])]
#Gy = (np.dot(np.array([1, 1, 1]), (m2 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))
Gx = [np.sum(m1 * img[i - 1:i + 2, j - 1:j + 2])]
#Gx = (np.dot(np.array([1, 1, 1]), (m1 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))
if Gx[0] == 0:
theta[i - 1, j - 1] = 90
continue
else:
temp = ((np.arctan2(Gy[0], Gx[0])) * 180 / np.pi)+90
if Gx[0] * Gy[0] > 0:
if Gx[0] > 0:
# 第一象线
theta[i - 1, j - 1] = np.abs(temp)
else:
# 第三象线
theta[i - 1, j - 1] = (np.abs(temp) - 180)
if Gx[0] * Gy[0] < 0:
if Gx[0] > 0:
# 第四象线
theta[i - 1, j - 1] = (-1) * np.abs(temp)
else:
# 第二象线
theta[i - 1, j - 1] = 180 - np.abs(temp)
img1[i - 1, j - 1] = (np.sqrt(Gx[0] ** 2 + Gy[0] ** 2))
for i in range(1, rows - 2):
for j in range(1, cols - 2):
if (((theta[i, j] >= -22.5) and (theta[i, j] < 22.5)) or
((theta[i, j] <= -157.5) and (theta[i, j] >= -180)) or
((theta[i, j] >= 157.5) and (theta[i, j] < 180))):
theta[i, j] = 0.0
elif (((theta[i, j] >= 22.5) and (theta[i, j] < 67.5)) or
((theta[i, j] <= -112.5) and (theta[i, j] >= -157.5))):
theta[i, j] = -45.0
elif (((theta[i, j] >= 67.5) and (theta[i, j] < 112.5)) or
((theta[i, j] <= -67.5) and (theta[i, j] >= -112.5))):
theta[i, j] = 90.0
elif (((theta[i, j] >= 112.5) and (theta[i, j] < 157.5)) or
((theta[i, j] <= -22.5) and (theta[i, j] >= -67.5))):
theta[i, j] = 45.0
'''
for i in range(1, rows - 1):
for j in range(1, cols - 1):
Gy = [np.sum(m2 * img[i - 1:i + 2, j - 1:j + 2])]
#Gy = (np.dot(np.array([1, 1, 1]), (m2 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))
Gx = [np.sum(m1 * img[i - 1:i + 2, j - 1:j + 2])]
#Gx = (np.dot(np.array([1, 1, 1]), (m1 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))
if Gx[0] == 0:
theta[i - 1, j - 1] = 90
continue
else:
temp = (np.arctan2(Gy[0], Gx[0])) * 180 / np.pi)
if Gx[0] * Gy[0] > 0:
if Gx[0] > 0:
# 第一象线
theta[i - 1, j - 1] = np.abs(temp)
else:
# 第三象线
theta[i - 1, j - 1] = (np.abs(temp) - 180)
if Gx[0] * Gy[0] < 0:
if Gx[0] > 0:
# 第四象线
theta[i - 1, j - 1] = (-1) * np.abs(temp)
else:
# 第二象线
theta[i - 1, j - 1] = 180 - np.abs(temp)
img1[i - 1, j - 1] = (np.sqrt(Gx[0] ** 2 + Gy[0] ** 2))
for i in range(1, rows - 2):
for j in range(1, cols - 2):
if (((theta[i, j] >= -22.5) and (theta[i, j] < 22.5)) or
((theta[i, j] <= -157.5) and (theta[i, j] >= -180)) or
((theta[i, j] >= 157.5) and (theta[i, j] < 180))):
theta[i, j] = 90.0
elif (((theta[i, j] >= 22.5) and (theta[i, j] < 67.5)) or
((theta[i, j] <= -112.5) and (theta[i, j] >= -157.5))):
theta[i, j] = 45.0
elif (((theta[i, j] >= 67.5) and (theta[i, j] < 112.5)) or
((theta[i, j] <= -67.5) and (theta[i, j] >= -112.5))):
theta[i, j] = 0.0
elif (((theta[i, j] >= 112.5) and (theta[i, j] < 157.5)) or
((theta[i, j] <= -22.5) and (theta[i, j] >= -67.5))):
theta[i, j] = -45.0
'''
# 第三步:进行 非极大值抑制计算
img2 = np.zeros(img1.shape) # 非极大值抑制图像矩阵
for i in range(1, img2.shape[0] - 1):
for j in range(1, img2.shape[1] - 1):
# 0度j不变
if (theta[i, j] == 0.0) and (img1[i, j] == np.max([img1[i, j], img1[i + 1, j], img1[i - 1, j]])):
img2[i, j] = img1[i, j]
if (theta[i, j] == -45.0) and img1[i, j] == np.max([img1[i, j], img1[i - 1, j - 1], img1[i + 1, j + 1]]):
img2[i, j] = img1[i, j]
if (theta[i, j] == 90.0) and img1[i, j] == np.max([img1[i, j], img1[i, j + 1], img1[i, j - 1]]):
img2[i, j] = img1[i, j]
if (theta[i, j] == 45.0) and img1[i, j] == np.max([img1[i, j], img1[i - 1, j + 1], img1[i + 1, j - 1]]):
img2[i, j] = img1[i, j]
# 第四步:双阈值检测和边缘连接
img3 = np.zeros(img2.shape) # 定义双阈值图像
# TL = 0.4*np.max(img2)
# TH = 0.5*np.max(img2)
TL = 50
TH = 100
# 关键在这两个阈值的选择
for i in range(1, img3.shape[0] - 1):
for j in range(1, img3.shape[1] - 1):
if img2[i, j] < TL:
img3[i, j] = 0
elif img2[i, j] > TH:
img3[i, j] = 255
elif ((img2[i + 1, j] < TH) or (img2[i - 1, j] < TH) or (img2[i, j + 1] < TH) or
(img2[i, j - 1] < TH) or (img2[i - 1, j - 1] < TH) or (img2[i - 1, j + 1] < TH) or
(img2[i + 1, j + 1] < TH) or (img2[i + 1, j - 1] < TH)):
img3[i, j] = 255
cv2.namedWindow('1', 0)
cv2.resizeWindow("1", 640, 480)
cv2.namedWindow('2', 0)
cv2.resizeWindow("2", 640, 480)
cv2.namedWindow('3', 0)
cv2.resizeWindow("3", 640, 480)
cv2.namedWindow('4', 0)
cv2.resizeWindow("4", 640, 480)
cv2.imshow("1", img) # 原始图像
cv2.imshow("2", img1) # 梯度幅值图
cv2.imshow("3", img2) # 非极大值抑制灰度图
cv2.imshow("4", img3) # 最终效果图
cv2.waitKey(0)
运行结果如下
来源:http://www.uml.org.cn/python/202009022.asp
猜你喜欢
- asp使用fso读取驱动器信息:<%vv=drive()response.write vv funct
- 前言1.工作中,经常需要合并多个Excel文件。如果文件数量比较多,则工作量大,易出错,此时,可以使用Python来快速的完成合并。2.使用
- 摘要global 标志实际上是为了提示 python 解释器,表明被其修饰的变量是全局变量。这样解释器就可以从当前空间 (curr
- 本文实例讲述了Python使用Selenium模块模拟浏览器抓取斗鱼直播间信息。分享给大家供大家参考,具体如下:import timefro
- 本文整理了3种鼠标经过图片,图片边框加粗或改变颜色的方法,希望大家喜欢。下面3中只是提供了一个方法,具体的鼠标经过图片的样式,你自己可以修改
- 今天项目中遇到了中文编码的zip文件,处理了蛮长时间,所以记录下,以免下次踩坑。Python2下Python2中读取zip文件,zipfil
- 算法11、分小数和整数:如果小数点个数为1,则可能是小数。如小数点个数为0,则可能是整数。小数点个数非0也非1,那么就不是数字。2、如果是小
- CSS Sprites 简介:通常被意译为“CSS图像拼合”或“CSS贴图定位”。CSS Sprites并不是一门新技术,目前它已经在网页开
- 本文和大家重点讨论一下Perl split函数的用法,Perl中的一个非常有用的函数是Perl split函数-把字符串进行分割并把分割后的
- 代码如下:CREATE DATABASE 临时的 ON ( NAME='临时的_Data', FILENAME='h
- python logging日志模块的详解日志级别日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR C
- 前几天玩了玩Google的Map API,感觉还不错,很简单。但凡有过任何编程经验的同学,看完以下的教程,都可以在10分钟内掌握它的主要功能
- 使用python编写了共六种图像增强算法:1)基于直方图均衡化2)基于拉普拉斯算子3)基于对数变换4)基于伽马变换5)限制对比度自适应直方图
- 目录1. 警告不是异常2. 警告能捕获吗3. 捕获警告方法一4. 捕获警告方法二5. 捕获警告方法三1. 警告不是异常你是不是经常在使用一些
- ASP+javascript实现可显示和隐藏的树型菜单实例:<script language=&qu
- 前言在迷宫问题中,给定入口和出口,要求找到路径。本文将讨论三种求解方法,递归求解、回溯求解和队列求解。在介绍具体算法之前,先考虑将迷宫数字化
- key_buffer_size - 这对MyISAM表来说非常重要。如果只是使用MyISAM表,可以把它设置为可用内存的 30-40%。合理
- 1.安装时选择的自动安装,忘了用户名和密码导致现在试了几个Oracle默认用户名密码後(表格中附带默认用户名及密码),都提示无效的用户名、密
- 使用方法:terminal中输入python weather.py http://www.weather.com.cn/weather/10
- 异步编程一直是JavaScript 编程的重大事项。关于异步方案, ES6 先是出现了 基于状态管理的 Promise,然后出现了 Gene