-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoint_synthesis.py
More file actions
130 lines (99 loc) · 4.58 KB
/
joint_synthesis.py
File metadata and controls
130 lines (99 loc) · 4.58 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
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
import logging
import os
import typing
import PIL.Image
import numpy as np
from matplotlib import pyplot as plt
import analysis
import common
import synthesis
import hierarchy_node
def synthesize_primary_textons(
exemplars: typing.List[str], source_map: synthesis.source_map.SourceMap, size: typing.Tuple[int, int]
) -> hierarchy_node.VectorNode:
source_primaries = [
analysis.result_objects.PrimaryTextonResult.load("intermediate/{}/primary_textons.dat".format(source)).primary_textons
for source in exemplars
]
new_parent = hierarchy_node.VectorNode.from_rectangle(
(
max([parent.get_bounding_height() for parent in source_primaries]),
max([parent.get_bounding_width() for parent in source_primaries])
)
)
for source_id, primaries in enumerate(source_primaries):
for texton in primaries.level_order_traversal():
texton.source_id = source_id
new_parent.children.extend(primaries.children)
return synthesis.primary_texton_distro(new_parent, size, source_map=source_map)
def synthesize_secondary_textons(
exemplars: typing.List[str], source_map: synthesis.source_map.SourceMap, size: typing.Tuple[int, int]
) -> hierarchy_node.VectorNode:
source_secondaries = [
analysis.result_objects.SecondaryTextonResult.load("intermediate/{}/secondary_textons.dat".format(source))
for source in exemplars
]
distances = []
new_parent = hierarchy_node.VectorNode.from_rectangle(
(
max([parent.secondary_textons.get_bounding_height() for parent in source_secondaries]),
max([parent.secondary_textons.get_bounding_width() for parent in source_secondaries])
)
)
for source_id, primaries in enumerate(source_secondaries):
for texton in primaries.secondary_textons.level_order_traversal():
texton.source_id = source_id
new_parent.children.extend(primaries.secondary_textons.children)
distances.extend(primaries.element_spacing)
distances = np.array(distances)
return synthesis.secondary_texton_distro(new_parent, size, distances, source_map=source_map)
def synthesize_gradient_field(
exemplars: typing.List[str], source_map: synthesis.source_map.SourceMap, size: typing.Tuple[int, int]
) -> typing.Tuple[analysis.result_objects.GradientFieldResult, np.ndarray]:
gradient_data = [
analysis.result_objects.GradientFieldResult.load("intermediate/{}/gradient_field.dat".format(source))
for source in exemplars
]
densities = []
ids = []
colors = []
for source_id, primaries in enumerate(gradient_data):
densities.append(primaries.query_point_spacing)
colors.extend(primaries.colors)
ids.extend([source_id] * len(primaries.colors))
density = np.mean(densities)
gradient_field_points, gradient_field_colors = synthesis.generate_gradient_field(
np.array(colors), density, size, source_map, np.array(ids)
)
gradient_field_raster = common.gradient_field.rasterize_rbf(
gradient_field_points, gradient_field_colors, size
)
gradient_field = analysis.result_objects.GradientFieldResult(
gradient_field_points, gradient_field_colors, density, gradient_data[0].solid_color
)
return gradient_field, gradient_field_raster
def joint_synthesis(
config: synthesis.SynthesisConfig,
exemplars: typing.List[str], source_map: synthesis.source_map.SourceMap, size: typing.Tuple[int, int],
):
primary_textons = synthesize_primary_textons(exemplars, source_map, size)
secondary_textons = synthesize_secondary_textons(exemplars, source_map, size)
gradient_field, gradient_field_raster = synthesize_gradient_field(exemplars, source_map, size)
synthesis.secondary_color_adjustment(secondary_textons, gradient_field_raster, size)
logging.info("Saving synthetic files to {}...".format(os.path.abspath(config.output_directory)))
synthesis.export_result(
primary_textons, secondary_textons, gradient_field, gradient_field_raster,
config.output_directory, config.no_binary, config.no_layers
)
if __name__ == '__main__':
common.logger.configure_logger()
cfg = synthesis.SynthesisConfig()
cfg.output_directory = os.path.join("output", "joint_synthesis")
exemplars = ["concrete_4", "flowers_1", "tile_1"]
source_map = synthesis.source_map.MultiSourceMap([
"source_maps/pg_background.png",
"source_maps/pg_p.png",
"source_maps/pg_g.png",
])
result_size = (1000, 515)
joint_synthesis(cfg, exemplars, source_map, result_size)