@@ -125,24 +125,37 @@ def determine_skew_debug_images(
125
125
angle_pm_90 = angle_pm_90 ,
126
126
)
127
127
hough_line_data , hough_line_peaks_data , all_freqs = data
128
- freqs_original , freqs = all_freqs
128
+ freqs0 , freqs = all_freqs
129
129
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
131
136
132
- limits = []
137
+ limits : List [Tuple [float , float ]] = []
138
+ limits2 : List [Tuple [float , float ]] = []
133
139
if min_angle is not None and max_angle is not None :
134
140
min_angle_norm = min_angle % (np .pi / (1 if angle_pm_90 else 2 ))
135
141
max_angle_norm = max_angle % (np .pi / (1 if angle_pm_90 else 2 ))
136
142
if min_angle_norm < max_angle_norm :
137
143
min_angle_norm += np .pi / (1 if angle_pm_90 else 2 )
138
144
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
141
149
if min_angle_limit < max_angle_limit :
142
150
limits .append ((- np .pi / 2 , min_angle_limit ))
143
151
limits .append ((max_angle_limit , np .pi / 2 ))
144
152
else :
145
153
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 ))
146
159
147
160
debug_images = []
148
161
@@ -160,14 +173,10 @@ def determine_skew_debug_images(
160
173
axe .set_title ("Hough transform" )
161
174
axe .set_xlabel ("Angles (degrees)" )
162
175
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" )
169
178
170
- for limit_min , limit_max in limits :
179
+ for limit_min , limit_max in limits2 :
171
180
if limit_min != - np .pi / 2 :
172
181
axe .axline ((np .rad2deg (limit_min ), 0 ), (np .rad2deg (limit_min ), 10 ))
173
182
if limit_max != np .pi / 2 :
@@ -211,21 +220,25 @@ def determine_skew_debug_images(
211
220
axe .set_axis_off ()
212
221
axe .set_title ("Detected lines" )
213
222
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 )])
216
225
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 )
218
229
)
219
230
diff = float (abs (angle2 - skew_angle )) if skew_angle is not None else 999.0
220
231
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
+ )
222
235
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 )
224
237
axe .text (
225
238
coord0x ,
226
239
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 ),
229
242
rotation_mode = "anchor" ,
230
243
transform_rotates_text = True ,
231
244
)
@@ -245,7 +258,13 @@ def determine_skew_debug_images(
245
258
axe [0 ].set_title ("Original detected angles" )
246
259
axe [1 ].set_title ("Corrected angles" )
247
260
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 :
249
268
axe .scatter (freqs .keys (), freqs .values ())
250
269
axe .set_theta_zero_location ("N" )
251
270
axe .grid (True )
@@ -256,19 +275,17 @@ def fill_polar(axe: Any, freqs: Dict[np.float64, int], half: bool = False) -> No
256
275
axe .set_thetamin (- 90 )
257
276
axe .set_thetamax (90 )
258
277
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" )
263
280
264
281
for limit_min , limit_max in limits :
265
282
if limit_min != - np .pi / 2 and (not half or - np .pi / 4 < limit_min < np .pi / 4 ):
266
283
axe .axvline (limit_min )
267
284
if limit_max != np .pi / 2 and (not half or - np .pi / 4 < limit_max < np .pi / 4 ):
268
285
axe .axvline (limit_max )
269
286
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 )
272
289
273
290
plt .tight_layout ()
274
291
with tempfile .NamedTemporaryFile (suffix = ".png" ) as file :
0 commit comments