The following function will randomly "sprinkle salt" on a loaded image. For the sake of boosting performance, the conditional statement
uint j = rows == 1 ? 0 : randomRow(generator);
should not be inside the loop.
Instead, I want to define a lambda getJ
before the loop as
auto getJ = rows == 1 ? []() {return 0; } : []() {return randomRow(generator); };
However, my code with this lambda does not compile with the following red squiggled text:
Question
How to conditionally define such a lambda?
void salt_(Mat mat, unsigned long long n)
{
const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;
if (mat.isContinuous())
{
cols *= rows;
rows = 1;
}
default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);
uniform_int_distribution<uint> randomCol(0, cols - 1);
// auto getJ = rows == 1 ? []() {return 0; } : []() {return randomRow(generator); };
uchar * const data = mat.data;
for (unsigned long long counter = 0; counter < n; counter++)
{
uint i = randomCol(generator);
uint j = rows == 1 ? 0 : randomRow(generator);
//uint j = getJ();
uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;
}
}