簡體   English   中英

在沒有OpenCV的C ++中從RGB數組拆分通道

[英]Split channels from RGB array in C++ without OpenCV

我正在C ++中使用表示圖像像素的無符號字符數組。 每個像素具有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];
  }

但這出奇地慢。

謝謝!

我的嘗試:加載並存儲字節四乘四。 字節加擾將是乏味的,但吞吐量可能會提高。

// 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的冪次不相符)。

使循環運行更快的一項很棒的技術是循環展開。 您可以在這里閱讀有關內容: http : //en.wikipedia.org/wiki/Loop_unwinding

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM