Skip to content

incorrect sorting in stitching_detailed.py #18650

@dave-andersen

Description

@dave-andersen

In samples/python/stitching_detailed.py, this code is supposed to grab the median focal value:

    focals = []
    for cam in cameras:
        focals.append(cam.focal)
    sorted(focals)
    if len(focals) % 2 == 1:
        warped_image_scale = focals[len(focals) // 2]
    else:
        warped_image_scale = (focals[len(focals) // 2] + focals[len(focals) // 2 - 1]) / 2

But it doesn't - sorted(focals) returns a copy of the array focals, but does not modify the original.

The code should instead read:

  focals.sort()

I assume it's better to keep this code as parallel as possible to the C++ version, but if someone wants to make a bigger change beyond fixing the bug, the initialization of focals here is non-pythonic, and would read better to me as,

  focals = sorted([cam.focal for cam in cameras])

which has the added benefit of making it harder to introduce the bug above.

(Or letting np do it, which exposes the idea it's looking for a median:

warped_image_scale = float(np.median([c.focal for c in cameras]))

Thanks as always for a fantastic framework!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions