• 在Python中使用OpenCV(CV2)对图像进行边缘检测


    转载: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:

    输出:

    Python | Edge Detection of Image using OpenCV (CV2) (1)

    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:

    输出:

    Python | Edge Detection of Image using OpenCV (CV2) (2)

    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:

    输出:

    Python | Edge Detection of Image using OpenCV (CV2) (3)

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

  • 相关阅读:
    web service 项目 和 普通 web项目 的 区别
    1) Apache Maven 's README.txt
    spring + rs + RocketMQ 【精】
    wadl 的自动生成(cxf版本3.1.1)
    新建maven web 项目后,出现的小问题
    cxf-rs client 调用
    wadl 的自动生成(cxf版本2.7.6)
    Java学习路线
    Tomcat 学习记录
    Spring Boot 学习记录
  • 原文地址:https://www.cnblogs.com/MCSFX/p/13652922.html
Copyright © 2020-2023  润新知