Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient channel swapping #3242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MatthijsBurgh
Copy link

@MatthijsBurgh MatthijsBurgh commented Feb 17, 2025

Motivation

Swapping the channels manually is much faster than the conversion function. It is also independent of the image size.

In [1]: import numpy as np

In [2]: import cv2

In [3]: bla = (np.random.rand(1080, 1920, 3) * 255).astype(np.float32)

In [4]: bla2 = (np.random.rand(2080, 3920, 3) * 255).astype(np.float32)

In [5]: timeit cv2.cvtColor(bla, cv2.COLOR_BGR2RGB)
818 μs ± 14.3 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [6]: timeit cv2.cvtColor(bla2, cv2.COLOR_BGR2RGB)
11.7 ms ± 79.6 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [7]: timeit bla[..., ::-1]
77.7 ns ± 3.16 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [8]: timeit bla2[..., ::-1]
75.7 ns ± 0.438 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Modification

Replace cvtColor calls to manually swapping for BGR2RGB or vice-versa.

BC-breaking (Optional)

Nope

Use cases (Optional)

No new features

Checklist

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
  • CLA has been signed and all committers have signed the CLA in this PR.

@CLAassistant
Copy link

CLAassistant commented Feb 17, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants