cv::Mat.forEach - how does position work?

asked 2016-09-20 04:21:52 -0600

Benjamin M gravatar image

updated 2016-09-21 06:31:36 -0600

AFAIK the signature of the function param of Mat.forEach is:

void (*)(const T value, const *int pos);

EDIT: Well, according to core/utility.hpp it's more like: (which should go into the documentation, IMHO)

//     (_Tp&, const int*)   <- multidimential
//  or (_Tp&, void*)        <- in case of you don't need current idx.

Now, I'm not sure what the pos argument is and I don't think the documentation is absolutely clear on that. From the documentation, it almost looks like this is the channel of the current element?

I have now used it in my project for a CV_64FC1 Matrix and pos[1] is the current collumn. Since pos[0] is always zero and this is a single row Matrix, I guess it's pos[dimension]?

So when I want to use it with m.at<t>() I need to calculate the index from the pos elments? I would like to better understand this, then I could make a pull request with a documentation improvement. I have looked at the code, but I'm not sure I understand the multidimensional case correctly.

edit retag flag offensive close merge delete

Comments

Could you explicitly explain why you are in the desire of using this function, when the at operater is easier to use?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-09-22 06:19:04 -0600 )edit
1

It's not either or, it's both. I want to perform an operation for every element and I need to compare that element to it's neighbors, that's why I want the index. forEach is done in parallel (says the documentation), whereas if I would go through the Matrix via index and .at(), that wouldn't be the case. Also I think forEach + Lambda Expression looks cleaner than explicit loops.

Benjamin M gravatar imageBenjamin M ( 2016-09-22 07:45:00 -0600 )edit

Do you succeed to compile @Benjamin M

struct Operator {
    void operator ()(Pixel &pixel, const int * position) {
        pixel.x = 255;
    }
};
image.forEach<Pixel>(Operator());

with vs 2015 or gcc version 6.2.0 (x86_64-posix-sjlj-rev1, Built by MinGW-W64 project)?

LBerger gravatar imageLBerger ( 2016-10-24 15:43:19 -0600 )edit