forked from Ankuraxz/Hacktoberfest-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartoonizer.py
62 lines (48 loc) · 1.79 KB
/
Cartoonizer.py
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
57
58
59
60
61
62
'''
Cartoonizer is an application of Digital Image processing that converts Image to Cartoon-like Image.
The code uses Down sampling and Up sampling using Laplacian Pyramid, Bilateral Filtering, Median Blurring, Adaptive Threshold and Bitwise And.
Checkout details and .exe application here - https://github.com/Ankuraxz/Cartoonizer
'''
from cv2 import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
import sys
def resize(frame):
img = cv2.resize(frame,(800,800))
return img
def cartoonizer(img,num_down = 2, num_bi = 5):
#Params
#num_down = 2 #DOWNSAMPLE STEPS
#num_bi = 5 # BILATERAL FILTERING STEPS
img_c = img
for ix in range(num_down):
img_c = cv2.pyrDown(img)# Pyramid Down : Downsampling
# print(img_c.shape)
for iy in range(num_bi):
img_c = cv2.bilateralFilter(img_c,d=9,sigmaColor=9,sigmaSpace=7) #Filtering
# print(img_c.shape)
#UPSAMPLING
for ix in range(num_down):
img_c = cv2.pyrUp(img_c)# Pyramid Down : Downsampling
# print(img_c.shape)
#BLUR and Threshold
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) # GRAY SCALE
img_blur = cv2.medianBlur(img_gray,7) #MEDIAN BLUR
img_edge = cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,blockSize=9,C=2)
img_c = cv2.resize(img_c,(800,800))
#RGB CONVERSION + BITWISE &
img_edge = cv2.cvtColor(img_edge,cv2.COLOR_GRAY2RGB)
# print(img_c.shape)
# print(img_edge.shape)
img_cartoon = cv2.bitwise_and(img_c,img_edge)
stack = np.hstack([img,img_cartoon])
return stack
if __name__ == "__main__":
# read original image in bgr mode
img = cv2.imread("<img.jpg>")
# Resizing
img = resize(img)
cartoon = cartoonizer(img)
cv2.imshow("Cartoon", cartoon)
cv2.waitKey(0)