| |
| import cv2 |
| import numpy as np |
| from matplotlib import pyplot as plt |
| import os |
|
|
| def cartoonize(img_path): |
| |
| img = cv2.imread(img_path) |
|
|
| |
| img_gb = cv2.GaussianBlur(img, (7, 7) ,0) |
| |
| img_mb = cv2.medianBlur(img_gb, 5) |
| |
| img_bf = cv2.bilateralFilter(img_mb, 5, 80, 80) |
|
|
|
|
| |
| img_lp_al = cv2.Laplacian(img_bf, cv2.CV_8U, ksize=5) |
|
|
|
|
| |
| img_lp_al_grey = cv2.cvtColor(img_lp_al, cv2.COLOR_BGR2GRAY) |
|
|
| |
| blur_al = cv2.GaussianBlur(img_lp_al_grey, (5, 5), 0) |
|
|
| |
| _, tresh_al = cv2.threshold(blur_al, 245, 255,cv2.THRESH_BINARY + cv2.THRESH_OTSU) |
|
|
| |
| inverted_Bilateral = cv2.subtract(255, tresh_al) |
|
|
| |
| |
| |
|
|
| |
| img_reshaped = img.reshape((-1,3)) |
| |
| img_reshaped = np.float32(img_reshaped) |
| |
| criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) |
| |
| K = 16 |
| |
| _, label, center = cv2.kmeans(img_reshaped, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) |
|
|
| |
| center = np.uint8(center) |
| res = center[label.flatten()] |
| |
| img_Kmeans = res.reshape((img.shape)) |
|
|
| |
| inverted_Bilateral = cv2.cvtColor(inverted_Bilateral, cv2.COLOR_GRAY2RGB) |
| |
| cartoon_Bilateral = cv2.bitwise_and(inverted_Bilateral, img_Kmeans) |
|
|
| |
| cv2.imwrite(img_path, cartoon_Bilateral) |
|
|
| |
| |
| def style_frames(): |
| for image in os.listdir("frames/final"): |
| frame_path = os.path.join("frames",'final',image) |
| cartoonize(frame_path) |