Skip to content

Commit

Permalink
Efficient channel swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsBurgh committed Feb 17, 2025
1 parent 73fa88a commit 303dcb0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion mmcv/image/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ def imfrombytes(content: bytes,
flag = imread_flags[flag] if is_str(flag) else flag
img = cv2.imdecode(img_np, flag)
if flag == IMREAD_COLOR and channel_order == 'rgb':
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
# Faster than cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
img = img[..., ::-1]
return img


Expand Down
9 changes: 6 additions & 3 deletions mmcv/image/photometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def imnormalize_(img, mean, std, to_rgb=True):
mean = np.float64(mean.reshape(1, -1))
stdinv = 1 / np.float64(std.reshape(1, -1))
if to_rgb:
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplace
# Faster than cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
img = img[..., ::-1]
cv2.subtract(img, mean, img) # inplace
cv2.multiply(img, stdinv, img) # inplace
return img
Expand All @@ -56,8 +57,9 @@ def imdenormalize(img, mean, std, to_bgr=True):
std = std.reshape(1, -1).astype(np.float64)
img = cv2.multiply(img, std) # make a copy
cv2.add(img, mean, img) # inplace
# Faster than cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img)
if to_bgr:
cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img) # inplace
img = img[..., ::-1]
return img


Expand Down Expand Up @@ -427,7 +429,8 @@ def adjust_lighting(img, eigval, eigvec, alphastd=0.1, to_rgb=True):

img = img.copy().astype(np.float32)
if to_rgb:
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplace
# Faster than cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
img = img[..., ::-1]

alpha = np.random.normal(0, alphastd, n_eigval)
alter = eigvec \
Expand Down

0 comments on commit 303dcb0

Please sign in to comment.