电子商务网站建设评估工具有哪些,wordpress置顶失败,找生产厂家的网站,域名备案要多少钱canny边缘检测原理canny边缘检测共有5部分组成#xff0c;下边我会分别来介绍。1 高斯模糊(略)2 计算梯度幅值和方向。可选用的模板#xff1a;soble算子、Prewitt算子、Roberts模板等等;一般采用soble算子#xff0c;OpenCV也是如此#xff0c;利用soble水平和垂直算子与输…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。(一般取TH0.3或0.2,TL0.1)2 将小于低阈值的点抛弃赋0将大于高阈值的点立即标记(这些点为确定边缘 点)赋1或2553将小于高阈值大于低阈值的点使用8连通区域确定(即只有与TH像素连接时才会被接受成为边缘点赋 1或255)python 实现import cv2import numpy as npm1 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, dtypeuint8) # 与原图大小相同theta np.zeros(img.shape, dtypefloat) # 方向矩阵原图像大小img cv2.copyMakeBorder(img, 1, 1, 1, 1, borderTypecv2.BORDER_REPLICATE)rows, cols img.shapefor 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] 90continueelse:temp ((np.arctan2(Gy[0], Gx[0])) * 180 / np.pi)90if 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.0elif (((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.0elif (((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.0elif (((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.0for 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] 90continueelse: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.0elif (((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.0elif (((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.0elif (((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 50TH 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] 0elif img2[i, j] TH:img3[i, j] 255elif ((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] 255cv2.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)运行结果如下以上就是python实现canny边缘检测的详细内容更多关于canny边缘检测的资料请关注我们其它相关文章本文标题: python实现canny边缘检测本文地址: http://www.cppcns.com/jiaoben/python/345130.html