-
Notifications
You must be signed in to change notification settings - Fork 3
/
color_simplify.py
82 lines (64 loc) · 2.07 KB
/
color_simplify.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
#!/usr/bin/env python3
import numpy as np
import skimage.transform
from scipy.ndimage import uniform_filter
from rtv_smooth import rtv_smooth, tv_smooth
from utils import do_imgs, read_img, write_img
in_filenames = [
"./in.png",
]
out_suffix = "_simplify"
scale = None
# scale = 0.5
# scale = (540, 960)
erase_ratio = 0.5
max_iter = 1
output_8_bit = False
save_mask = False
def box_filter(x, d):
if x.ndim == 2:
return uniform_filter(x, d)
else:
assert x.ndim == 3
return uniform_filter(x, (d, d, 1))
def _write_img(filename, img):
write_img(
filename,
img,
swap_rb=(img.ndim == 3),
signed=False,
output_8_bit=output_8_bit or img.ndim != 3,
)
def convert_img(sess, in_filename, out_filename, eps=1e-15):
img = read_img(in_filename, swap_rb=True, signed=False)
if isinstance(scale, float):
img = skimage.transform.rescale(img, scale, channel_axis=2)
elif isinstance(scale, tuple):
img = skimage.transform.resize(img, scale)
height, width, _ = img.shape
mask = np.random.rand(height, width) > 0.5
for i in range(max_iter):
print("eap", i)
img_new = tv_smooth(img, mask)
img_new = rtv_smooth(img_new)
if save_mask:
filename = out_filename.replace(out_suffix, f"{out_suffix}_mask{i}")
_write_img(filename, mask.astype(np.float64))
filename = out_filename.replace(out_suffix, f"{out_suffix}_out{i}")
_write_img(filename, img_new)
if i == max_iter - 1:
break
value = ((img_new - img) ** 2).sum(axis=2)
weight = ((img_new - box_filter(img_new, 3)) ** 2).sum(axis=2)
knapsack = value / (weight + eps)
threshold = np.quantile(knapsack, 1 - (i + 1) / (max_iter - 1) * erase_ratio)
knapsack /= threshold + eps
mask = np.random.rand(height, width) > knapsack
if __name__ == "__main__":
do_imgs(
convert_img,
None,
in_filenames,
out_suffix=out_suffix,
out_extname=None if output_8_bit else ".png",
)