OpenCV-Canny边缘检测


title: Canny边缘检测算法

Canny边缘检测算法基本原理

  1. 图像灰度化
  2. 高斯滤波
  3. 用一阶偏导的有限差分来计算梯度的幅值和方向
  4. 对梯度幅值进行非极大值抑制
  5. 用双阈值算法检测和连接边缘

参考:
参考文档

OpenCV中的实现

代码实现:

"""
Canny边缘检测的概念 - OpenCV函数: cv.Canny()
Canny算法
"""

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread(r'C:\Users\admin\Desktop\OpenCV\people.jpg', 0)
'''
Canny(image, threshold1, threshold2, edges=None, apertureSize=None, L2gradient=None):
.   @param image 8-bit input image.
.   @param edges output edge map; single channels 8-bit image, which has the same size as image.
.   @param threshold1 first threshold for the hysteresis procedure.(minVal)
.   @param threshold2 second threshold for the hysteresis procedure.(maxVal, >maxVal ---> 255)
.   @param apertureSize aperture size for the Sobel operator.
.   @param L2gradient a flag, indicating whether a more accurate \f$L_2\f$ norm\f$=\sqrt{(dI/dx)^2 + (dI/dy)^2}\f$
    should be used to calculate the image gradient magnitude (L2gradient=true ), 
    or whether the default \f$L_1\f$ norm \f$=|dI/dx|+|dI/dy|\f$ is enough (L2gradient=false ).
'''
edges = cv.Canny(img, 50, 60)
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(edges, cmap='gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

Soble算子计算梯度

sobel算子主要用于获得数字图像的一阶梯度,常见的应用和物理意义是边缘检测。
原理:
算子使用两个33的矩阵(图1)算子使用两个33的矩阵(图1)去和原始图片作卷积,分别得到横向G(x)和纵向G(y)的梯度值,如果梯度值大于某一个阈值,则认为该点为边缘点

Gx方向的相关模板:
图1

Gy方向的相关模板:
图2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值