TomH
TomH

Reputation: 2975

openCV Mat N-dimensional reshape

I have a cv::Mat mat returned from dnn::blobFromImage that is size 1x3x600x450. (img is BGR w/o an alpha channel)

cv::Mat mat = dnn::blobFromImage(img);
cout << mat.size() << endl; // 1x3x600x450

What is the analog to numpy.reshape to reshape this to 3x600x450?

In python, if I assert that mat.shape[0] == 1, then with numpy.reshape I would do something like

mat = mat.reshape((mat.shape[1], mat.shape[2], mat.shape[3]))

What is the equivalent in OpenCV?

Upvotes: 2

Views: 3756

Answers (1)

TomH
TomH

Reputation: 2975

Per Dan's response in the comments above, this is what I went with:

cv::Mat mat = dnn::blobFromImage(img);
cout << mat.size() << endl; // 1x3x600x450

int sz[] = {mat.size[1], mat.size[2], mat.size[3]}; 
Mat newmat(3, sz, mat.type(), mat.ptr<float>(0));
cout << newmat.size() << endl; // 3x600x450

Upvotes: 3

Related Questions