我有一个具有不同拜耳模式的原始图像。 这就是我为了分离 channel 而实现的。 速度在这里非常重要,因为它将在数千张大图像上运行。
能否请您提出代码优化建议。 我知道 %(模数)不是很快,例如我如何替换它?
谢谢
void Utilities::SeparateChannels(int** _image, int*& gr, int*& r, int*& b, int*& gb,int _width, int _height, int _colorOrder)
{
//swith case the color Order
int counter_R = 0;
int counter_GR = 0;
int counter_GB = 0;
int counter_B = 0;
switch (_colorOrder)
{
//rggb
case 0:
for (int i = 0; i < _height; i++)
{
for (int j = 0; j < _width; j++)
{
if (i % 2 == 0 && j % 2 == 0)
{
r[counter_R] = _image[i][j];
counter_R++;
}
else if (i % 2 == 0 && j % 2 == 1)
{
gr[counter_GR] = _image[i][j];
counter_GR++;
}
else if (i % 2 == 1 && j % 2 == 0)
{
gb[counter_GB] = _image[i][j];
counter_GB++;
}
else if (i % 2 == 1 && j % 2 == 1)
{
b[counter_B] = _image[i][j];
counter_B++;
}
}
}
break;
default:
break;
}
}
最佳答案
可能值得考虑的一种可能性是将目标 channel 数据的数组设置为数组本身:
int *channels[] = {r, gr, gb, b};
同样,将计数器设置为数组:
int counters[4] = {0};
...那么您的代码可能会是这样的:
for (int i=0; i<_height; i++)
for (int j=0; j<_width; j++) {
channel = (i&1) << 1 + (j&1);
int &counter = counters[channel];
channels[channel][counter++] = image[i][j];
}
基本思想是我们将 i
和 j
的低位组合成一个数字,我们可以将其用作 channel 地址。然后我们使用该数字索引 channel 和该 channel 的计数器。
您的编译器可能已经在优化现有代码,使其大致等同于此(或什至可能比此产生的更好),但也有可能不是。
不过,我通常不会期望有很多改进(至少在典型的台式计算机上是这样)。我预计瓶颈是主内存的带宽,几乎与您如何编写循环的细节无关。
关于c++ - 将拜耳图像分离到颜色 channel C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26694295/