-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_dataset.jl
138 lines (123 loc) · 5.09 KB
/
generate_dataset.jl
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
using Printf: @sprintf
using Random: Xoshiro
using TOML
using ColorTypes: RGB, Gray
using FixedPointNumbers: N0f8
using FileIO: save
using ImageDraw: Point, LineSegment
using ImageDraw: draw!
using ProgressMeter: Progress, next!, finish!
using ToStruct: ToStruct
using SegRCDB
using SegRCDB: uniform, pnoise1
function main()
runtime_config = SegRCDBRuntimeConfig()
(; save_root, numof_images, numof_classes) = runtime_config
dataset_config = SegRCDBDatasetConfig()
(; seed, image_size, image_size, start_pos, instance_num, mode) =
dataset_config
dir4image = joinpath(save_root, "image")
dir4mask = joinpath(save_root, "mask")
mkpath(dir4image)
mkpath(dir4mask)
param_dir_root = joinpath(save_root, "param")
p = Progress(numof_images, "Generating...")
Base.Threads.@threads for image_id in 1:numof_images
rng = Xoshiro(image_id)
canvas = zeros(RGB{N0f8}, image_size, image_size)
mask = zeros(Gray{N0f8}, image_size, image_size)
for _ in 1:instance_num
c = rand(rng, 1:numof_classes)
fname = joinpath(param_dir_root, "$(@sprintf "%05d" c).toml")
d = TOML.parsefile(fname)
params = ToStruct.tostruct(SegRCDBInstanceParams, d)
#class_num = int(l[0][1])
class_num = params.Category_num
#vertex_number = int(l[1][1])
vertex_number = params.Vertex
#perlin_noise_coefficient = float(l[2][1])
perlin_noise_coefficient = params.Perlin_noise
#line_width = float(l[3][1])
line_width = params.line_width
#start_rad = float(l[4][1])
start_rad = params.Center_rad
#line_draw_num = int(l[5][1])
line_draw_num = params.Line_num
#oval_rate_x = float(l[6][1])
oval_rate_x = params.Oval_rate_x
#oval_rate_y = float(l[7][1])
oval_rate_y = params.Oval_rate_y
#g = int(l[8][1])
g = params.Color_Gray
class_color = Gray{N0f8}(reinterpret(N0f8, UInt8(g)))
start_pos_h = (image_size + rand(rng, -1*start_pos:start_pos)) / 2
start_pos_w = (image_size + rand(rng, -1*start_pos:start_pos)) / 2
θs = [i * 2π / vertex_number for i in 0:vertex_number]
vertex_x = Vector{Float64}(undef, vertex_number + 1)
vertex_y = Vector{Float64}(undef, vertex_number + 1)
for i in eachindex(θs, vertex_x, vertex_y)
s, c = sincos(θs[i])
vertex_x[i] = (c * start_rad * oval_rate_x + start_pos_w)
vertex_y[i] = (s * start_rad * oval_rate_y + start_pos_h)
end
noise_x = Vector{Float64}(undef, vertex_number + 1)
noise_y = Vector{Float64}(undef, vertex_number + 1)
for line_draw in 1:line_draw_num
for i in eachindex(noise_x, noise_y)
xu = uniform(rng, 0, 10000)
nx =
pnoise1(xu) * perlin_noise_coefficient * 2 -
perlin_noise_coefficient
noise_x[i] = nx
yu = uniform(rng, 0, 10000)
ny =
pnoise1(yu) * perlin_noise_coefficient * 2 -
perlin_noise_coefficient
noise_y[i] = ny
end
for i in eachindex(θs, vertex_x, vertex_y, noise_x, noise_y)
# Dear all
# WHAT IS `line_width` ??? I HAVE NO IDEA
s, c = sincos(θs[i])
vertex_x[i] -= c * (noise_x[i] - line_width)
vertex_y[i] -= s * (noise_y[i] - line_width)
end
# considering boundary condition
vertex_x[end] = vertex_x[begin]
vertex_y[end] = vertex_y[begin]
if mode == "M1"
for i in 1:vertex_number
p1 = Point(floor(Int, vertex_x[i]), floor(Int, vertex_y[i]))
p2 = Point(floor(Int, vertex_x[i+1]), floor(Int, vertex_y[i+1]))
draw!(mask, LineSegment(p1, p2), class_color)
end
end
object_gray = reinterpret(N0f8, rand(rng, UInt8))
object_color = RGB{N0f8}(object_gray, object_gray, object_gray)
for i in 1:vertex_number
p1 = Point(floor(Int, vertex_x[i]), floor(Int, vertex_y[i]))
p2 = Point(floor(Int, vertex_x[i+1]), floor(Int, vertex_y[i+1]))
draw!(
canvas,
LineSegment(p1, p2),
object_color,
)
end
end
end
imagename = @sprintf "%06d.png" image_id
save(
joinpath(dir4image, imagename),
canvas,
)
maskname = @sprintf "%06d.png" image_id
save(
joinpath(dir4mask, maskname),
mask,
)
# update progressmeter
next!(p)
end
finish!(p)
end
main()