5

I'm not sure if there exists a function in OpenCV (C++) to do this.

I want to call a custom defined function for every pixel of an cv::Mat in OpenCV and the entire result should be stored in a matrix.

Can I do this in a single line of code (something similar to map function in Python)?

garak
  • 4,713
  • 9
  • 39
  • 56

1 Answers1

6

I have not tried this but according to the docs there are STL style iterators for accessing the elements of a matrix:

// compute sum of positive matrix elements, iterator-based variant
double sum=0;
MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>();
for(; it != it_end; ++it)
    sum += std::max(*it, 0.);

If they are implemented correctly you should be able to use them with std::for_each approximately like this

std::for_each(M.begin<double>(), M.end<double>(), [](double& e) { /* do something with e */ });
Sarien
  • 6,647
  • 6
  • 35
  • 55
  • Your lambda should have `double& e` as a parameter. Using `double` will make a copy and not assign to the image data. – Aurelius Apr 30 '13 at 23:31
  • This looks like a good idea. I wonder if we could do it for an entire row instead of a pixel; – garak May 01 '13 at 15:06
  • You could increase the stepsize to the row length using an adaptor as described here: http://stackoverflow.com/questions/12726466/get-each-nth-element-of-iterator-range – Sarien May 01 '13 at 15:31