File size: 1,905 Bytes
a341af5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import cv2
import numpy as np
import streamlit as st


# Refer to the application notebook implement the following filters

def bw_filter(img):
    # Write your code here to convert img to a gray image
	img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	return img_gray

def vignette(img, level=2):
	 # Write your code here to create the vignette effect
    height, width = img.shape[:2]  

    # Generate vignette mask using Gaussian kernels.
    X_resultant_kernel = cv2.getGaussianKernel(width, width/level)
    Y_resultant_kernel = cv2.getGaussianKernel(height, height/level)
        
    # Generating resultant_kernel matrix.
    # H x 1 * 1 x W
    kernel = Y_resultant_kernel * X_resultant_kernel.T 
    mask = kernel / kernel.max()
    
    img_vignette = np.copy(img)
        
    # Applying the mask to each channel in the input image.
    for i in range(3):
        img_vignette[:,:,i] = img_vignette[:,:,i] * mask
    

    return img_vignette

def sepia(img):
	# Write your code here to create the sepia effect
	img_sepia = img.copy()
	# Converting to RGB as sepia matrix below is for RGB.
	img_sepia = cv2.cvtColor(img_sepia, cv2.COLOR_BGR2RGB) 
	img_sepia = np.array(img_sepia, dtype = np.float64)
	img_sepia = cv2.transform(img_sepia, np.matrix([[0.393, 0.769, 0.189],
                                                    [0.349, 0.686, 0.168],
                                                    [0.272, 0.534, 0.131]]))
    # Clip values to the range [0, 255].
	img_sepia = np.clip(img_sepia, 0, 255)
	img_sepia = np.array(img_sepia, dtype = np.uint8)
	img_sepia = cv2.cvtColor(img_sepia, cv2.COLOR_RGB2BGR)
	return img_sepia

def pencil_sketch(img, ksize=5):
	 # Write your code here to create the pencil sketch effect
	img_blur = cv2.GaussianBlur(img, (ksize, ksize), 0, 0)
	img_sketch, _ = cv2.pencilSketch(img_blur)
	return img_sketch

# Don't be constrained, add your own filters here