当前位置:   article > 正文

Canny边缘检测原理及python实现_八连通区域填充算法程序python

八连通区域填充算法程序python
  1. Canny边缘检测原理
    从表面效果上来讲,Canny算法是对Sobel、Prewitt等算子效果的进一步细化和更加准确的定位。
    Canny算法基于三个基本目标:
    a. 低错误率。所有边缘都应被找到,且没有伪响应。
    b. 边缘点应该被很好地定位。已定位的边缘必须尽可能接近真实边缘。
    c. 单一的边缘点响应。这意味在仅存一个单一边缘点的位置,检测器不应指出多个像素边缘。
    进而,Canny的工作本质是,从数学上表达前面的三个准则。因此Canny的步骤如下:
    a. 对输入图像进行高斯平滑,降低错误率。
    b. 计算梯度幅度和方向来估计每一点处的边缘强度与方向。
    c. 根据梯度方向,对梯度幅值进行非极大值抑制。本质上是对Sobel、Prewitt等算子结果的进一步细化。
    d. 用双阈值处理和连接边缘。
  2. 高斯平滑操作
    在这里插入图片描述
# 第一步:高斯平滑滤波
img = cv2.imread("lena.jpg", 0)
img = cv2.GaussianBlur(img, (3, 3), 2)
  • 1
  • 2
  • 3
  1. 计算梯度幅度和方向,找到边缘强度
    可选用的模板:soble算子、Prewitt算子、Roberts模板等等;在这里插入图片描述

一般采用soble算子,OpenCV也是如此,利用soble水平和垂直算子与输入图像卷积计算dx、dy:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
进一步可以得到图像梯度的幅值:
在这里插入图片描述
为了简化计算,幅值也可以作如下近似:
在这里插入图片描述
角度为:
在这里插入图片描述
如下图表示了中心点的梯度向量、方位角以及边缘方向(任一点的边缘与梯度向量正交) :
在这里插入图片描述

# 第二步:计算每一点的梯度幅值与方向,找到边缘强度
img1 = np.zeros(img.shape, dtype="uint8")  # 与原图大小相同
theta = np.zeros(img.shape, dtype="float")  # 方向矩阵原图像大小
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])]
        Gx = [np.sum(m1 * img[i - 1:i + 2, j - 1:j + 2])]

        # 计算角度
        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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  1. 非极大值抑制
    划重点:是沿着梯度方向对幅值进行非极大值抑制,而非边缘方向。
    在这里插入图片描述
    在这里插入图片描述
    量化情况可总结为:
    水平边缘–梯度方向为垂直: 在这里插入图片描述
    135°边缘–梯度方向为45°:在这里插入图片描述
    垂直边缘–梯度方向为水平:在这里插入图片描述
    45°边缘–梯度方向为135°: 在这里插入图片描述
    非极大值抑制即为沿着上述4种类型的梯度方向,比较3*3邻域内对应邻域值的大小:
    在这里插入图片描述
    在每一点上,领域中心 x 与沿着其对应的梯度方向的两个像素相比,若中心像素为最大值,则保留,否则中心置0,这样可以抑制非极大值,保留局部梯度最大的点,以得到细化的边缘。
# 第三步:非极大值抑制计算
img2 = np.zeros(img1.shape)  # 非极大值抑制图像矩阵
# 非极大值抑制即为沿着上述4种类型的梯度方向,比较3*3邻域内对应邻域值的大小:
for i in range(1, img2.shape[0] - 1):
    for j in range(1, img2.shape[1] - 1):
        # 0度为水平边缘
        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]
        # -45度边缘
        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]
        # 90度垂直边缘
        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]
        # 45度边缘
        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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  1. 双阈值方法进行二值化,并连接边缘在这里插入图片描述

a. 选取系数TH和TL,比率为2:1或3:1。(一般取TH=0.3或0.2,TL=0.1);
b. 将小于低阈值的点抛弃,赋0;将大于高阈值的点立即标记(这些点为确定边缘点),赋1或255;
c. 将小于高阈值,大于低阈值的点使用8连通区域确定(即:只有与TH像素连接时才会被接受,成为边缘点,赋 1或255)

# 第四步:双阈值检测和边缘连接
img3 = np.zeros(img2.shape)  # 定义双阈值图像
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
        # 将小于高阈值,大于低阈值的点使用8连通区域确定(即:只有与TH像素连接时才会被接受,成为边缘点,赋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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  1. 完整代码
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]])

# 第一步:高斯平滑滤波
img = cv2.imread("lena.jpg", 0)
sobel = cv2.Canny(img, 50, 100)
cv2.imshow("canny", sobel)  # 角度值灰度图
img = cv2.GaussianBlur(img, (3, 3), 2)

# 第二步:计算每一点的梯度幅值与方向,找到边缘强度
img1 = np.zeros(img.shape, dtype="uint8")  # 与原图大小相同
theta = np.zeros(img.shape, dtype="float")  # 方向矩阵原图像大小
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])]
        Gx = [np.sum(m1 * img[i - 1:i + 2, j - 1:j + 2])]

        # 计算角度
        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

# 第三步:非极大值抑制计算
img2 = np.zeros(img1.shape)  # 非极大值抑制图像矩阵
# 非极大值抑制即为沿着上述4种类型的梯度方向,比较3*3邻域内对应邻域值的大小:
for i in range(1, img2.shape[0] - 1):
    for j in range(1, img2.shape[1] - 1):
        # 0度为水平边缘
        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]
        # -45度边缘
        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]
        # 90度垂直边缘
        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]
        # 45度边缘
        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 = 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
        # 将小于高阈值,大于低阈值的点使用8连通区域确定(即:只有与TH像素连接时才会被接受,成为边缘点,赋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.imshow("original_img", img)  # 原始图像
cv2.imshow("grad_img", img1)  # 梯度幅值图
cv2.imshow("max_img", img2)  # 非极大值抑制灰度图
cv2.imshow("final_img", img3)  # 最终效果图
cv2.waitKey(0)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  1. 运行结果
    original_img
    在这里插入图片描述

final_img

在这里插入图片描述
参考文献:https://blog.csdn.net/qq_43043256/article/details/102947533
cnblogs.com/april0315/p/13629813.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/838550
推荐阅读
相关标签
  

闽ICP备14008679号