# Opencv forEach with lambda expression for parallel processing

I'm using forEach function to access pixels in parallel like so:

// build look up table

unsigned char lut[256];
for (int i = 0; i < 256; i++)
lut[i] = cv::saturate_cast<uchar>(pow((float)(i / 255.0), fGamma) * 255.0f); //pow: power exponent

dst.forEach<cv::Vec3b>
(
[&lut](cv::Vec3b &pixel, const int* po) -> void
{
pixel[0] = lut[(pixel[0])];
pixel[1] = lut[(pixel[1])];
pixel[2] = lut[(pixel[2])];
}
);


My question is how can I make sure that it is accessing pixels in parallel?

I'm processing RGB images so I would like to make sure it is working in parallel.

And also in lambda expression without forEach function:

std::vector<std::vector<int> > histSum(3, std::vector<int>(256,0));
dst.forEach<cv::Vec3b>
(
[&histSum](cv::Vec3b &pixel, const int* po) -> void
{
++histSum[0][pixel[0]];
++histSum[1][pixel[1]];
++histSum[2][pixel[2]];
}
);

std::vector<int> A(3, 255);
auto A_estim_lambda([&A, rows, cols, &histSum]{

for (auto index = rows*cols/500; index>histSum[0][A[0]]; --A[0])
index -= histSum[0][A[0]];
for (auto index = rows*cols/500; index>histSum[1][A[1]]; --A[1])
index -= histSum[1][A[1]];
for (auto index = rows*cols/500; index>histSum[2][A[2]]; --A[2])
index -= histSum[2][A[2]];

return A;
});

auto AA=A_estim_lambda();


I'm not sure if this is processing in parallel and also I think I can write the last three loops in one loop but I don't know how or if it is possible.

Do you have any other remarks about my code?

Compilation:

g++ -std=c++1z -Wall -Ofast -march=native test3.cpp -o test3 pkg-config --cflags --libs opencv

• It's hard to say very much, given that you have no declarations for dst, pow and more. – Toby Speight Dec 12 '17 at 16:36
• @TobySpeight Sorry! I just edited my post – Ja_cpp Dec 12 '17 at 16:44