forked from Data-Science-Community-SRM/Cartoonify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2 - Pencil Sktech and Pencil Edge.py
175 lines (73 loc) · 2.09 KB
/
2 - Pencil Sktech and Pencil Edge.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
# In[26]:
image_path = r"C:\Users\rusal\Cartoonify\Project-5\scenery.jpg"
# In[27]:
image = cv2.imread(image_path)
# In[28]:
plt.imshow(image, cmap = "gray")
# ## Applying Pencil Sketching
# ### Defining the functions
# In[29]:
def gray(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# In[30]:
def blur(image):
return cv2.GaussianBlur(image,(25,25),0)
# In[31]:
def pen_sketch(image1,image2):
return cv2.divide(image1,image2,scale = 250.0)
# ### We create a copy of the image and call the functions on the image
# In[32]:
img_copy = np.copy(image)
# In[33]:
gray_img = gray(img_copy)
plt.imshow(gray_img, cmap = "gray")
# In[34]:
blur_img = blur(gray_img)
plt.imshow(blur_img, cmap = "gray")
# #### This is how the final pencil sketch looks when applied on the image
# In[35]:
sketch_img = pen_sketch(gray_img,blur_img)
plt.imshow(sketch_img, cmap = "gray")
# #### Here is the original picture for comparison
# In[36]:
plt.imshow(image)
# ## Applying Pencil Edge
# ### Defining the functions (We shall use the same GrayScale function as above)
# In[37]:
def med_blur(image):
return cv2.medianBlur(image, 25)
# In[38]:
def laplacian(image):
return cv2.Laplacian(image, -1, ksize=3)
# In[39]:
def invert(image):
return 255-image
# In[40]:
def pencil_edge(image):
return cv2.threshold(image,150,255, cv2.THRESH_BINARY)
# #### Creating a copy of original image and calling the above functions
# In[41]:
img_copy = np.copy(image)
# In[42]:
gray_img = gray(img_copy)
plt.imshow(gray_img, cmap = "gray")
# In[43]:
med_img = med_blur(gray_img)
plt.imshow(med_img, cmap = "gray")
# In[44]:
laplace_img = laplacian(med_img)
plt.imshow(laplace_img, cmap = "gray")
# In[45]:
invert_img = invert(laplace_img)
plt.imshow(invert_img, cmap = "gray")
# #### This is the final Pencil Edge on the image
# In[46]:
pencil_edge_img = pencil_edge(invert_img)
plt.imshow(pencil_edge_img[1], cmap = "gray")
# In[ ]: