Ask Your Question
0

Colour conversion from BGR to RGB in cv2 is slower than on python

asked 2018-04-05 00:44:22 -0600

Santhosh1 gravatar image

updated 2018-04-05 00:44:42 -0600

I wanted to convert the BGR image red by the imread function to RGB

I timed both the cv and python to perform this operation

t1 = default_timer()
ima1 = cv2.cvtColor(ima,cv2.COLOR_BGR2RGB)
print("Time taken by cv2: ",default_timer()-t1)
t2 = default_timer()
ima2 = ima[..., ::-1]
print("Time taken by python: ",default_timer()-t2)

Here's the result

Time taken by cv2: 0.01933589199961716

Time taken by python: 7.061599990265677e-05

How is it that python is converting it faster than cv?

edit retag flag offensive close merge delete

Comments

try to convert twice and measure second call : opencv uses opencl and opencl kernel need to be compiled

LBerger gravatar imageLBerger ( 2018-04-05 02:30:36 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-04-05 03:16:14 -0600

dkurt gravatar image

@Santhosh1, your experiment is unfair because ima2 = ima[..., ::-1] permutes only indices but not the data. That means to check identical operations you have to call numpy.ascontiguousarray In example,

import numpy as np

a = np.zeros([4, 5, 3], dtype=np.uint8)
a[:,:,0] = ord('b')
a[:,:,1] = ord('g')
a[:,:,2] = ord('r')

print a
print a.flags
print a.data

gives

[[[ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]]

 [[ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]]

 [[ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]]

 [[ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]
  [ 98 103 114]]]

  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

bgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgrbgr

But

a = a[:,:,::-1]

print a
print a.flags
print a.data

cannot get raw data because an array became discontinuous

[[[114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]]

 [[114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]]

 [[114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]]

 [[114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]
  [114 103  98]]]

  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    print a.data
AttributeError: cannot get single-segment buffer for discontiguous array
edit flag offensive delete link more

Comments

I never got any error like this, I tried it just now. May be because you are doing a = a[:,:,::-1] while I have mentioned a=a[..., ::-1]

Santhosh1 gravatar imageSanthosh1 ( 2018-04-05 03:57:42 -0600 )edit

I didn't even get an AttributeError when I tried with a = a[:,:,::-1]

Santhosh1 gravatar imageSanthosh1 ( 2018-04-05 04:00:54 -0600 )edit

@Santhosh1, please specify versions of Python and NumPy.

dkurt gravatar imagedkurt ( 2018-04-05 06:52:41 -0600 )edit
0

answered 2019-09-30 17:14:13 -0600

tjwmmd2 gravatar image

Inspired by hearing about the "contiguous RAM" problems mentioned by dkurt, I went deeper and wrote a separate post with a long answer for everyone else who wants the fastest RGB/BGR conversion. It contains very important information. Spoiler alert: Do NOT use Numpy tricks!

https://answers.opencv.org/question/2...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-05 00:44:22 -0600

Seen: 35,276 times

Last updated: Sep 30 '19