Skip to content

Commit 1dc4ada

Browse files
authored
Merge pull request #256 from sbrunner/fix_angles
Fix angles
2 parents 460000c + febccb4 commit 1dc4ada

File tree

36 files changed

+48
-37
lines changed

36 files changed

+48
-37
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
python-version:
2626
- '3.8'
2727
- '3.9'
28+
- '3.10'
2829

2930
steps:
3031
- name: GitHub event

deskew/__init__.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,37 @@ def determine_skew_debug_images(
125125
angle_pm_90=angle_pm_90,
126126
)
127127
hough_line_data, hough_line_peaks_data, all_freqs = data
128-
freqs_original, freqs = all_freqs
128+
freqs0, freqs = all_freqs
129129

130-
skew_angle2 = None if skew_angle is None else (skew_angle + np.pi / 2) % (np.pi) - np.pi / 2
130+
booth_angle: List[float] = []
131+
skew_angles0: List[float] = []
132+
if skew_angle is not None:
133+
skew_angle0: float = float(skew_angle % np.pi - np.pi / 2)
134+
booth_angle = [float(skew_angle), skew_angle0]
135+
skew_angles0 = [skew_angle0] if angle_pm_90 else booth_angle
131136

132-
limits = []
137+
limits: List[Tuple[float, float]] = []
138+
limits2: List[Tuple[float, float]] = []
133139
if min_angle is not None and max_angle is not None:
134140
min_angle_norm = min_angle % (np.pi / (1 if angle_pm_90 else 2))
135141
max_angle_norm = max_angle % (np.pi / (1 if angle_pm_90 else 2))
136142
if min_angle_norm < max_angle_norm:
137143
min_angle_norm += np.pi / (1 if angle_pm_90 else 2)
138144
for add in [0.0] if angle_pm_90 else [0.0, np.pi / 2]:
139-
min_angle_limit = (min_angle_norm + add + np.pi / 2) % np.pi - np.pi / 2
140-
max_angle_limit = (max_angle_norm + add + np.pi / 2) % np.pi - np.pi / 2
145+
min_angle_limit: float = (min_angle_norm + add + np.pi / 2) % np.pi - np.pi / 2
146+
max_angle_limit: float = (max_angle_norm + add + np.pi / 2) % np.pi - np.pi / 2
147+
min_angle_limit2: float = min_angle_limit % np.pi - np.pi / 2
148+
max_angle_limit2: float = max_angle_limit % np.pi - np.pi / 2
141149
if min_angle_limit < max_angle_limit:
142150
limits.append((-np.pi / 2, min_angle_limit))
143151
limits.append((max_angle_limit, np.pi / 2))
144152
else:
145153
limits.append((max_angle_limit, min_angle_limit))
154+
if min_angle_limit2 < max_angle_limit2:
155+
limits2.append((-np.pi / 2, min_angle_limit2))
156+
limits2.append((max_angle_limit2, np.pi / 2))
157+
else:
158+
limits2.append((max_angle_limit2, min_angle_limit2))
146159

147160
debug_images = []
148161

@@ -160,14 +173,10 @@ def determine_skew_debug_images(
160173
axe.set_title("Hough transform")
161174
axe.set_xlabel("Angles (degrees)")
162175
axe.set_ylabel("Distance (pixels)")
163-
if skew_angle is not None:
164-
axe.axline((np.rad2deg(skew_angle), 0), (np.rad2deg(skew_angle), 10), color="lightgreen")
165-
if not angle_pm_90:
166-
if TYPE_CHECKING:
167-
assert skew_angle2 is not None # nosec
168-
axe.axline((np.rad2deg(skew_angle2), 0), (np.rad2deg(skew_angle2), 10), color="lightgreen")
176+
for angle in skew_angles0:
177+
axe.axline((np.rad2deg(angle), 0), (np.rad2deg(angle), 10), color="lightgreen")
169178

170-
for limit_min, limit_max in limits:
179+
for limit_min, limit_max in limits2:
171180
if limit_min != -np.pi / 2:
172181
axe.axline((np.rad2deg(limit_min), 0), (np.rad2deg(limit_min), 10))
173182
if limit_max != np.pi / 2:
@@ -211,21 +220,25 @@ def determine_skew_debug_images(
211220
axe.set_axis_off()
212221
axe.set_title("Detected lines")
213222

214-
for _, angle, dist in zip(*hough_line_peaks_data):
215-
(coord0x, coord0y) = dist * np.array([np.cos(angle), np.sin(angle)])
223+
for _, line_angle, dist in zip(*hough_line_peaks_data):
224+
(coord0x, coord0y) = dist * np.array([np.cos(line_angle), np.sin(line_angle)])
216225
angle2 = (
217-
(angle % np.pi - np.pi / 2) if angle_pm_90 else ((angle + np.pi / 4) % (np.pi / 2) - np.pi / 4)
226+
(line_angle % np.pi - np.pi / 2)
227+
if angle_pm_90
228+
else ((line_angle + np.pi / 4) % (np.pi / 2) - np.pi / 4)
218229
)
219230
diff = float(abs(angle2 - skew_angle)) if skew_angle is not None else 999.0
220231
if diff < 0.001:
221-
axe.axline((coord0x, coord0y), slope=np.tan(angle + np.pi / 2), linewidth=1, color="lightgreen")
232+
axe.axline(
233+
(coord0x, coord0y), slope=np.tan(line_angle + np.pi / 2), linewidth=1, color="lightgreen"
234+
)
222235
else:
223-
axe.axline((coord0x, coord0y), slope=np.tan(angle + np.pi / 2), linewidth=1)
236+
axe.axline((coord0x, coord0y), slope=np.tan(line_angle + np.pi / 2), linewidth=1)
224237
axe.text(
225238
coord0x,
226239
coord0y,
227-
f"{round(np.rad2deg(angle)*1000)/1000}",
228-
rotation=np.rad2deg(angle - np.pi / 2),
240+
f"{round(np.rad2deg(line_angle)*1000)/1000}",
241+
rotation=np.rad2deg(line_angle - np.pi / 2),
229242
rotation_mode="anchor",
230243
transform_rotates_text=True,
231244
)
@@ -245,7 +258,13 @@ def determine_skew_debug_images(
245258
axe[0].set_title("Original detected angles")
246259
axe[1].set_title("Corrected angles")
247260

248-
def fill_polar(axe: Any, freqs: Dict[np.float64, int], half: bool = False) -> None:
261+
def fill_polar(
262+
axe: Any,
263+
freqs: Dict[np.float64, int],
264+
angles: List[float],
265+
limits: List[Tuple[float, float]],
266+
half: bool = False,
267+
) -> None:
249268
axe.scatter(freqs.keys(), freqs.values())
250269
axe.set_theta_zero_location("N")
251270
axe.grid(True)
@@ -256,19 +275,17 @@ def fill_polar(axe: Any, freqs: Dict[np.float64, int], half: bool = False) -> No
256275
axe.set_thetamin(-90)
257276
axe.set_thetamax(90)
258277

259-
if skew_angle is not None:
260-
axe.axvline(skew_angle, color="lightgreen")
261-
if not angle_pm_90 or not half:
262-
axe.axvline(skew_angle2, color="lightgreen")
278+
for angle in angles:
279+
axe.axvline(angle, color="lightgreen")
263280

264281
for limit_min, limit_max in limits:
265282
if limit_min != -np.pi / 2 and (not half or -np.pi / 4 < limit_min < np.pi / 4):
266283
axe.axvline(limit_min)
267284
if limit_max != np.pi / 2 and (not half or -np.pi / 4 < limit_max < np.pi / 4):
268285
axe.axvline(limit_max)
269286

270-
fill_polar(axe[0], freqs_original)
271-
fill_polar(axe[1], freqs, not angle_pm_90)
287+
fill_polar(axe[0], freqs0, skew_angles0, limits2)
288+
fill_polar(axe[1], freqs, [] if skew_angle is None else [float(skew_angle)], limits, not angle_pm_90)
272289

273290
plt.tight_layout()
274291
with tempfile.NamedTemporaryFile(suffix=".png") as file:
1012 Bytes
Loading
2.68 KB
Loading
2.68 KB
Loading
2.68 KB
Loading
2.59 KB
Loading
2.59 KB
Loading
1.02 KB
Loading
2.68 KB
Loading

0 commit comments

Comments
 (0)