在没有OpenCV的情况下,用C ++分割来自RGB数组的通道

时间:2014-02-07 04:51:46

标签: c++ image optimization image-processing

我在C ++中使用一个表示图像中像素的unsigned char数组。每个像素具有3个通道(R,G,B)。图像是线性表示的,有点像

RGBRGBRGBRGB .....

如何有效地将R,G和B中的每一个拆分成单独的数组?

我试过了:

  for(int pos = 0; pos < srcWidth * srcHeight; pos++) {
      int rgbPos = pos * 3;
      splitChannels[0][pos] = rgbSrcData[rgbPos];
      splitChannels[1][pos] = rgbSrcData[rgbPos + 1];
      splitChannels[2][pos] = rgbSrcData[rgbPos + 2];
  }

但这非常缓慢。

谢谢!

2 个答案:

答案 0 :(得分:1)

我的尝试:将四个字节加载并存储四个字节。字节扰乱将是乏味的,但吞吐量可能会提高。

// Load 4 interleaved pixels
unsigned int RGB0= ((int*)rgbSrcData)[i];
unsigned int RGB1= ((int*)rgbSrcData)[i + 1];
unsigned int RGB2= ((int*)rgbSrcData)[i + 2];

// Rearrange and store 4 unpacked pixels
((int*)splitChannels[0])[j]= 
    (RGB0 & 0xFF) | (RGB0 >> 24) | (RGB1 & 0xFF0000) | ((RGB2 & 0xFF00) << 16);
((int*)splitChannels[1])[j]= 
    ((RGB0 & 0xFF00) >> 8) | (RGB1 & 0xFF) | (RGB1 >> 24) | (RGB2 & 0xFF0000) >> 16;
((int*)splitChannels[2])[j]= 
    ((RGB0 & 0xFF0000) >> 16) | (RGB1 & 0xFF00) | ((RGB2 & 0xFF) >> 16) | (RGB2 & 0xFF000000);

(注意:未经检查!)也可以使用仅限移位版本。

SSE解决方案会更复杂(步幅3与2的幂相比)。

答案 1 :(得分:0)

用于使其运行更快的一种很好的技术是循环展开。 您可以在此处阅读:http://en.wikipedia.org/wiki/Loop_unwinding