简体   繁体   中英

Python edge detection and curvature calculation

I know the edge detection problem has been posted before (in Java: Count the number of objects in an Image , language independent: Image edge detection ), but I want to know how to implement it in python.

I'm doing edge detection and curvature calculation around the edge on some simple shapes (binary shape with some noise). I know there are some wrapper for OpenCV, but not sure which one is better: pyopencv, pycv, pycvf?

Since I'm basically only doing this two tasks, I'm also not sure whether it would be faster to implement it by myself rather than using the library.

We have segmentation and edge detection algorithms in the actively developed scikit-image that you may find useful:

Scikit Images Examples

You can easily achieve edge detection with scipy in python.

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

And here is an example of original image and the image after edge detection. 在此输入图像描述

In scikit-image, there is a special page with explanations of how to do edge detection.

There is a very simple way to find contours in python with scikit image. It's really just a couple line of code, like this:

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

This returns the vector representation of the contour lines. In a separate array for each line. And it's also easy to decrease the number of points in a line by calculating an approximation. Here is a bit longer description with source code: image vectorization with python

There are different edge detectors you can use: Canny, Sobel, Laplacian, Scharr, Prewitt, Roberts . You can do it with 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)

or with 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 edge detector is probably the most commonly used and most effective method, but also the most complex. For more details on what is the difference between the mentioned methods check this blog post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM