Python边缘检测和曲率计算

时间:2012-02-04 00:14:39

标签: python image-processing computer-vision

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

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

由于我基本上只执行这两项任务,所以我也不确定自己实现它而不是使用库是否会更快。

4 个答案:

答案 0 :(得分:12)

我们在积极开发的scikit-image中提供了您可能会觉得有用的分段和边缘检测算法:

Scikit Images Examples

答案 1 :(得分:5)

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

以下是原始图像和边缘检测后的图像的示例。 enter image description here

在scikit-image中,有special page with explanations如何进行边缘检测。

答案 2 :(得分:3)

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

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

返回轮廓线的矢量表示。在每行的单独数组中。并且通过计算近似值也可以很容易地减少线中的点数。以下是源代码的详细说明:image vectorization with python

答案 3 :(得分:0)

您可以使用不同的边缘检测器: 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