Apply Gaussian Blur to Image using OpenCV

  • Share this:

Code introduction


This function uses OpenCV's GaussianBlur function to apply Gaussian blur to an image.


Technology Stack : OpenCV, NumPy

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
def blur_image(image_path, kernel_size=(5, 5)):
    """
    Blurs an image using OpenCV's GaussianBlur function.
    
    Args:
    image_path (str): The path to the image file.
    kernel_size (tuple): The size of the Gaussian kernel. It must be an odd number.
    
    Returns:
    numpy.ndarray: The blurred image.
    """
    import cv2
    import numpy as np
    
    # Read the image
    image = cv2.imread(image_path)
    
    # Check if the image was loaded
    if image is None:
        raise ValueError("Image not found or unable to load.")
    
    # Apply Gaussian blur
    blurred_image = cv2.GaussianBlur(image, kernel_size, 0)
    
    return blurred_image                
              
Tags: