Photo-Filter-2.0 / filters.py
eienmojiki's picture
Update filters.py
1eba945 verified
raw
history blame
3.21 kB
import cv2
import numpy as np
from registry import registry
@registry.register("Original")
def original(image):
return image
@registry.register("Grayscale")
def grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@registry.register("Gaussian Blur", defaults={'kernel_size': 15}, min_vals={'kernel_size': 3}, max_vals={'kernel_size': 31}, step_vals={'kernel_size': 2})
def gaussian_blur(image, kernel_size: int = 15):
return cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
@registry.register("Pencil Sketch")
def pencil_sketch(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted = cv2.bitwise_not(gray)
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
inverted_blurred = cv2.bitwise_not(blurred)
return cv2.divide(gray, inverted_blurred, scale=256.0)
@registry.register("Sepia")
def sepia(image):
kernel = np.array([
[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]
])
return cv2.transform(image, kernel)
@registry.register("Edge Enhance", defaults={'intensity': 1.5}, min_vals={'intensity': 0.5}, max_vals={'intensity': 5.0}, step_vals={'intensity': 0.1})
def edge_enhance(image, intensity: float = 1.5):
kernel = np.array([
[-1 * intensity, -1 * intensity, -1 * intensity],
[-1 * intensity, 9 * intensity, -1 * intensity],
[-1 * intensity, -1 * intensity, -1 * intensity]
])
return cv2.filter2D(image, -1, kernel)
@registry.register("Canny Edge", defaults={'lower_threshold': 100, 'upper_threshold': 200, 'convert_to_gray': True}, min_vals={'lower_threshold': 0, 'upper_threshold': 255}, max_vals={'lower_threshold': 255, 'upper_threshold': 255}, step_vals={'lower_threshold': 1, 'upper_threshold': 1})
def canny_edge(image, lower_threshold: int=100, upper_threshold: int=200, convert_to_gray: bool=True):
if convert_to_gray and len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image
edges = cv2.Canny(gray, lower_threshold, upper_threshold)
return edges
@registry.register("Sobel Edge", defaults={'dx': 1, 'dy': 0, 'kernel_size': 3, 'convert_to_gray': True}, min_vals={'dx': 1, 'dy': 1, 'kernel_size': 3}, max_vals={'dx': 2, 'dy': 2, 'kernel_size': 7}, step_vals={'dx': 1, 'dy': 1, 'kernel_size': 2})
def sobel_edge(image, dx: int=1, dy: int=0, kernel_size: int=3, convert_to_gray: bool=True):
"""
Applies the Sobel edge detector to detect horizontal or vertical edges.
Args:
img (numpy.ndarray): Input image (BGR or grayscale)
dx (int): Order of derivative in x-direction (0 = no x-edge detection)
dy (int): Order of derivative in y-direction (0 = no y-edge detection)
kernel_size (int): Size of Sobel kernel (1, 3, 5, or 7)
convert_to_gray (bool): Convert to grayscale first
Returns:
numpy.ndarray: Edge magnitude image
"""
if convert_to_gray and len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image
sobel = cv2.Sobel(gray, cv2.CV_64F, dx, dy, ksize=kernel_size)
abs_sobel = cv2.convertScaleAbs(sobel)
return abs_sobel