转载:https://blog.csdn.net/cumtb2002/article/details/107798767

Modules used:

使用的模块:

For this, we will use the opencv-python module which provides us various functions to work on images.

为此,我们将使用opencv-python模块,该模块为我们提供了处理图像的各种功能。

Download opencv-python

下载opencv-python

  1.  
    General Way:
  2.  
    pip install opencv-python
  3.  
     
  4.  
    Pycharm Users:
  5.  
    Go to the project Interpreter and install this module from there.

opencv-python Module:

opencv-python模块:

opencv-python is a python library that will solve the Computer Vision Problems and provides us various functions to edit the Images.

opencv-python是一个python库,它将解决计算机视觉问题并为我们提供编辑图像的各种功能。

Note: The edge Detection is possible only in grayscale Image.

注意:只能在灰度图像中进行边缘检测。

What we will do in this script?

我们将在此脚本中做什么?

To detect the edges of the images we will use opencv-python various Functions and Provide thresholds.

为了检测图像的边缘,我们将使用opencv-python的各种功能并提供阈值。

In this article we will detect the edge of the Image with the help of various functions and the accuracy of edge increases as we go down,

在本文中,我们将借助各种功能来检测图像的边缘,并且当我们下降时边缘的精度会提高,

  • Sobel Function: This Function will create the Horizontal and vertical edges and after that, we will use the Bitwise or operator to combine them

    Sobel函数 :此函数将创建水平边缘和垂直边缘,然后,我们将使用按位或运算符将它们组合

  • Laplacian Function: This Function is the simplest Function in which we just have to put the Grayscale Variable into it, and we will get the edge detected image.

    拉普拉斯函数 :此函数是最简单的函数,只需要将灰度变量放入其中,就可以得到边缘检测到的图像。

  • Canny Function: This is the most powerful function for edge detection and most accurate.

    Canny功能 :这是边缘检测功能最强大且最准确的功能。

Let's see the code:

让我们看一下代码:

1)使用Sobel函数 (1) Using Sobel Function)

  1.  
    # importing the module
  2.  
    import cv2
  3.  
     
  4.  
    # read the image and store the data in a variable
  5.  
    image=cv2.imread("/home/abhinav/PycharmProjects/untitled1/b.jpg")
  6.  
     
  7.  
    # make it grayscale
  8.  
    Gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  9.  
     
  10.  
    # Make it with the help of sobel
  11.  
    # make the sobel_horizontal
  12.  
    # For horizontal x axis=1 and yaxis=0
  13.  
    # for vertical x axis=0 and y axis=1
  14.  
    Horizontal=cv2.Sobel(Gray,0,1,0,cv2.CV_64F)
  15.  
     
  16.  
    # the thresholds are like
  17.  
    # (variable,0,<x axis>,<y axis>,cv2.CV_64F)
  18.  
    Vertical=cv2.Sobel(Gray,0,0,1,cv2.CV_64F)
  19.  
     
  20.  
    # DO the Bitwise operation
  21.  
    Bitwise_Or=cv2.bitwise_or(Horizontal,Vertical)
  22.  
     
  23.  
    # Show the Edged Image
  24.  
    cv2.imshow("Sobel Image",Bitwise_Or)
  25.  
    cv2.imshow("Original Image",Gray)
  26.  
    cv2.waitKey(0)
  27.  
    cv2.destroyAllWindows()

Output:

输出:

2)拉普拉斯函数 (2) Laplacian Function)

  1.  
    # importing the module
  2.  
    import cv2
  3.  
     
  4.  
    # read the image and store the data in a variable
  5.  
    image=cv2.imread("/home/abhinav/PycharmProjects/untitled1/b.jpg")
  6.  
     
  7.  
    # make it grayscale
  8.  
    Gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  9.  
     
  10.  
    # Make Laplacian Function
  11.  
    Lappy=cv2.Laplacian(Gray,cv2.CV_64F)
  12.  
     
  13.  
    cv2.imshow("Laplacian",Lappy)
  14.  
    cv2.imshow("Original",Gray)
  15.  
    cv2.waitKey(0)
  16.  
    cv2.destroyAllWindows()

Output:

输出:

3)使用Canny函数 (3) Using Canny Function)

  1.  
    # importing the module
  2.  
    import cv2
  3.  
     
  4.  
    # read the image and store the data in a variable
  5.  
    image=cv2.imread("/home/abhinav/PycharmProjects/untitled1/b.jpg")
  6.  
     
  7.  
    # make it grayscale
  8.  
    Gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  9.  
     
  10.  
    # Make canny Function
  11.  
    canny=cv2.Canny(Gray,40,140)
  12.  
     
  13.  
    # the threshold is varies bw 0 and 255
  14.  
    cv2.imshow("Canny",canny)
  15.  
    cv2.imshow("Original",Gray)
  16.  
    cv2.waitKey(0)
  17.  
    cv2.destroyAllWindows()

Output:

输出:

翻译自: https://www.includehelp.com/python/edge-detection-of-image-using-opencv-cv2.aspx

在Python中使用OpenCV(CV2)对图像进行边缘检测的更多相关文章

  1. OpenCV-Python(1)在Python中使用OpenCV进行人脸检测

    OpenCV是如今最流行的计算机视觉库,而我们今天就是要学习如何安装使用OpenCV,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...

  2. Python下的OpenCV学习 02 —— 图像的读取与保存

    OpenCV提供了众多对图片操作的函数,其中最基本的就是图片的读取与输出了. 一.读取图片 利用OpenCV读取一张图片是非常容易的,只需要用到 imread() 函数,打开shell或者cmd,进入 ...

  3. python中使用Opencv进行车牌号检测——2018.10.24

    初学Python.Opencv,想用它做个实例解决车牌号检测. 车牌号检测需要分为四个部分:1.车辆图像获取.2.车牌定位.3.车牌字符分割和4.车牌字符识别 在百度查到了车牌识别部分车牌定位和车牌字 ...

  4. python中使用Opencv进行人脸识别

    上一节讲到人脸检测,现在讲一下人脸识别.具体是通过程序采集图像并进行训练,并且基于这些训练的图像对人脸进行动态识别. 人脸识别前所需要的人脸库可以通过两种方式获得:1.自己从视频获取图像   2.从人 ...

  5. python中使用Opencv进行人脸检测

    这两天学习了人脸识别,看了学长写的代码,边看边码边理解搞完了一边,再又是自己靠着理解和记忆硬码了一边,感觉还是很生疏,就只能来写个随笔加深一下印象了. 关于人脸识别,首先需要了解的是级联分类器Casc ...

  6. python中使用OpenCV处理图片

    1.导入OpenCV包 import cv2 2.读取图片 cv2.imread(image_path, mode)        读入函数,包含两个参数,第一个为图片路径及图片名,第二个为读取图片方 ...

  7. Python中cv2库和matplotlib库色彩空间排布不一致

    今天在python中读如图片时发现以下问题: 1.在from matplotlib import pyplot as plt之后,再import cv2 cv2.imshow()不能正常使用,还不知道 ...

  8. Python开发:OpenCV版本差异所引发的cv2.findContours()函数传参问题

    一.问题如下: cv2.findContours()这个方法是用来找出轮廓值的: # cv2.findContours找出轮廓值,cv2.RETR_EXTERNAL表示图像的外轮廓 binary, c ...

  9. 使用python开启你的opencv之旅---图像的读入,存储

    python的便捷是如此的引人着迷,而opencv给python提供的接口使我们能够使用python来快速验证我们的想法,或者与别的模块快速结合,在这个系列文章我会通过jupyter notebook ...

随机推荐

  1. Apache Pulsar 在 BIGO 的性能调优实战(上)

    背景 在人工智能技术的支持下,BIGO 基于视频的产品和服务受到广泛欢迎,在 150 多个国家/地区拥有用户,其中包括 Bigo Live(直播)和 Likee(短视频).Bigo Live 在 15 ...

  2. iOS开发知识梳理博文集

    前言 做iOS开发有3年了,从当初的小白到现在,断断续续看过很多资料,之前也写过一些博文来记录,但是感觉知识点都比较凌乱.所以最近准备抽时间把iOS开发的相关知识进行一个梳理,主要分为OC基础.UI控 ...

  3. oeasy教您玩转linux010109clear清屏

     回忆上次内容 上次讲了灵魂三问 whatis whereis which 通过这三个问题,可以对命令基本了解,我们来试一下!!

  4. ORA-01033错误解决方案-九五小庞

    Microsoft Windows [版本 6.3.9600](c) 2013 Microsoft Corporation.保留所有权利. C:\Users\Administrator>sqlp ...

  5. 阿里面试:dubbo的服务引用过程

    点赞再看,养成习惯,微信搜一搜[三太子敖丙]关注这个喜欢写情怀的程序员. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系 ...

  6. 17_Python的常用模块

    1.随机数模块 random 1.随机小数 import random # (0,1)随机取浮点数 random.random() # 0.17988578778011 # (1, 3)取指定范围的浮 ...

  7. 《闲扯Redis十一》Redis 有序集合对象底层实现

    一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...

  8. 用于测试 JsonAnalyzer2 1.01版的测试用例

    14. 原文={"animal":"ca,t","color":"ora:nge","isMale" ...

  9. 以jar包为容器的java程序访问一同打到jar包里的配置文件的方法

    Java程序有时会被打到jar包里执行,同时src/main/resources里一些配置文件也会被打进去. 比如,src/main/resources下有一个box目录,里面有几个json文件,用m ...

  10. (006)增加Blazor WebAssembly子站,推荐一个可视化linux ssh客户端FinalShell

    增加一个Blazor WebAssembly子站,并添加来回链接. 同时推荐一个好用的ssh客户端:FinalShell,windows用户再也不怕linux黑窗口不会用了:) * 支持直接命令行; ...