Skip to content

Commit a397aac

Browse files
committed
Initial commit
0 parents  commit a397aac

27 files changed

+4134
-0
lines changed

.idea/code.iml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

preprocessing/gen_sketch/color.py

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
from PIL import Image, ImageEnhance, ImageFilter
2+
from pylab import *
3+
# import numpy as np
4+
from scipy.ndimage import filters
5+
# from skimage import io
6+
import glob, os
7+
8+
in_dir = 'pic_org'
9+
out_dir = 'pic_color'
10+
if not os.path.exists(out_dir): os.mkdir(out_dir)
11+
12+
13+
class MyGaussianBlur(ImageFilter.Filter):
14+
name = "GaussianBlur"
15+
16+
def __init__(self, radius=2, bounds=None):
17+
self.radius = radius
18+
self.bounds = bounds
19+
20+
def filter(self, image):
21+
if self.bounds:
22+
clips = image.crop(self.bounds).gaussian_blur(self.radius)
23+
image.paste(clips, self.bounds)
24+
return image
25+
else:
26+
return image.gaussian_blur(self.radius)
27+
28+
29+
def start(mat):
30+
w, h, c = mat.shape
31+
ws = int(w * random(1))
32+
hs = int(h * random(1))
33+
while (mat[ws, hs, 0] > 254) and (mat[ws, hs, 1] > 254) and (mat[ws, hs, 2] > 254):
34+
ws = int(w * random(1))
35+
hs = int(h * random(1))
36+
return ws, hs
37+
38+
39+
def gen_color_line(mat, dir,max_length,max_dif):
40+
w, h, c = mat.shape
41+
ws = int(w * random(1))
42+
hs = int(h * random(1))
43+
while (mat[ws, hs, 0] > 254) and (mat[ws, hs, 1] > 254) and (mat[ws, hs, 2] > 254):
44+
ws = int(w * random(1))
45+
hs = int(h * random(1))
46+
if dir == 1:
47+
wt = ws
48+
ht = hs
49+
while (wt < w - 1) and (abs(int(mat[wt, ht, 1]) + int(mat[wt, ht, 2]) + int(mat[wt, ht, 0]) - (
50+
int(mat[wt + 1, ht, 1]) + int(mat[wt + 1, ht, 2]) + int(mat[wt + 1, ht, 0]))) < 80):
51+
wt = wt + 1
52+
if dir == 2:
53+
wt = ws
54+
ht = hs
55+
while (ht < h - 1) and (abs(int(mat[wt, ht, 1]) + int(mat[wt, ht, 2]) + int(mat[wt, ht, 0]) - (
56+
int(mat[wt, ht + 1, 1]) + int(mat[wt, ht + 1, 2]) + int(mat[wt, ht + 1, 0]))) < 3):
57+
ht = ht + 1
58+
if dir == 3:
59+
wt = ws
60+
ht = hs
61+
length = 0
62+
while (length < max_length) and (wt < w-1) and (ht < h-1) and (
63+
abs(int(mat[wt, ht, 1]) + int(mat[wt, ht, 2]) + int(mat[wt, ht, 0]) - (
64+
int(mat[wt + 1, ht + 1, 1]) + int(mat[wt + 1, ht + 1, 2]) + int(
65+
mat[wt + 1, ht + 1, 0]))) < max_dif):
66+
ht += 1
67+
wt += 1
68+
length = abs(wt - ws) + abs(ht - hs)
69+
return ws, hs, wt, ht, length
70+
71+
72+
def main():
73+
74+
count = 0
75+
#parameter
76+
wsize = 1024 # double the resolution
77+
Gamma = 0.97
78+
Phi = 200
79+
Epsilon = 0.1
80+
k = 2
81+
Sigma = 1.5
82+
max_length=20
83+
min_length=10
84+
max_dif=30
85+
n_point=50
86+
dir = 3
87+
88+
input_paths = glob.glob(in_dir+ '/*.jpg')
89+
input_paths+=(glob.glob(in_dir+ '/*.jpeg'))
90+
input_paths+=(glob.glob(in_dir+ '/*.png'))
91+
for files1 in input_paths:
92+
filepath, filename = os.path.split(files1)
93+
94+
im = Image.open(files1).convert('L')
95+
im = array(ImageEnhance.Sharpness(im).enhance(3.0))
96+
im2 = filters.gaussian_filter(im, Sigma)
97+
im3 = filters.gaussian_filter(im, Sigma * k)
98+
differencedIm2 = im2 - (Gamma * im3)
99+
(x, y) = shape(im2)
100+
for i in range(x):
101+
for j in range(y):
102+
if differencedIm2[i, j] < Epsilon:
103+
differencedIm2[i, j] = 1
104+
else:
105+
differencedIm2[i, j] = 250 + tanh(Phi * (differencedIm2[i, j]))
106+
107+
108+
gray_pic = differencedIm2.astype(np.uint8)
109+
color_pic = Image.open(files1)
110+
real = np.atleast_2d(color_pic)
111+
112+
113+
if real.ndim == 3:
114+
w, h, c = real.shape
115+
if c==3:
116+
image = color_pic.filter(MyGaussianBlur(radius=5))
117+
mat = np.atleast_2d(image)
118+
119+
if gray_pic.ndim == 2:
120+
gray_pic = np.expand_dims(gray_pic, 2)
121+
gray_pic = np.tile(gray_pic, [1, 1, 3])
122+
123+
124+
125+
for i in range(n_point):
126+
length = 0
127+
while length < min_length:
128+
ws, hs, wt, ht, length = gen_color_line(mat, dir,max_length,max_dif)
129+
gray_pic[ws:wt, hs:ht, :] = mat[ws:wt, hs:ht, :]
130+
131+
gray_pic = np.append(real, gray_pic, axis=1)
132+
final_img = Image.fromarray(gray_pic)
133+
134+
im = final_img
135+
w, h = im.size
136+
hsize = int(h * wsize / float(w))
137+
if hsize * 2 > wsize: # crop to three
138+
im = im.resize((wsize, hsize))
139+
bounds1 = (0, 0, wsize, int(wsize / 2))
140+
cropImg1 = im.crop(bounds1)
141+
# cropImg1.show()
142+
cropImg1.save(os.path.join(out_dir, 'u' + filename))
143+
bounds2 = (0, hsize - int(wsize / 2), wsize, hsize)
144+
cropImg2 = im.crop(bounds2)
145+
# cropImg.show()
146+
cropImg2.save(os.path.join(out_dir, 'd' + filename))
147+
else:
148+
im = im.resize((wsize, (wsize // 2)))
149+
im.save(os.path.join(out_dir, 't' + filename))
150+
count += 1
151+
print('done!' + str(count))
152+
153+
154+
# io.imsave(os.path.join(out_dir, filename), differencedIm2.astype(np.uint8))
155+
156+
157+
if __name__ == '__main__':
158+
main()
86 KB
Loading
Loading
Loading
Loading
121 KB
Loading
10 MB
Loading
326 KB
Loading
326 KB
Loading
326 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Put your orginal picture here!
85.9 KB
Loading
Loading
Loading
Loading
121 KB
Loading

preprocessing/gen_sketch/sketch.py

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
from PIL import Image, ImageEnhance, ImageFilter
2+
from pylab import *
3+
# import numpy as np
4+
from scipy.ndimage import filters
5+
# from skimage import io
6+
import glob, os
7+
8+
in_dir = 'pic_org'
9+
out_dir = 'pic_sketch'
10+
if not os.path.exists(out_dir): os.mkdir(out_dir)
11+
12+
13+
class MyGaussianBlur(ImageFilter.Filter):
14+
name = "GaussianBlur"
15+
16+
def __init__(self, radius=2, bounds=None):
17+
self.radius = radius
18+
self.bounds = bounds
19+
20+
def filter(self, image):
21+
if self.bounds:
22+
clips = image.crop(self.bounds).gaussian_blur(self.radius)
23+
image.paste(clips, self.bounds)
24+
return image
25+
else:
26+
return image.gaussian_blur(self.radius)
27+
28+
29+
def start(mat):
30+
w, h, c = mat.shape
31+
ws = int(w * random(1))
32+
hs = int(h * random(1))
33+
while (mat[ws, hs, 0] > 254) and (mat[ws, hs, 1] > 254) and (mat[ws, hs, 2] > 254):
34+
ws = int(w * random(1))
35+
hs = int(h * random(1))
36+
return ws, hs
37+
38+
39+
def gen_color_line(mat, dir,max_length,max_dif):
40+
w, h, c = mat.shape
41+
ws = int(w * random(1))
42+
hs = int(h * random(1))
43+
while (mat[ws, hs, 0] > 254) and (mat[ws, hs, 1] > 254) and (mat[ws, hs, 2] > 254):
44+
ws = int(w * random(1))
45+
hs = int(h * random(1))
46+
if dir == 1:
47+
wt = ws
48+
ht = hs
49+
while (wt < w - 1) and (abs(int(mat[wt, ht, 1]) + int(mat[wt, ht, 2]) + int(mat[wt, ht, 0]) - (
50+
int(mat[wt + 1, ht, 1]) + int(mat[wt + 1, ht, 2]) + int(mat[wt + 1, ht, 0]))) < 80):
51+
wt = wt + 1
52+
if dir == 2:
53+
wt = ws
54+
ht = hs
55+
while (ht < h - 1) and (abs(int(mat[wt, ht, 1]) + int(mat[wt, ht, 2]) + int(mat[wt, ht, 0]) - (
56+
int(mat[wt, ht + 1, 1]) + int(mat[wt, ht + 1, 2]) + int(mat[wt, ht + 1, 0]))) < 3):
57+
ht = ht + 1
58+
if dir == 3:
59+
wt = ws
60+
ht = hs
61+
length = 0
62+
while (length < max_length) and (wt < w-1) and (ht < h-1) and (
63+
abs(int(mat[wt, ht, 1]) + int(mat[wt, ht, 2]) + int(mat[wt, ht, 0]) - (
64+
int(mat[wt + 1, ht + 1, 1]) + int(mat[wt + 1, ht + 1, 2]) + int(
65+
mat[wt + 1, ht + 1, 0]))) < max_dif):
66+
ht += 1
67+
wt += 1
68+
length = abs(wt - ws) + abs(ht - hs)
69+
return ws, hs, wt, ht, length
70+
71+
72+
def main():
73+
74+
count = 0
75+
#parameter
76+
wsize = 1024 # double the resolution
77+
Gamma = 0.97
78+
Phi = 200
79+
Epsilon = 0.1
80+
k = 2
81+
Sigma = 1.5
82+
max_length=20
83+
min_length=10
84+
max_dif=30
85+
n_point=50
86+
dir = 3
87+
88+
input_paths = glob.glob(in_dir+ '/*.jpg')
89+
input_paths+=(glob.glob(in_dir+ '/*.jpeg'))
90+
input_paths+=(glob.glob(in_dir+ '/*.png'))
91+
for files1 in input_paths:
92+
filepath, filename = os.path.split(files1)
93+
94+
im = Image.open(files1).convert('L')
95+
im = array(ImageEnhance.Sharpness(im).enhance(3.0))
96+
im2 = filters.gaussian_filter(im, Sigma)
97+
im3 = filters.gaussian_filter(im, Sigma * k)
98+
differencedIm2 = im2 - (Gamma * im3)
99+
(x, y) = shape(im2)
100+
for i in range(x):
101+
for j in range(y):
102+
if differencedIm2[i, j] < Epsilon:
103+
differencedIm2[i, j] = 1
104+
else:
105+
differencedIm2[i, j] = 250 + tanh(Phi * (differencedIm2[i, j]))
106+
107+
108+
gray_pic = differencedIm2.astype(np.uint8)
109+
color_pic = Image.open(files1)
110+
real = np.atleast_2d(color_pic)
111+
112+
113+
if real.ndim == 3:
114+
w, h, c = real.shape
115+
if c==3:
116+
image = color_pic.filter(MyGaussianBlur(radius=5))
117+
mat = np.atleast_2d(image)
118+
119+
if gray_pic.ndim == 2:
120+
gray_pic = np.expand_dims(gray_pic, 2)
121+
gray_pic = np.tile(gray_pic, [1, 1, 3])
122+
123+
124+
125+
# for i in range(n_point):
126+
# length = 0
127+
# while length < min_length:
128+
# ws, hs, wt, ht, length = gen_color_line(mat, dir,max_length,max_dif)
129+
# gray_pic[ws:wt, hs:ht, :] = mat[ws:wt, hs:ht, :]
130+
131+
gray_pic = np.append(real, gray_pic, axis=1)
132+
final_img = Image.fromarray(gray_pic)
133+
134+
im = final_img
135+
w, h = im.size
136+
hsize = int(h * wsize / float(w))
137+
if hsize * 2 > wsize: # crop to three
138+
im = im.resize((wsize, hsize))
139+
bounds1 = (0, 0, wsize, int(wsize / 2))
140+
cropImg1 = im.crop(bounds1)
141+
# cropImg1.show()
142+
cropImg1.save(os.path.join(out_dir, 'u' + filename))
143+
bounds2 = (0, hsize - int(wsize / 2), wsize, hsize)
144+
cropImg2 = im.crop(bounds2)
145+
# cropImg.show()
146+
cropImg2.save(os.path.join(out_dir, 'd' + filename))
147+
else:
148+
im = im.resize((wsize, (wsize // 2)))
149+
im.save(os.path.join(out_dir, 't' + filename))
150+
count += 1
151+
print('done!' + str(count))
152+
153+
154+
# io.imsave(os.path.join(out_dir, filename), differencedIm2.astype(np.uint8))
155+
156+
157+
if __name__ == '__main__':
158+
main()

0 commit comments

Comments
 (0)