Skip to content

Use numpy.column_stack() instead of numpy.transpose(numpy.nonzero(...)) for clarity and performance #17

@SaFE-APIOpt

Description

@SaFE-APIOpt

black_pixel_indexes = numpy.transpose(numpy.nonzero(bg <= 150))

In the code:
black_pixel_indexes = numpy.transpose(numpy.nonzero(bg <= 150))
a more efficient and clearer alternative is:
black_pixel_indexes = numpy.column_stack(numpy.nonzero(bg <= 150))
Both versions return the same result — a 2D array where each row represents the [row, column] index of pixels meeting the condition. However, numpy.column_stack() is designed specifically for this use case and avoids the extra step of transposing the result, making the intent clearer and execution slightly faster.

This is a minor yet effective code quality and performance improvement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions