Scikit Image − Farid Filter



The Farid filter is a widely used and fundamental technique for edge detection in image processing. It aims to compute the gradient of an image to identify areas with rapid intensity changes, typically corresponding to edges and object boundaries within the image. Similar to the Scharr filter, this operator is designed with a rotation invariance constraint.

The scikit-image (skimage) library provides three functions within its filter module to detect the edge magnitudes of an image using the Farid transform. The functions are farid(), farid_h() and farid_v().

Using the skimage.filters.farid() function

The filters.farid() function is used to compute the edge magnitude of an input image using the Farid transform.

Syntax

Following is the syntax of this function −

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

Parameters

Here are the details of the parameters −

  • image: This parameter represents the input image on edge magnitudes are calculated. It should be a NumPy array.
  • 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 a sequence of integers that specify along which axis or axes the edge filter should be computed. If not provided, the edge magnitude is computed by default. This is defined as:
farid_mag = np.sqrt(sum([farid(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 Farid edge map.

    Note: To obtain a magnitude that's not very sensitive to direction, you can compute it by taking the square root of the sum of the squares of both the horizontal and vertical derivatives. This approach is designed to be rotation-invariant, similar to the Scharr operator.

    Example

    Here is an example of applying the Farid transform to an image using the filters.farid() function −

    import matplotlib.pyplot as plt
    from skimage.filters import farid
    from skimage import io
    
    # Load the input image 
    image = io.imread('Images/lines.jpg')
    
    # Apply the Farid filter 
    filtered_image = farid(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 Farid Filter Result
    ax[1].imshow(filtered_image, cmap='gray')
    ax[1].axis('off')
    ax[1].set_title('Farid Filter Result') 
    
    plt.tight_layout()
    plt.show()
    

    Output

    Farid Filter

    Horizontal and Vertical edge detection using the farid_h() and farid_v() functions

    The skimage.filters.farid_h() and skimage.filters.farid_v() functions are used to find horizontal and vertical edges in a 2-D image, respectively, by applying the Farid transform. These two functions return a 2-D array representing the Farid edge map.

    Syntax

    Syntax for farid_h() function −

    skimage.filters.farid_h(image, *, mask=None)
    

    Syntax

    Syntax for farid_v() function −

    skimage.filters.farid_v(image, *, mask=None)
    

    Parameters

    Here's an explanation of the parameters for both 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 Farid transform to a certain area. Surrounding pixels are also masked to prevent them from affecting the result.

    Example

    This example applies the Farid horizontal and vertical edge detection filters (farid_h() and farid_v()) on an input image −

    import matplotlib.pyplot as plt
    from skimage.filters import farid_h, farid_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 Farid horizontal filter
    filtered_image_h = farid_h(gray_image)
    
    # Apply the Farid vertical filter
    filtered_image_v = farid_v(gray_image)
    
    # Plot the original, Farid horizontal, and Farid 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 Farid Horizontal Filter Result
    ax[1].imshow(filtered_image_h, cmap='gray')
    ax[1].set_title('Farid Horizontal Filter Result')
    ax[1].axis('off')
    
    # Display the Farid Vertical Filter Result
    ax[2].imshow(filtered_image_v, cmap='gray')
    ax[2].set_title('Farid Vertical Filter Result')
    ax[2].axis('off')
    
    plt.tight_layout()
    plt.show()
    

    Output

    Farid Filter
    Advertisements