Python edge detection and curvature calculation

前端 未结 4 638
眼角桃花
眼角桃花 2021-01-31 22:56

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 t

4条回答
  •  名媛妹妹
    2021-01-31 23:27

    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.

提交回复
热议问题