Random Color Filter Application

  • Share this:

Code introduction


This function loads an image from a specified path and applies a random color filter to it. The color filter is a 3x3 matrix where each element represents the intensity of red, green, and blue colors.


Technology Stack : OpenCV, NumPy

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
import cv2
import numpy as np

def apply_random_color_filter(image_path):
    # Load the image
    image = cv2.imread(image_path)
    if image is None:
        raise ValueError("Image not found or unable to load")

    # Generate a random color filter
    filter_matrix = np.random.randint(0, 256, (3, 3), dtype=np.uint8)

    # Apply the color filter
    filtered_image = cv2.transform(image, filter_matrix)

    return filtered_image                
              
Tags: