opencv学习十四:Canny边缘检测算法

Canny边缘检测

cannny算法介绍
在这里插入图片描述
非极大值抑制:在获得梯度和方向,去除所有不是边界的点。
实现方向:逐渐遍历像素点,判断当前像素点是否是周围像素点中具有相同方向梯度的最大值。是保留,不是则为0.

在这里插入图片描述在这里插入图片描述
cannny代码实现
edges=cv2.Canny(image,threshold1,threshold2)
edges:处理结果
image:原始图像
threshold1:minVal
threshold2:maxVal
如果想让边界细节更多,则把threshold1和threshold2设小些。
代码如下:

import cv2 as cv
import numpy as np

def edge_demo(image):
    blurred = cv.GaussianBlur(image, (3, 3), 0)#为什么要GaussianBlur?
    #因为blur可以降低噪声,Canny算法对噪声敏感,降噪之后效果要好,也不能模糊太厉害,去掉了边缘信息。
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    #X Gradient
    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0) #x和y方向的结果
    # Y Gradient
    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
    #edge
    edge_output = cv.Canny(xgrad, ygrad, 50, 150)#低阈值50.高阈值150
    #或者直接用cv.Canny
    #edge_output = cv.Canny(gray, 50, 150)#低阈值50.高阈值150
    cv.imshow("Canny Edge", edge_output)

    dst = cv.bitwise_and(image, image, mask=edge_output)
    cv.imshow("Color Edge", dst)


src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/lena.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
edge_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

运行截图:
先计算梯度 后使用Canny算法
在这里插入图片描述

不计算梯度 直接使用Canny算法
在这里插入图片描述

blurred = cv.GaussianBlur(image, (3, 3), 0)#高斯模糊,降低噪声。canny对噪声比较敏感,也不能模糊太厉害,去掉了边缘信息。
edge_output = cv.Canny(xgrad, ygrad, 50, 150)
#edge_output = cv.Canny(gray, 50, 150)
是相同的
高阈值应该是低阈值的3倍

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以使用Python的NumPy和Matplotlib库来实现Canny边缘检测算法。以下是一个简单的实现示例: ```python import numpy as np import matplotlib.pyplot as plt def canny_edge_detection(image, sigma=1, kernel_size=5, low_threshold=0.1, high_threshold=0.3): # Convert image to grayscale gray = np.mean(image, axis=2) # Apply Gaussian blur blurred = np.zeros_like(gray) kernel = np.zeros((kernel_size, kernel_size)) for i in range(kernel_size): for j in range(kernel_size): kernel[i, j] = np.exp(-((i - kernel_size // 2) ** 2 + (j - kernel_size // 2) ** 2) / (2 * sigma ** 2)) kernel /= np.sum(kernel) for i in range(kernel_size // 2, gray.shape[0] - kernel_size // 2): for j in range(kernel_size // 2, gray.shape[1] - kernel_size // 2): blurred[i, j] = np.sum(gray[i - kernel_size // 2:i + kernel_size // 2 + 1, j - kernel_size // 2:j + kernel_size // 2 + 1] * kernel) # Compute gradient magnitude and direction dx = np.zeros_like(blurred) dy = np.zeros_like(blurred) for i in range(1, blurred.shape[0] - 1): for j in range(1, blurred.shape[1] - 1): dx[i, j] = blurred[i, j + 1] - blurred[i, j - 1] dy[i, j] = blurred[i + 1, j] - blurred[i - 1, j] magnitude = np.sqrt(dx ** 2 + dy ** 2) direction = np.arctan2(dy, dx) # Non-maximum suppression suppressed = np.zeros_like(magnitude) for i in range(1, magnitude.shape[0] - 1): for j in range(1, magnitude.shape[1] - 1): angle = direction[i, j] * 180 / np.pi if angle < 0: angle += 180 if (angle >= 0 and angle < 22.5) or (angle >= 157.5 and angle < 180): if magnitude[i, j] >= magnitude[i, j - 1] and magnitude[i, j] >= magnitude[i, j + 1]: suppressed[i, j] = magnitude[i, j] elif (angle >= 22.5 and angle < 67.5): if magnitude[i, j] >= magnitude[i - 1, j - 1] and magnitude[i, j] >= magnitude[i + 1, j + 1]: suppressed[i, j] = magnitude[i, j] elif (angle >= 67.5 and angle < 112.5): if magnitude[i, j] >= magnitude[i - 1, j] and magnitude[i, j] >= magnitude[i + 1, j]: suppressed[i, j] = magnitude[i, j] elif (angle >= 112.5 and angle < 157.5): if magnitude[i, j] >= magnitude[i - 1, j + 1] and magnitude[i, j] >= magnitude[i + 1, j - 1]: suppressed[i, j] = magnitude[i, j] # Double thresholding and edge tracking low_threshold *= np.max(suppressed) high_threshold *= np.max(suppressed) edges = np.zeros_like(suppressed) strong_i, strong_j = np.where(suppressed >= high_threshold) weak_i, weak_j = np.where((suppressed >= low_threshold) & (suppressed < high_threshold)) edges[strong_i, strong_j] = 1 while len(weak_i) > 0: i, j = weak_i[0], weak_j[0] weak_i, weak_j = np.delete(weak_i, 0), np.delete(weak_j, 0) if edges[i + 1, j] == 1 or edges[i - 1, j] == 1 or edges[i, j + 1] == 1 or edges[i, j - 1] == 1 or edges[i + 1, j + 1] == 1 or edges[i - 1, j - 1] == 1 or edges[i + 1, j - 1] == 1 or edges[i - 1, j + 1] == 1: edges[i, j] = 1 return edges # Test the function on a sample image image = plt.imread('lena.png') edges = canny_edge_detection(image) plt.imshow(edges, cmap='gray') plt.show() ``` 这个实现使用了高斯滤波器来平滑图像,计算梯度幅值和方向,进行非极大值抑制,双阈值处理和边缘跟踪。可以通过调整参数来控制算法的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值