Courtesy of the OpenCV 2.3 GPU code comes a neat snippet of code for using a template parameter for reading RGB or BGR ordered components when dealing with RGB triplets.

The Code

template <int blueIndex>
float rgb2grey(const float *src)
   return 0.114f*src[blueIndex^2] + 0.587f*src[1] + 0.299f*src[blueIndex];

Then to use the function you simply supply the index where the blue value resides to take care of the RGB vs BGR ordering.

For RGB ordering:

float g = rgb2grey<2>(src);

And for BGR ordering:

float g = rgb2grey<0>(src);

This only works for swapping R and B around and won't work for more weird and wonderful orderings

The original OpenCV code can be found in modules/gpu/src/opencv2/gpu/device/detail/color.hpp.

Templates and CUDA

For me the fact that you can use template meta-programming is a real plus point of CUDA. It allows for good code re-use and the template expansion gives good scope for the compiler to optimise. It can also allow you to remove conditionals from kernels in appropriate circumstances - more in a future post!

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>