1.知识原理,摘录自https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html

Canny Edge Detection

Goal

In this chapter, we will learn about

  • Concept of Canny edge detection
  • OpenCV functions for that : cv2.Canny()

Theory

Canny Edge Detection is a popular edge detection algorithm. It was developed by John F. Canny in 1986. It is a multi-stage algorithm and we will go through each stages.

  1. Noise Reduction

Since edge detection is susceptible to noise in the image, first step is to remove the noise in the image with a 5x5 Gaussian filter. We have already seen this in previous chapters.

  1. Finding Intensity Gradient of the Image

Smoothened image is then filtered with a Sobel kernel in both horizontal and vertical direction to get first derivative in horizontal direction (G_x) and vertical direction (G_y). From these two images, we can find edge gradient and direction for each pixel as follows:

Edge\_Gradient \; (G) = \sqrt{G_x^2 + G_y^2}
Angle \; (\theta) = \tan^{-1} \bigg(\frac{G_y}{G_x}\bigg)

Gradient direction is always perpendicular to edges. It is rounded to one of four angles representing vertical, horizontal and two diagonal directions.

  1. Non-maximum Suppression

After getting gradient magnitude and direction, a full scan of image is done to remove any unwanted pixels which may not constitute the edge. For this, at every pixel, pixel is checked if it is a local maximum in its neighborhood in the direction of gradient. Check the image below:

Point A is on the edge ( in vertical direction). Gradient direction is normal to the edge. Point B and C are in gradient directions. So point A is checked with point B and C to see if it forms a local maximum. If so, it is considered for next stage, otherwise, it is suppressed ( put to zero).

In short, the result you get is a binary image with "thin edges".

  1. Hysteresis Thresholding

This stage decides which are all edges are really edges and which are not. For this, we need two threshold values, minVal and maxVal. Any edges with intensity gradient more than maxVal are sure to be edges and those below minVal are sure to be non-edges, so discarded. Those who lie between these two thresholds are classified edges or non-edges based on their connectivity. If they are connected to "sure-edge" pixels, they are considered to be part of edges. Otherwise, they are also discarded. See the image below:

The edge A is above the maxVal, so considered as "sure-edge". Although edge C is below maxVal, it is connected to edge A, so that also considered as valid edge and we get that full curve. But edge B, although it is above minVal and is in same region as that of edge C, it is not connected to any "sure-edge", so that is discarded. So it is very important that we have to select minVal and maxVal accordingly to get the correct result.

This stage also removes small pixels noises on the assumption that edges are long lines.

So what we finally get is strong edges in the image.

Canny Edge Detection in OpenCV

OpenCV puts all the above in single function, cv2.Canny(). We will see how to use it. First argument is our input image. Second and third arguments are our minVal and maxVal respectively. Third argument is aperture_size. It is the size of Sobel kernel used for find image gradients. By default it is 3. Last argument is L2gradient which specifies the equation for finding gradient magnitude. If it is True, it uses the equation mentioned above which is more accurate, otherwise it uses this function: Edge\_Gradient \; (G) = |G_x| + |G_y|. By default, it is False.

import cv2
import numpy as np
from matplotlib import pyplot as pltimg = cv2.imread('messi5.jpg',0)
edges = cv2.Canny(img,100,200)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()

See the result below:

.

2.我的实践

import cv2
import numpy as npimg = cv2.imread('C:\Users\Yi\Desktop\MAI\CV\image.jpg',0)
edges = cv2.Canny(img,100,200)
cv2.namedWindow("Edge Image")
cv2.imshow("Edge Image",edges)
cv2.waitKey (0)
cv2.destroyAllWindows() 

结果如下,嘻嘻~~

转载于:https://www.cnblogs.com/yizhaoAI/p/8655276.html

Conputer Vision-边缘检测-OpenCV实践相关推荐

  1. 使用openCV进行边缘检测、二值化、轮廓、轮廓检测、BGR、灰度图、二值化,专栏:各种openCV实践的案例

    专栏连接:openCV练习-各种openCV实践的案例 前言 使用OpenCV的轮廓检测,当我们加入对象的边界上的所有点时,我们得到一个轮廓. 通常,特定轮廓区域与边界像素有关,具有相似的颜色和强度. ...

  2. 图像边缘检测--OpenCV之cvCanny函数

    图像边缘检测--OpenCV之cvCanny函数 分类: C/C++ void cvCanny( const CvArr* image, CvArr* edges, double threshold1 ...

  3. OpenCV实践小项目(一): 信用卡数字识别

    1. 写在前面 今天整理一个OpenCV实践的小项目, 前几天整理了一篇OpenCV处理图像的知识笔记,后面,就通过一些小项目把这些知识运用到实践中去,一个是加深理解,另一个是融会贯通,连成整体,因为 ...

  4. HOG:从理论到OpenCV实践

    HOG:从理论到OpenCV实践 时间 2014-03-11 22:55:57 CSDN博客 原文  http://blog.csdn.net/zhazhiqiang/article/details/ ...

  5. OpenCV实践之路——人脸识别之一数据收集和预处理

    本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/51386949 微博:http ...

  6. OpenCV实践之路——雅虎色情图片检测神经网络试用报告

    本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/52821497 微博:http ...

  7. 使用OpenCV进行图像修复、Navier-Stokes、INPAINT_TELEA,专栏:各种openCV实践的案例

    专栏连接:openCV练习-各种openCV实践的案例 前言 使用OpenCV进行图像修复,在ps里面有现成的功能,但是今天使用代码尝试了下,感觉效果还不错. 这个代码对比较细长的划痕.破损修复的效果 ...

  8. 【Opencv】【OpenCV实践】【OpenCV的使用学习记录】【fmt学习记录】

    [Opencv][OpenCV实践][OpenCV的使用学习记录][fmt学习记录] 0 前言 1 opencv使用说明 1.1 头文件的使用 1.2 CMakeLists.txt的使用 1.3 代码 ...

  9. Python OpenCV实践,相机标定

    Python OpenCV实践,相机标定 前言 准备棋盘格 标定相机 图像去畸变 前言 本篇主要是使用python opencv标定相机内参和畸变参数的记录,主要参考opencv官方文档中的示例. 本 ...

  10. OpenCV实践之路——人脸识别之三识别自己的脸

    本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/51472330 微博:http ...

最新文章

  1. CLASSPATH的作用
  2. 【原创】如何在 Linux 下调整可打开文件/文件描述符数目
  3. python:实现简单的web开发demo
  4. @Transactional事务生效条件与样例
  5. vant-UI组件初使用:浅谈 - 解说篇
  6. Spring学习总结(20)——Spring加载多个项目properties配置文件问题解决
  7. 【干货分享】自己总结录制的web前端精讲视频,零基础入门学习资料,开发工具
  8. jsp代码编写简单的BBS论坛项目的总结
  9. OMRON继电器基础讲解
  10. 海马模拟器怎么连接android studio
  11. 刘顺琦 - CSCI 561 mid 1definition
  12. [小程序] 微信开发者工具下载与安装 WXMLfor if 模板页面引用 tabBar
  13. VSCode安装教程
  14. Camunda流程引擎 Modeler (二)
  15. 关闭outlook express系统提示压缩邮件导致邮件丢失
  16. 时钟数字 java_用java写一个数字时钟
  17. github学生开发者包_2020年GitHub学生开发包指南
  18. 【lnmp】require(): open_basedir restriction in effect.【百度不是万能的,关键时刻还得靠自己】
  19. CDSN文章下载代码
  20. 杰理之KeyPage【篇】

热门文章

  1. 【David Silver强化学习公开课之一】强化学习入门
  2. Distributed Systems笔记-NFS、AFS、GFS
  3. android6.0源码分析之Camera API2.0下的初始化流程分析
  4. 深入理解Android的startservice和bindservice
  5. JZOJ 5623. 【NOI2018模拟4.2】program
  6. ajax 参数大小限制,Ajax中的POST数据大小是否有限制?
  7. php 访问 memcache,memcache+php实现页面访问的加速
  8. db2 删除索引_MySQL 选错索引的原因是什么?
  9. MATLAB入门级知识
  10. abb变频器如何就地增加频率_abb变频器报接地故障如何处理,故障原因分析