You can download this code by clicking the button below.
This code is now available for download.
This code defines a function that loads an image from the provided image path and applies a random color filter to it. It first defines a random color range in the HSV color space, then creates a mask to filter pixels within that color range, and finally performs a bitwise operation between the original image and the mask to apply the filter.
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)
# Define a random color range from the HSV color space
lower_color = np.random.randint(0, 180, size=(2,))
upper_color = np.random.randint(180, 255, size=(2,))
# Convert the image from BGR to HSV
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Create a mask for the color range
mask = cv2.inRange(hsv_image, lower_color, upper_color)
# Bitwise the original image and the mask
result = cv2.bitwise_and(image, image, mask=mask)
# Return the result
return result