Python edge detection and curvature calculation

I know that an edge detection problem was previously published (in Java: Count the number of objects in an image , regardless of language: Edge detection of an image ), but I want to know how to implement it in python.

I am doing edge detection and calculating edge curvature on some simple shapes (binary form with some noise). I know there is a shell for OpenCV, but I'm not sure which one is better: pyopencv, pycv, pycvf?

Since I mainly perform only two tasks, I am also not sure whether it will be faster to implement it independently, and not use the library.

+9
python image-processing computer-vision
source share
4 answers

In actively developed scikit-image segmentation and detection scikit-image , which may be useful:

Scikit Image Examples

+12
source share

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 the original image and the image after edge detection. enter image description here

There is a special page in scikit-image with explanations on how to make edge detection.

+5
source share

There is a very simple way to find outlines in python with scikit image. These are really a couple lines of code, for example:

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

This returns a vector representation of the contour lines. In a separate array for each row. It is also easy to reduce the number of points in the line by calculating the approximation. Below is a more detailed description with source code: vectorizing images with python

+3
source share

You can use various edge detectors: Canny, Sobel, Laplacian, Scharr, Prewitt, Roberts. You can do this 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 

The Canny Edge detector is probably the most commonly used and most effective method, but also the most complex. For more information on the difference between the mentioned methods, check out this blog post .

0
source share

All Articles