Ask Your Question
0

Sub Mat in mulidimensional Mat

asked 2019-09-30 05:02:19 -0500

aligoglos gravatar image

updated 2019-09-30 07:28:46 -0500

berak gravatar image

I want to convert some codes from python to c++ . for example we have 3 dimensional array like (25 * 40 * 10) and we want to separate array, in python we have numpy arrays and simply do it with:

Probs = Y[...,0]
Affines = Y[...,2:] 
rx,ry = Y.shape[:2]
ywh = Y.shape[1::-1]
iwh = np.array(I.shape[1::-1],dtype=float).reshape((2,1))

but how can I implement this line in c++ with Mat?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-09-30 08:26:04 -0500

berak gravatar image

assuming, you have something like this (e.g. from a dnn blob):

int sz[] = {
    2, // batch size
    3, // num channels
    4, // H
    5  // W
};

Mat m(4,sz,CV_32F);
cout << m.size << endl; // note: without () !!

then you can iterate over the outer "peels", and get a 2d slice to the most inner 2d images

for (int i=0t; i<m.size[0]; i++) {
    for (int j=0; j<m.size[1]; j++) {
        // get the most inner slice:
        Mat inner(m.size[2], m.size[3], CV_32F, m.ptr<float>(i,j));
        cout << i << " " << j << " " << inner.size() << endl;       
    }
}

output:

2 x 3 x 4 x 5
0 0 [5 x 4]
0 1 [5 x 4]
0 2 [5 x 4]
1 0 [5 x 4]
1 1 [5 x 4]
1 2 [5 x 4]
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-09-30 05:02:19 -0500

Seen: 45 times

Last updated: Sep 30 '19