-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_tooner.py
More file actions
73 lines (57 loc) · 1.9 KB
/
image_tooner.py
File metadata and controls
73 lines (57 loc) · 1.9 KB
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
63
64
65
66
67
68
69
70
71
72
73
#step 1
#Use bilateral filter for edge-aware smoothing.
import cv2
num_down = 2 # number of downsampling steps
num_bilateral = 7 # number of bilateral filtering steps
img_rgb = cv2.imread("girl.jpg")
#convert the image into a square image of 800x800
width1 = 720
height1 = 720
dim1 = (width1,height1)
img_rgb = cv2.resize(img_rgb,dim1,interpolation = cv2.INTER_AREA)
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in range(num_down):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of
# applying one large filter
for _ in range(num_bilateral):
img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)
# upsample image to original size
for _ in range(num_down):
img_color = cv2.pyrUp(img_color)
#STEP 2 & 3
#Use median filter to reduce noise
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
#STEP 4
#Use adaptive thresholding to create an edge mask
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=9,
C=2)
# Step 5
# Combine color image with edge mask & display picture
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
#Step 6
#determine size of both images
#resize edge image to the size of color image
col = img_color.shape
print('size of color', col)
edge = img_edge.shape
print('size of edge',edge)
if img_color.shape[0] == img_edge.shape[0] and img_color.shape[1] == img_edge.shape[1]:
img_edge_resized = img_edge
else:
width = img_color.shape[1]
height = img_color.shape[0]
dim =(width,height)
img_edge_resized = cv2.resize(img_edge,dim,interpolation = cv2.INTER_AREA)
#Step 7
# convert back to color, bit-AND with color image
img_cartoon = cv2.bitwise_and(img_color,img_edge_resized)
#display
cv2.imshow("ThE_cartoon", img_cartoon)