Skip to content

Commit 20bf221

Browse files
committed
Efficient channel swapping
1 parent 73fa88a commit 20bf221

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

mmcv/image/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def imfrombytes(content: bytes,
284284
flag = imread_flags[flag] if is_str(flag) else flag
285285
img = cv2.imdecode(img_np, flag)
286286
if flag == IMREAD_COLOR and channel_order == 'rgb':
287-
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
287+
img = img[..., ::-1] # Faster than cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
288288
return img
289289

290290

mmcv/image/photometric.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def imnormalize_(img, mean, std, to_rgb=True):
4444
mean = np.float64(mean.reshape(1, -1))
4545
stdinv = 1 / np.float64(std.reshape(1, -1))
4646
if to_rgb:
47-
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplace
47+
img = img[..., ::-1] # Faster than cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplace
4848
cv2.subtract(img, mean, img) # inplace
4949
cv2.multiply(img, stdinv, img) # inplace
5050
return img
@@ -57,7 +57,7 @@ def imdenormalize(img, mean, std, to_bgr=True):
5757
img = cv2.multiply(img, std) # make a copy
5858
cv2.add(img, mean, img) # inplace
5959
if to_bgr:
60-
cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img) # inplace
60+
img = img[..., ::-1] # Faster than cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img) # inplace
6161
return img
6262

6363

@@ -427,7 +427,7 @@ def adjust_lighting(img, eigval, eigvec, alphastd=0.1, to_rgb=True):
427427

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

432432
alpha = np.random.normal(0, alphastd, n_eigval)
433433
alter = eigvec \

0 commit comments

Comments
 (0)