You can download this code by clicking the button below.
This code is now available for download.
The function randomly selects one of the three possible image filters (Gaussian Blur, Median Blur, Bilateral Filter) and applies it to the input image. Parameters such as Gaussian kernel size, median kernel size, bilateral filter's radial distance, and standard deviation are randomly generated.
Technology Stack : Image processing, OpenCV, NumPy, random selection
Code Type : Image processing
Code Difficulty : Intermediate
def apply_random_filter(image):
import cv2
import numpy as np
import random
# Randomly select a filter from a list of possible filters
filters = [cv2.GaussianBlur, cv2.medianBlur, cv2.bilateralFilter]
filter = random.choice(filters)
# Randomly determine the parameters for the selected filter
if filter == cv2.GaussianBlur:
kernel_size = random.randint(3, 9) | 1 # Must be an odd number
image = filter(image, (kernel_size, kernel_size))
elif filter == cv2.medianBlur:
kernel_size = random.randint(3, 9) | 1 # Must be an odd number
image = filter(image, kernel_size)
elif filter == cv2.bilateralFilter:
d = random.randint(5, 15)
sigma_color = random.uniform(50, 150)
sigma_space = random.uniform(10, 100)
image = filter(image, d, sigma_color, sigma_space)
return image