Skip to content

Commit 2897480

Browse files
committed
[panorama] updated progress reporting in panorama processing
1 parent 22096ce commit 2897480

File tree

1 file changed

+114
-108
lines changed

1 file changed

+114
-108
lines changed

src/instrumentman/panorama/process.py

Lines changed: 114 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -278,133 +278,139 @@ def run_annotate(
278278
apply_rotation(pos - center, np.linalg.inv(offset_rot))
279279
)
280280

281-
console.print("Merging images... ", end="")
281+
with Progress(transient=True) as progress:
282+
progress.add_task(description="Merging images...", total=None)
282283

283-
blender = cv.detail.Blender.createDefault(cv.detail.BLENDER_MULTI_BAND)
284-
blender.prepare(
285-
corners,
286-
[(i.shape[1], i.shape[0]) for i in images_warped]
287-
)
288-
for corner, img, msk in zip(corners, images_warped, masks_warped):
289-
dilated_mask = cv.dilate(msk, None) # type: ignore[call-overload]
290-
seam_mask = cv.resize(
291-
dilated_mask,
292-
(msk.shape[1], msk.shape[0]),
293-
None,
294-
0,
295-
0,
296-
cv.INTER_LINEAR_EXACT
297-
)
298-
msk_warped = cv.bitwise_and(seam_mask, msk)
299-
blender.feed(img.astype("int16"), msk_warped, corner)
300-
301-
result: cvt.MatLike
302-
result, _ = blender.blend(
303-
None, None
304-
) # type: ignore[call-overload]
305-
306-
console.print("Done")
307-
if len(points) > 0:
308-
console.print("Annotating points... ", end="")
309-
310-
# Top left image center point for reference
311-
origin_x, origin_y, _, _ = cv.detail.resultRoi(
284+
blender = cv.detail.Blender.createDefault(cv.detail.BLENDER_MULTI_BAND)
285+
blender.prepare(
312286
corners,
313287
[(i.shape[1], i.shape[0]) for i in images_warped]
314288
)
315-
tl_x, tl_y, tl_hz, tl_v = centers[0]
316-
tl_x -= origin_x
317-
tl_y -= origin_y
318-
319-
if scale is None:
320-
scale = 1000
321-
322-
full_360 = round(scale * np.pi * 2)
289+
for corner, img, msk in zip(corners, images_warped, masks_warped):
290+
dilated_mask = cv.dilate(msk, None) # type: ignore[call-overload]
291+
seam_mask = cv.resize(
292+
dilated_mask,
293+
(msk.shape[1], msk.shape[0]),
294+
None,
295+
0,
296+
0,
297+
cv.INTER_LINEAR_EXACT
298+
)
299+
msk_warped = cv.bitwise_and(seam_mask, msk)
300+
blender.feed(img.astype("int16"), msk_warped, corner)
323301

324-
if camera_offset is None:
325-
camera_offset = mean_coordinate(cam_offsets)
302+
result: cvt.MatLike
303+
result, _ = blender.blend(
304+
None, None
305+
) # type: ignore[call-overload]
326306

327-
for pt, coord, label in points:
328-
# To calculate the approximate "telescope" rotation, a preliminary
329-
# polar position is needed. Then the camera offset is rotated with
330-
# the preliminary angles.
331-
prelim_hz, prelim_v, _ = (coord - center).to_polar()
332-
offset_rot = (
333-
rot_z(float(prelim_hz)) @ rot_x(np.pi / 2 - float(prelim_v))
334-
)
335-
pt_hz, pt_v, _ = (
336-
coord
337-
- (center + apply_rotation(camera_offset, offset_rot))
338-
).to_polar()
339-
340-
pt_hz_f = float(pt_hz - tl_hz)
341-
pt_v_f = float(pt_v - tl_v)
342-
pt_x = round(tl_x + pt_hz_f * scale) % full_360
343-
pt_y = round(tl_y + pt_v_f * scale) % full_360
344-
345-
cv.drawMarker(
346-
result,
347-
(pt_x, pt_y),
348-
color,
349-
marker,
350-
markersize,
351-
thickness
307+
console.print("Merged images")
308+
if len(points) > 0:
309+
with Progress(transient=True) as progress:
310+
# Top left image center point for reference
311+
origin_x, origin_y, _, _ = cv.detail.resultRoi(
312+
corners,
313+
[(i.shape[1], i.shape[0]) for i in images_warped]
352314
)
315+
tl_x, tl_y, tl_hz, tl_v = centers[0]
316+
tl_x -= origin_x
317+
tl_y -= origin_y
353318

354-
cv.putText(
355-
result,
356-
pt,
357-
text_pos(
358-
pt,
319+
if scale is None:
320+
scale = 1000
321+
322+
full_360 = round(scale * np.pi * 2)
323+
324+
if camera_offset is None:
325+
camera_offset = mean_coordinate(cam_offsets)
326+
327+
for pt, coord, label in progress.track(
328+
points,
329+
description="Annotating points"
330+
):
331+
# To calculate the approximate "telescope" rotation, a
332+
# preliminary polar position is needed. Then the camera offset
333+
# is rotated with the preliminary angles.
334+
prelim_hz, prelim_v, _ = (coord - center).to_polar()
335+
offset_rot = (
336+
rot_z(float(prelim_hz))
337+
@ rot_x(np.pi / 2 - float(prelim_v))
338+
)
339+
pt_hz, pt_v, _ = (
340+
coord
341+
- (center + apply_rotation(camera_offset, offset_rot))
342+
).to_polar()
343+
344+
pt_hz_f = float(pt_hz - tl_hz)
345+
pt_v_f = float(pt_v - tl_v)
346+
pt_x = round(tl_x + pt_hz_f * scale) % full_360
347+
pt_y = round(tl_y + pt_v_f * scale) % full_360
348+
349+
cv.drawMarker(
350+
result,
359351
(pt_x, pt_y),
360-
offset,
352+
color,
353+
marker,
354+
markersize,
355+
thickness
356+
)
357+
358+
cv.putText(
359+
result,
360+
pt,
361+
text_pos(
362+
pt,
363+
(pt_x, pt_y),
364+
offset,
365+
font,
366+
fontscale,
367+
thickness,
368+
justify
369+
),
361370
font,
362371
fontscale,
372+
color,
363373
thickness,
364-
justify
365-
),
366-
font,
367-
fontscale,
368-
color,
369-
thickness,
370-
bottomLeftOrigin=False
371-
)
372-
if label == "":
373-
continue
374+
bottomLeftOrigin=False
375+
)
376+
if label == "":
377+
continue
374378

375-
cv.putText(
376-
result,
377-
label,
378-
text_pos(
379+
cv.putText(
380+
result,
379381
label,
380-
(pt_x, pt_y),
381-
label_offset,
382+
text_pos(
383+
label,
384+
(pt_x, pt_y),
385+
label_offset,
386+
label_font,
387+
label_fontscale,
388+
label_thickness,
389+
label_justify
390+
),
382391
label_font,
383392
label_fontscale,
393+
label_color,
384394
label_thickness,
385-
label_justify
386-
),
387-
label_font,
388-
label_fontscale,
389-
label_color,
390-
label_thickness,
391-
bottomLeftOrigin=False
392-
)
395+
bottomLeftOrigin=False
396+
)
393397

394-
console.print("Done")
395-
396-
console.print("Saving final image... ", end="")
397-
# For some reason the blending function returns the image as int16 instead
398-
# uint8, and it might contain negative values. These need to be clipped,
399-
# otherwise the type conversion will result in color artifacts due to the
400-
# integer underflow.
401-
result = np.clip(result, 0, 255)
402-
cv.imwrite(
403-
str(output),
404-
result.astype(np.uint8)
405-
)
398+
console.print("Annotated points")
399+
400+
with Progress(transient=True) as progress:
401+
progress.add_task("Saving final image...", total=None)
402+
# console.print("Saving final image... ", end="")
403+
# For some reason the blending function returns the image as int16
404+
# instead uint8, and it might contain negative values. These need to be
405+
# clipped, otherwise the type conversion will result in color artifacts
406+
# due to the integer underflow.
407+
result = np.clip(result, 0, 255)
408+
cv.imwrite(
409+
str(output),
410+
result.astype(np.uint8)
411+
)
406412

407-
console.print("Done")
413+
console.print("Saved final image")
408414
console.print("Panorama complete", style="green")
409415

410416

0 commit comments

Comments
 (0)