Scikit Image − Sobel Filter



The Sobel filter is a popular and fundamental tool for edge detection in image processing. It calculates the gradient of an image to find regions of rapid intensity change, which are often indicative of edges and boundaries in the image.

The "scikit-image" (skimage) library offers three key functions within its filter module for applying the Sobel filter to images, which includes sobel(), sobel_h(), and sobel_v(). These functions are valuable tools for various image analysis tasks where the detection and enhancement of edges and boundaries are crucial.

Using the skimage.filters.sobel() function

The sobel() function is used to find edges in an image using the Sobel filter.

Following is the syntax of this function −

skimage.filters.sobel(image, mask=None, *, axis=None, mode='reflect', cval=0.0)

Parameters

    Here's an explanation of the parameters −

  • image: The input image on which sobel edge detection will be applied. It should be a NumPy ndarray representing the image.
  • mask (optional): An array of boolean values that can be used to clip the output image. Wherever mask is set to 0, the corresponding values in the output image will be set to 0 as well.
  • axis (optional): An integer or sequence of integers specifying the axis or axes along which to compute the edge filter. If not provided, the edge magnitude is computed. Which is defined as:
sobel_mag = np.sqrt(sum([sobel(image, axis=i)**2 for i in range(image.ndim)]) / image.ndim)
  • mode (optional): A string or sequence of strings specifying the boundary mode for the convolution. It determines how the filter behaves near the edges of the image. It can be a single boundary mode or one boundary mode per axis. Possible modes include 'reflect' (default), 'constant', 'nearest', etc. Refer to the documentation of scipy.ndimage.convolve for a description of these modes.
  • cval (optional): This parameter is only relevant when the mode is set to 'constant'. It defines the constant value used for values outside the boundary of the image data.
  • Return value

    The function returns an array of float values, which represents the Sobel edge map.

    Example

    The following example applies the Sobel edge detection filter to an input image using the skimage.filters.sobel() function −

    import matplotlib.pyplot as plt
    from skimage.filters import sobel
    from skimage import io
    
    # Load the input image 
    image = io.imread('Images/tree.jpg', )
    
    # Apply the Sobel filter
    filtered_image = sobel(image)
    
    # Plot the original and filtered images
    fig, axes = plt.subplots(1, 2, figsize=(10, 5))
    ax = axes.ravel()
    
    # Display the Original Image
    ax[0].imshow(image, cmap='gray')
    ax[0].set_title('Original Image')
    ax[0].axis('off')
    
    # Display the Sobel Filter Result
    ax[1].imshow(filtered_image, cmap='gray')
    ax[1].axis('off')
    ax[1].set_title('Sobel Filter Result')
    
    plt.tight_layout()
    plt.show()
    

    Output

    Sobel Filter

    Horizontal and Vertical edge detection using the sobel_h() and sobel_v() functions

    The skimage.filters.sobel_h() and skimage.filters.sobel_v() functions are used to find horizontal and vertical edges in a 2-D image, respectively, by applying the Sobel transform. These functions calculate the gradients of the image in the horizontal and vertical directions. And these two functions returns a 2-D array representing the Sobel edge map, which highlights regions of rapid intensity change along the horizontal and vertical axis, typically corresponding to horizontal and vertical edges.

    Syntax

    Syntax for sobel_h() function −

    skimage.filters.sobel_h(image, mask=None)
    

    Syntax

    Syntax for sobel_v() function −

    skimage.filters.sobel_v(image, mask=None)
    

    Here is the parameters explanation of both the functions −

    • image (2-D array): The input image to process for horizontal and vertical edge detection.
    • mask (optional, 2-D array): An optional mask to limit the application of the Sobel transform to a certain area. Surrounding pixels are also masked to prevent them from affecting the result.

    Example

    This example applies the Sobel horizontal and vertical edge detection filters (sobel_h() and sobel_v()) on an input image −

     
    import matplotlib.pyplot as plt
    from skimage.filters import sobel_h, sobel_v
    from skimage import io, color
    
    # Load the input image 
    image = io.imread('Images/Lines.jpg')
    
    # Convert the image to grayscale
    gray_image = color.rgb2gray(image)
    
    # Apply the Sobel horizontal filter
    filtered_image_h = sobel_h(gray_image)
    
    # Apply the Sobel vertical filter
    filtered_image_v = sobel_v(gray_image)
    
    # Plot the original, Sobel horizontal, and Sobel vertical filtered images
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))
    ax = axes.ravel()
    
    # Display the Original Image
    ax[0].imshow(gray_image, cmap='gray')
    ax[0].set_title('Original Image')
    ax[0].axis('off')
    
    # Display the Sobel Horizontal Filter Result
    ax[1].imshow(filtered_image_h, cmap='gray')
    ax[1].set_title('Sobel Horizontal Filter Result')
    ax[1].axis('off')
    
    # Display the Sobel Vertical Filter Result
    ax[2].imshow(filtered_image_v, cmap='gray')
    ax[2].set_title('Sobel Vertical Filter Result')
    ax[2].axis('off')
    
    plt.tight_layout()
    plt.show()
    

    Output

    Sobel Filter
    Advertisements