-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cartoonify.py
30 lines (26 loc) · 967 Bytes
/
Cartoonify.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
# Import libraries
import cv2
import numpy as np
# Read image from webcam
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
if not ret:
break
cv2.imshow("Input", frame)
k = cv2.waitKey(1)
if k%256==32: # Press space to capture image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Grayscaling
smooth = cv2.medianBlur(gray, 5) # Smoothening
edges = cv2.adaptiveThreshold(smooth, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) # Obtaining edges
# Cartoonization
color = cv2.bilateralFilter(frame, 9, 250, 250) #Smoothening
cartoon = cv2.bitwise_and(color, color, mask=edges) #Cartoonifying
# Displaying processed images
cv2.imshow("Edges", edges)
cv2.imshow("Cartoon", cartoon)
k = cv2.waitKey(0)
break
# Termination
cam.release()
cv2.destroyAllWindows()