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):

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

2. 计算梯度大小和方向

水平方向和竖直方向


梯度图:

3. 非极大值抑制

4. 双阈值检测和连接


以下是代码:

import numpy as np
import cv2
import argparse from Computer_Vision.Canny_Edge_Detection.sobel import sobel_edge_detection
from Computer_Vision.Canny_Edge_Detection.gaussian_smoothing import gaussian_blur import matplotlib.pyplot as plt def 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 output def 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 output def 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_image if __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实现 Canny Edge Detection(Canny 边缘检测算法)的更多相关文章

  1. 猜数字游戏--基于python

    """题目:练习使用python写一个猜数字的游戏,数字范围0-100,每次猜错,需要给出缩小后的范围,每个人只有10次的猜测机会,猜测机会用完游戏结束!"&q ...

  2. 【笔记】基于Python的数字图像处理

    [博客导航] [Python相关] 前言 基于Python的数字图像处理,离不开相关处理的第三方库函数.搜索网络资源,列出如下资源链接. Python图像处理库到底用哪家 python计算机视觉编程— ...

  3. Image Processing and Analysis_8_Edge Detection:Scale-space and edge detection using anisotropic diffusion——1990

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  4. Image Processing and Analysis_8_Edge Detection:A Computational Approach to Edge Detection——1986

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  5. Image Processing and Analysis_8_Edge Detection:Theory of Edge Detection ——1980

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  6. 计算机视觉中的边缘检测Edge Detection in Computer Vision

    计算机视觉中的边缘检测   边缘检测是计算机视觉中最重要的概念之一.这是一个很直观的概念,在一个图像上运行图像检测应该只输出边缘,与素描比较相似.我的目标不仅是清晰地解释边缘检测是怎样工作的,同时也提 ...

  7. Image Processing and Analysis_8_Edge Detection:Edge Detection Revisited ——2004

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  8. Image Processing and Analysis_8_Edge Detection:Local Scale Control for Edge Detection and Blur Estimation——1998

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  9. Image Processing and Analysis_8_Edge Detection: Optimal edge detection in two-dimensional images ——1996

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

随机推荐

  1. Java面试题和解答(五)

    1.在Java中Executor和Executors的区别? Executor是线程池的顶层接口,它的实现类如下图所示: Executors是一个类,提供了多个静态方法,用于生成不同类型的线程池,如下 ...

  2. Python中的模块引用机制

    一.模块引用 Def: 在Python 程序中使用另一个文件定义的类(方法).函数.数据等 被引用模块位置.通常 Python2 : "/Library/Python/2.7/site-pa ...

  3. javascript中的定时器入门

    JavaScript提供定时器(timer)的功能,可以延期执行或重复执行函数或代码段. window对象提供了三个方法来实现定时器的效果,分别是setTimeout().setInternal()和 ...

  4. SSM整合3(springMVC+mybatis)

    一.RequestMapping URL路径映射:置于方法上,可多个URL映射同一个方法,格式:@RequestMapping(value="item")或@RequestMapp ...

  5. Java中的“浅复制”与“深复制”

    复制 将一个对象的引用复制给另一个对象,一共有三种方式.第一种方式是直接赋值,第二种方式是浅复制,第三种方式是深复制. 1.直接赋值 在Java中,A a1 = a2,这实际上复制的是引用,也就是说 ...

  6. PostgreSQL update set from 两表联合更新,注意与其它数据库更新语法有差别

    最近用PostgreSql数据库进行表关联更新时,发现与之前用的Sql Server 和My Sql语法有很大差别,稍微不注意,很容易出错. PostgreSql表更新时,两个表只允许一个表起别名,一 ...

  7. JQuery iframe宽高度自适应浏览器窗口大小的解决方法

    iframe宽高度自适应浏览器窗口大小的解决方法   by:授客 QQ:1033553122 1.   测试环境 JQuery-3.2.1.min.js 下载地址: https://gitee.com ...

  8. 使用NodeJS模块-NodeJS官方提供的核心模块

    除了使用自己写的本地模块以外,NodeJS可以使用另外两种类型的模块,分别是NodeJS官方提供的核心模块和第三方提供的模块 NodeJS官方提供的核心模块 NodeJS平台自带的一套基本的功能模块, ...

  9. [转载] Java的四种引用关系

    目录 1 强引用 (Final Reference) 2 软引用 (Soft Reference) 2.1 案例1: 软引用的垃圾回收 2.2 案例2: 软引用缓存的使用 2.3 软引用的应用场景 3 ...

  10. myeclipse 9.0 破解方法,亲测可用

    MyEclipse 9.0的破解方法,步骤如下: 1.破解公钥,确保MyEclipse没有开启,否则失败! 用WinRAR打开Common\plugins\com.genuitec.eclipse.c ...