【问题标题】:Python edge detection and curvature calculationPython边缘检测和曲率计算
【发布时间】:2012-02-04 00:14:39
【问题描述】:

我知道边缘检测问题之前已经发布过(Java 中:Count the number of objects in an Image,与语言无关:Image edge detection),但我想知道如何在 python 中实现。

我正在对一些简单形状(带有一些噪声的二进制形状)的边缘进行边缘检测和曲率计算。我知道 OpenCV 有一些包装器,但不确定哪个更好:pyopencv、pycv、pycvf?

由于我基本上只做这两个任务,我也不确定自己实现它是否比使用库更快。

【问题讨论】:

标签: python image-processing computer-vision


【解决方案1】:

我们在积极开发的scikit-image 中有分割和边缘检测算法,您可能会发现它们很有用:

Scikit Images Examples

【讨论】:

  • 边缘检测很酷。你会实现曲率计算吗?
  • @clwen 你如何定义曲率?如果你给我一篇论文或一个例子,我们可以试一试。
【解决方案2】:

你可以在 python 中使用 scipy 轻松实现边缘检测。

from scipy import ndimage
edge_horizont = ndimage.sobel(greyscale, 0)
edge_vertical = ndimage.sobel(greyscale, 1)
magnitude = np.hypot(edge_horizont, edge_vertical)

这是原始图像和边缘检测后图像的示例。

在scikit-image中,有一个special page with explanations是如何做边缘检测的。

【讨论】:

    【解决方案3】:

    有一种非常简单的方法可以使用 scikit 图像在 python 中查找轮廓。它实际上只是几行代码,如下所示:

        from skimage import measure
        contours = measure.find_contours(gimg, 0.8)
    

    这将返回轮廓线的矢量表示。在每行的单独数组中。通过计算近似值也很容易减少一条线上的点数。这里有一段较长的源代码描述:image vectorization with python

    【讨论】:

      【解决方案4】:

      您可以使用不同的边缘检测器:Canny、Sobel、Laplacian、Scharr、Prewitt、Roberts。你可以用OpenCV

      import cv2
      import numpy as np
      
      img = cv2.imread('your_image.jpg', 0)
      
      # Canny
      edges_canny = cv2.Canny(img, 100, 100)
      
      # Sobel
      sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
      sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
      edges_sobel = np.hypot(sobel_x, sobel_y)
      edges_sobel *= 255.0 / np.max(edges_sobel)
      
      # Laplacian
      edges_laplacian = cv2.Laplacian(img, cv2.CV_64F)
      
      # Scharr
      schar_x = cv2.Scharr(img, cv2.CV_64F, 1, 0)
      schar_y = cv2.Scharr(img, cv2.CV_64F, 0, 1)
      edges_scharr = np.hypot(schar_x, schar_y)
      edges_scharr *= 255.0 / np.max(edges_scharr)
      

      scikit-image:

      import cv2
      from skimage import feature, filters
      
      img = cv2.imread('your_image.jpg', 0)
      
      edges_canny = feature.canny(img) # Canny
      edges_sobel = filters.sobel(img) # Sobel
      edges_laplace = filters.laplace(img) # Laplacian
      edges_scharr = filters.scharr(img) # Scharr
      edges_prewitt = filters.prewitt(img) # Prewitt
      edges_roberts = filters.roberts(img) # Roberts
      

      Canny 边缘检测器可能是最常用和最有效的方法,但也是最复杂的方法。有关上述方法之间有什么区别的更多详细信息,请查看this blog post

      【讨论】:

      • 如何将创建的边缘叠加回原始图像
      猜你喜欢
      • 2012-08-09
      • 1970-01-01
      • 2021-03-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多