首页 技术 正文
技术 2022年11月18日
0 收藏 367 点赞 5,091 浏览 4631 个字

Canny 边缘检测算法


Steps:

  1. 高斯滤波平滑
  2. 计算梯度大小和方向
  3. 非极大值抑制
  4. 双阈值检测和连接

代码结构:

Canny Edge Detection
|Gaussian_Smoothing
||convolution.py
|||convolution()
||gaussion_smoothing.py
|||dnorm()
|||gaussian_kernel()
|||gaussian_blur()
|Sobel_Filter
||sobel.py
|||sobel_edge_detection()
|Canny.py
||non_max_suppression()
||threshold()
||hysteresis()
||main()

代码解读:


1. 高斯滤波平滑

  • 创建一个高斯核(kernel_size=5):

    【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)

  • 执行卷积和平均操作(以下均以 lenna 图为例)

    【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)

2. 计算梯度大小和方向

水平方向和竖直方向

【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)
【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)

梯度图:

【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)

3. 非极大值抑制

【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)

4. 双阈值检测和连接

【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)


以下是代码:

import numpy as np
import cv2
import argparsefrom Computer_Vision.Canny_Edge_Detection.sobel import sobel_edge_detection
from Computer_Vision.Canny_Edge_Detection.gaussian_smoothing import gaussian_blurimport matplotlib.pyplot as pltdef non_max_suppression(gradient_magnitude, gradient_direction, verbose):
image_row, image_col = gradient_magnitude.shape output = np.zeros(gradient_magnitude.shape) PI = 180 for row in range(1, image_row - 1):
for col in range(1, image_col - 1):
direction = gradient_direction[row, col] if (0 <= direction < PI / 8) or (15 * PI / 8 <= direction <= 2 * PI):
before_pixel = gradient_magnitude[row, col - 1]
after_pixel = gradient_magnitude[row, col + 1] elif (PI / 8 <= direction < 3 * PI / 8) or (9 * PI / 8 <= direction < 11 * PI / 8):
before_pixel = gradient_magnitude[row + 1, col - 1]
after_pixel = gradient_magnitude[row - 1, col + 1] elif (3 * PI / 8 <= direction < 5 * PI / 8) or (11 * PI / 8 <= direction < 13 * PI / 8):
before_pixel = gradient_magnitude[row - 1, col]
after_pixel = gradient_magnitude[row + 1, col] else:
before_pixel = gradient_magnitude[row - 1, col - 1]
after_pixel = gradient_magnitude[row + 1, col + 1] if gradient_magnitude[row, col] >= before_pixel and gradient_magnitude[row, col] >= after_pixel:
output[row, col] = gradient_magnitude[row, col] if verbose:
plt.imshow(output, cmap='gray')
plt.title("Non Max Suppression")
plt.show() return outputdef threshold(image, low, high, weak, verbose=False):
output = np.zeros(image.shape) strong = 255 strong_row, strong_col = np.where(image >= high)
weak_row, weak_col = np.where((image <= high) & (image >= low)) output[strong_row, strong_col] = strong
output[weak_row, weak_col] = weak if verbose:
plt.imshow(output, cmap='gray')
plt.title("threshold")
plt.show() return outputdef hysteresis(image, weak):
image_row, image_col = image.shape top_to_bottom = image.copy() for row in range(1, image_row):
for col in range(1, image_col):
if top_to_bottom[row, col] == weak:
if top_to_bottom[row, col + 1] == 255 or top_to_bottom[row, col - 1] == 255 or top_to_bottom[row - 1, col] == 255 or top_to_bottom[
row + 1, col] == 255 or top_to_bottom[
row - 1, col - 1] == 255 or top_to_bottom[row + 1, col - 1] == 255 or top_to_bottom[row - 1, col + 1] == 255 or top_to_bottom[
row + 1, col + 1] == 255:
top_to_bottom[row, col] = 255
else:
top_to_bottom[row, col] = 0 bottom_to_top = image.copy() for row in range(image_row - 1, 0, -1):
for col in range(image_col - 1, 0, -1):
if bottom_to_top[row, col] == weak:
if bottom_to_top[row, col + 1] == 255 or bottom_to_top[row, col - 1] == 255 or bottom_to_top[row - 1, col] == 255 or bottom_to_top[
row + 1, col] == 255 or bottom_to_top[
row - 1, col - 1] == 255 or bottom_to_top[row + 1, col - 1] == 255 or bottom_to_top[row - 1, col + 1] == 255 or bottom_to_top[
row + 1, col + 1] == 255:
bottom_to_top[row, col] = 255
else:
bottom_to_top[row, col] = 0 right_to_left = image.copy() for row in range(1, image_row):
for col in range(image_col - 1, 0, -1):
if right_to_left[row, col] == weak:
if right_to_left[row, col + 1] == 255 or right_to_left[row, col - 1] == 255 or right_to_left[row - 1, col] == 255 or right_to_left[
row + 1, col] == 255 or right_to_left[
row - 1, col - 1] == 255 or right_to_left[row + 1, col - 1] == 255 or right_to_left[row - 1, col + 1] == 255 or right_to_left[
row + 1, col + 1] == 255:
right_to_left[row, col] = 255
else:
right_to_left[row, col] = 0 left_to_right = image.copy() for row in range(image_row - 1, 0, -1):
for col in range(1, image_col):
if left_to_right[row, col] == weak:
if left_to_right[row, col + 1] == 255 or left_to_right[row, col - 1] == 255 or left_to_right[row - 1, col] == 255 or left_to_right[
row + 1, col] == 255 or left_to_right[
row - 1, col - 1] == 255 or left_to_right[row + 1, col - 1] == 255 or left_to_right[row - 1, col + 1] == 255 or left_to_right[
row + 1, col + 1] == 255:
left_to_right[row, col] = 255
else:
left_to_right[row, col] = 0 final_image = top_to_bottom + bottom_to_top + right_to_left + left_to_right final_image[final_image > 255] = 255 return final_imageif __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
ap.add_argument("-v", "--verbose", type=bool, default=False, help="Path to the image")
args = vars(ap.parse_args()) image = cv2.imread(args["image"]) blurred_image = gaussian_blur(image, kernel_size=9, verbose=False) edge_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) gradient_magnitude, gradient_direction = sobel_edge_detection(blurred_image, edge_filter, convert_to_degree=True, verbose=args["verbose"]) new_image = non_max_suppression(gradient_magnitude, gradient_direction, verbose=args["verbose"]) weak = 50 new_image = threshold(new_image, 5, 20, weak=weak, verbose=args["verbose"]) new_image = hysteresis(new_image, weak) plt.imshow(new_image, cmap='gray')
plt.title("Canny Edge Detector")
plt.show()

References

hahahha

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900