Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is possible convert the Mat with two channels into a vector<vector<int>>

If I have a mat like

Mat mat = (Mat_<int>(1, 8) << 5, 6, 0, 4, 0, 1, 9, 9);

Of course I can convert mat into a vector vec by

vector<int> vec(mat.begin<int>(), mat.end<int>());

But when the mat have 2 or more channels, how to convert it into a vector<vector<int>>? I mean if I have such Mat

int vec[4][2] = { {5, 6}, {0, 4}, {0,1}, {9, 9} };
Mat mat(4,1,CV_32SC2,vec);

How to get a vector<vector<int>> vec2{ {5, 6}, {0, 4}, {0,1}, {9, 9} }

Is possible convert the Mat with two channels into a vector<vector<int>>

If I have a mat like

Mat mat = (Mat_<int>(1, 8) << 5, 6, 0, 4, 0, 1, 9, 9);

Of course I can convert mat into a vector vec by

vector<int> vec(mat.begin<int>(), mat.end<int>());

But when the mat have 2 or more channels, how to convert it into a vector<vector<int>>? I mean if I have such Mat

int vec[4][2] = { {5, 6}, {0, 4}, {0,1}, {9, 9} };
Mat mat(4,1,CV_32SC2,vec);

How to get a vector<vector<int>> vec2{ {5, 6}, {0, 4}, {0,1}, {9, 9} }? Of course we can traverse very pixel like this

vector<vector<int>> vec2;
for (int i = 0; i < 4; i++) {
    Vec2i*p = mat.ptr<Vec2i>(i);
    vec2.push_back(vector<int>());
    vec2[vec2.size() - 1].push_back(p[0][0]);
    vec2[vec2.size() - 1].push_back(p[0][1]);
}

But any better method can do this?