@@ -190,101 +190,94 @@ def run_annotate(
190190 fov_w , fov_h = meta ["fov" ]
191191 center = Coordinate (* meta ["center" ])
192192 console = Console ()
193- progress = Progress (
193+ with Progress (
194194 TextColumn ("[progress.description]{task.description}" ),
195195 BarColumn (),
196196 MofNCompleteColumn (),
197197 TimeRemainingColumn (),
198198 console = console
199- )
200- progress .start ()
201- task_images = progress .add_task (
202- "Preprocessing images" ,
203- total = len (meta ["images" ])
204- )
199+ ) as progress :
200+ for data in progress .track (
201+ meta ["images" ],
202+ description = "Preprocessing images"
203+ ):
204+ pos = Coordinate (* data ["position" ])
205+ vec = Coordinate (* data ["vector" ])
206+ path = images .get (data ["filename" ])
207+ if path is None :
208+ echo_yellow (f"Could not find '{ data ['filename' ]} '" )
209+ continue
205210
206- for data in meta ["images" ]:
207- pos = Coordinate (* data ["position" ])
208- vec = Coordinate (* data ["vector" ])
209- path = images .get (data ["filename" ])
210- if path is None :
211- echo_yellow (f"Could not find '{ data ['filename' ]} '" )
212- progress .update (task_images , advance = 1 )
213- continue
214-
215- img = cv .imread (str (path ))
216- if img is None :
217- echo_yellow (f"Could not load '{ data ['filename' ]} '" )
218- progress .update (task_images , advance = 1 )
219- continue
220-
221- hz , v , _ = vec .to_polar ()
222- height : int
223- width : int
224- height , width , _ = img .shape
225- f_w : float = width / 2 / np .tan (fov_w / 2 )
226- f_h : float = height / 2 / np .tan (fov_h / 2 )
227- if scale is None :
228- scale = (f_w + f_h ) / 2
211+ img = cv .imread (str (path ))
212+ if img is None :
213+ echo_yellow (f"Could not load '{ data ['filename' ]} '" )
214+ continue
229215
230- scale = min (scale , _MAX_SCALE )
216+ hz , v , _ = vec .to_polar ()
217+ height : int
218+ width : int
219+ height , width , _ = img .shape
220+ f_w : float = width / 2 / np .tan (fov_w / 2 )
221+ f_h : float = height / 2 / np .tan (fov_h / 2 )
222+ if scale is None :
223+ scale = (f_w + f_h ) / 2
231224
232- instrinsics : npt .NDArray [np .float32 ] = np .array (
233- (
234- (f_w , 0.0 , width / 2 ),
235- (0.0 , f_h , height / 2 ),
236- (0.0 , 0.0 , 1.0 )
237- )
238- ).astype ("float32" )
239- rot : npt .NDArray [np .float32 ] = (
240- rot_y (float (hz ))
241- @ rot_x (np .pi / 2 - float (v ))
242- ).astype ("float32" )
243-
244- warper = cv .PyRotationWarper ("spherical" , scale )
245- corner , image_warped = warper .warp (
246- img ,
247- instrinsics ,
248- rot ,
249- cv .INTER_LINEAR ,
250- cv .BORDER_REPLICATE
251- )
225+ scale = min (scale , _MAX_SCALE )
252226
253- _ , mask_warped = warper .warp (
254- np .full ((height , width ), 255 , "uint8" ),
255- instrinsics ,
256- rot ,
257- cv .INTER_NEAREST ,
258- cv .BORDER_CONSTANT
259- )
260- cx , cy = warper .warpPoint ((width / 2 , height / 2 ), instrinsics , rot )
227+ instrinsics : npt .NDArray [np .float32 ] = np .array (
228+ (
229+ (f_w , 0.0 , width / 2 ),
230+ (0.0 , f_h , height / 2 ),
231+ (0.0 , 0.0 , 1.0 )
232+ )
233+ ).astype ("float32" )
234+ rot : npt .NDArray [np .float32 ] = (
235+ rot_y (float (hz ))
236+ @ rot_x (np .pi / 2 - float (v ))
237+ ).astype ("float32" )
238+
239+ warper = cv .PyRotationWarper ("spherical" , scale )
240+ corner , image_warped = warper .warp (
241+ img ,
242+ instrinsics ,
243+ rot ,
244+ cv .INTER_LINEAR ,
245+ cv .BORDER_REPLICATE
246+ )
261247
262- centers .append (
263- (
264- int (cx ), int (cy ),
265- hz , v
248+ _ , mask_warped = warper .warp (
249+ np .full ((height , width ), 255 , "uint8" ),
250+ instrinsics ,
251+ rot ,
252+ cv .INTER_NEAREST ,
253+ cv .BORDER_CONSTANT
266254 )
267- )
268- corners .append (corner )
269- images_warped .append (image_warped )
270- masks_warped .append (mask_warped )
271-
272- # The de-rotation of the camera offset is not completely accurate
273- # since the optical axis of the camera might not be parallel to the
274- # axis of the telescope (which results in some angle deviation),
275- # but it is good enough estimation in case the offset is not precisely
276- # known beforehand.
277- #
278- # The matrix use in the spherical warp cannot be reused here, because
279- # OpenCV uses a different axis orientation order.
280- offset_rot = rot_z (float (hz )) @ rot_x (np .pi / 2 - float (v ))
281- cam_offsets .append (
282- apply_rotation (pos - center , np .linalg .inv (offset_rot ))
283- )
255+ cx , cy = warper .warpPoint (
256+ (width / 2 , height / 2 ), instrinsics , rot )
284257
285- progress .update (task_images , advance = 1 )
258+ centers .append (
259+ (
260+ int (cx ), int (cy ),
261+ hz , v
262+ )
263+ )
264+ corners .append (corner )
265+ images_warped .append (image_warped )
266+ masks_warped .append (mask_warped )
267+
268+ # The de-rotation of the camera offset is not completely accurate
269+ # since the optical axis of the camera might not be parallel to the
270+ # axis of the telescope (which results in some angle deviation),
271+ # but it is good enough estimation in case the offset is not
272+ # precisely known beforehand.
273+ #
274+ # The matrix use in the spherical warp cannot be reused here,
275+ # because OpenCV uses a different axis orientation order.
276+ offset_rot = rot_z (float (hz )) @ rot_x (np .pi / 2 - float (v ))
277+ cam_offsets .append (
278+ apply_rotation (pos - center , np .linalg .inv (offset_rot ))
279+ )
286280
287- progress .stop ()
288281 console .print ("Merging images... " , end = "" )
289282
290283 blender = cv .detail .Blender .createDefault (cv .detail .BLENDER_MULTI_BAND )
@@ -522,25 +515,28 @@ def main(
522515 if label_offset is None :
523516 label_offset = (label_fontsize // 2 , label_fontsize // 2 )
524517
525- run_annotate (
526- meta ,
527- output ,
528- image_map ,
529- scale ,
530- points ,
531- cam_offset ,
532- color ,
533- _FONT_MAP [font ],
534- fontscale ,
535- thickness ,
536- _MARKER_MAP [marker ],
537- markersize ,
538- offset ,
539- justify ,
540- _FONT_MAP [label_font ],
541- label_fontscale ,
542- label_thickness ,
543- label_color ,
544- label_offset ,
545- label_justify
546- )
518+ try :
519+ run_annotate (
520+ meta ,
521+ output ,
522+ image_map ,
523+ scale ,
524+ points ,
525+ cam_offset ,
526+ color ,
527+ _FONT_MAP [font ],
528+ fontscale ,
529+ thickness ,
530+ _MARKER_MAP [marker ],
531+ markersize ,
532+ offset ,
533+ justify ,
534+ _FONT_MAP [label_font ],
535+ label_fontscale ,
536+ label_thickness ,
537+ label_color ,
538+ label_offset ,
539+ label_justify
540+ )
541+ except cv .error as cve :
542+ echo_red (f"The process failed due to an OpenCV error ({ cve .code } )" )
0 commit comments