[Martin-dev] Add data generation script
Browse files- data_gen.py +82 -0
data_gen.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import matplotlib.patches as patches
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
def data_generation(rounding_size=0.00, gray_value=0.5, rotation_angle=0, shape_size=0.2):
|
6 |
+
"""
|
7 |
+
Draws two symmetric upright squares on a square canvas, positioned symmetrically and rotated around center.
|
8 |
+
|
9 |
+
Parameters:
|
10 |
+
- rounding_size: float (0 to ~0.5), roundness of the soft-corner square
|
11 |
+
- gray_value: float (0 to 1), grayscale color of the squares (1=white, 0=black)
|
12 |
+
- rotation_angle: float, rotation angle in degrees (affects position only, not shape orientation)
|
13 |
+
- shape_size: float, size of the squares (0 to 1)
|
14 |
+
"""
|
15 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
16 |
+
|
17 |
+
# Colors
|
18 |
+
# color_sharp = (0.5, 0.5, 0.5)
|
19 |
+
color_sharp = (0.75, 0.75, 0.75)
|
20 |
+
color_rounded = (gray_value, gray_value, gray_value)
|
21 |
+
|
22 |
+
# Center point
|
23 |
+
cx, cy = 0.5, 0.5
|
24 |
+
# offset = 0.3
|
25 |
+
offset = 0.25
|
26 |
+
|
27 |
+
# Rotation matrix
|
28 |
+
theta = np.radians(rotation_angle)
|
29 |
+
cos_t, sin_t = np.cos(theta), np.sin(theta)
|
30 |
+
|
31 |
+
def rotate_point(x, y, cx, cy):
|
32 |
+
"""Rotate a point (x, y) around center (cx, cy) by theta radians."""
|
33 |
+
x_shifted = x - cx
|
34 |
+
y_shifted = y - cy
|
35 |
+
x_new = cos_t * x_shifted - sin_t * y_shifted + cx
|
36 |
+
y_new = sin_t * x_shifted + cos_t * y_shifted + cy
|
37 |
+
return x_new, y_new
|
38 |
+
|
39 |
+
# Left (sharp) square position
|
40 |
+
shape_size_sharp = 0.2
|
41 |
+
left_center = rotate_point(cx - offset, cy, cx, cy)
|
42 |
+
square_sharp = patches.Rectangle(
|
43 |
+
(left_center[0] - shape_size_sharp/2, left_center[1] - shape_size_sharp/2),
|
44 |
+
shape_size_sharp, shape_size_sharp,
|
45 |
+
linewidth=1, edgecolor=color_sharp, facecolor=color_sharp
|
46 |
+
)
|
47 |
+
ax.add_patch(square_sharp)
|
48 |
+
|
49 |
+
# Right (rounded) square position
|
50 |
+
right_center = rotate_point(cx + offset, cy, cx, cy)
|
51 |
+
square_rounded = patches.FancyBboxPatch(
|
52 |
+
(right_center[0] - shape_size/2, right_center[1] - shape_size/2),
|
53 |
+
shape_size, shape_size,
|
54 |
+
boxstyle=f"round,pad=0.00, rounding_size={rounding_size}",
|
55 |
+
linewidth=1, edgecolor=color_rounded, facecolor=color_rounded
|
56 |
+
)
|
57 |
+
ax.add_patch(square_rounded)
|
58 |
+
|
59 |
+
# Set limits and aspect
|
60 |
+
ax.set_xlim(0, 1)
|
61 |
+
ax.set_ylim(0, 1)
|
62 |
+
ax.set_aspect('equal')
|
63 |
+
ax.axis('off')
|
64 |
+
|
65 |
+
# Save and show
|
66 |
+
plt.savefig(
|
67 |
+
f"test_{rounding_size:.3f}_{gray_value:.2f}_{rotation_angle:.1f}_{shape_size:.2f}.png",
|
68 |
+
dpi=300, bbox_inches='tight'
|
69 |
+
)
|
70 |
+
plt.show()
|
71 |
+
|
72 |
+
|
73 |
+
for r in [0.00, 0.03, 0.06, 0.09, 0.12]:
|
74 |
+
for g in [0.75, 0.65, 0.55, 0.45, 0.35]:
|
75 |
+
for a in [-45, -22.5, 0, 22.5, 45]:
|
76 |
+
for s in [0.2, 0.25, 0.30, 0.35, 0.40]:
|
77 |
+
data_generation(
|
78 |
+
rounding_size=r,
|
79 |
+
gray_value=g,
|
80 |
+
rotation_angle=a,
|
81 |
+
shape_size=s
|
82 |
+
)
|