Ask Your Question
0

How to extract 2D Mat from a 4D Mat

asked 2017-10-24 02:34:54 -0600

isra60 gravatar image

updated 2017-10-24 03:06:09 -0600

berak gravatar image

I have a Cv::Mat calle Prob

prob = deepLearningModel.forward();

This mat have 4 dimensions , and the size of each dimensions are tipically 1-1-nDetections-detectionsParameter

So I want is a 2D Mat based on the last dimensions , I mean nDetections*detectionsPameteter How can I achieve it??

Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-10-24 03:02:00 -0600

berak gravatar image

updated 2017-10-24 05:37:23 -0600

those 4d Mat's used in cv::dnn are: [nimages, nchannels, H, W]

if your blob is a 3 channel image (with seperate color planes), [1,3,24,24] you would access e.g. the 2nd channel like:

Mat green(24,24,CV_32F, blob.ptr<float>(0,1)); // image 0, channel 1

if your prob blob came from a prediction (1 of N classes), you can simply reshape it :

Mat flat = prob.reshape(1,1); //a single row, with nDetections columns.
Point maxLoc;
minMaxLoc(flat, 0,0,0,&maxLoc);
int predicted = maxLoc.x;

if it was from a detection (N detections a 7 numbers)

// from the ssd_mobilenet_object_detection sample    
Mat detectionMat(prob.size[2], prob.size[3], CV_32F, prob.ptr<float>());
edit flag offensive delete link more

Comments

1

In my case is not a nDetections columns. my model is a object detection, so it classifies and also give the bounding box. When applying reshape for example this are the results.

prob.dims = 4
prob.size = [1 X 1 X 3 X 7]

ProbMat [0, 9, 0.99742222, 0.69545317, 0.42244324, 0.99069548, 0.98882437, 0, 9, 0.86294723, 0.82298559, 0.23074003, 0.98194772, 0.48593336, 0, 15, 0.99996269, 0.18963903, 0.15723693, 0.8624571, 0.9951545]

So I want to obtain a (3,7) Mat.

isra60 gravatar imageisra60 ( 2017-10-24 05:20:14 -0600 )edit

ah, misread it, then. please see update above !

berak gravatar imageberak ( 2017-10-24 05:34:39 -0600 )edit
1

Great!! Thank you .

isra60 gravatar imageisra60 ( 2017-10-24 06:25:29 -0600 )edit

also keep a close watch on the dnn samples

berak gravatar imageberak ( 2017-10-24 06:27:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-24 02:34:54 -0600

Seen: 4,106 times

Last updated: Oct 24 '17