如何将具有两个通道的 Mat 转换为向量<vector<int>>?
How to convert the Mat with two channels into a vector<vector<int>>?
如果我有这样的垫子
Mat mat = (Mat_<int>(1, 8) << 5, 6, 0, 4, 0, 1, 9, 9);
当然我可以通过
将mat
转换成向量vec
vector<int> vec(mat.begin<int>(), mat.end<int>());
但是当mat
有2个或更多通道时,如何将其转换为vector<vector<int>>
?我的意思是如果我有这样的 mat
int vec[4][2] = { {5, 6}, {0, 4}, {0,1}, {9, 9} };
Mat mat(4,1,CV_32SC2,vec);
如何得到一个vector<vector<int>> vec2{ {5, 6}, {0, 4}, {0,1}, {9, 9} }
?当然我们可以这样遍历非常像素
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]);
}
但是有没有更好的方法可以做到这一点?
尝试使用此代码段输入 CV_32SC3 图片:
cv::Mat image = imread("picture.jpg");
cv::Scalar scalar[3] = { { 255, 0, 0}, { 0,255, 0 }, { 0, 0, 255 } };
Mat channel[3];
vector<vector<int>> splitted_images;
for (int i = 0; i < 3; i++) {
channel[i] = image & scalar[i];
vector<int> vec(channel[i].begin<int>(), channel[i].end<int>());
splitted_images.push_back(vec);
}
您可以尝试使用具有 2 个或更多通道的垫子
vector<int> vec1, vec2;
//we can convert the 2 channel image into a single channel image with 2 columns
mat.reshape(1).col(0).copyTo(vec1); //to access the first column
mat.reshape(1).col(1).copyTo(vec2); //to access the second column
//Or like this (if you know the number of channels
vector<vector<int>> vec(2);
mat.reshape(1).col(0).copyTo(vec[0]); //to access the first column
mat.reshape(1).col(1).copyTo(vec[1]); //to access the second column
//You can make them into a vector of points like this
vector<Point> pts;
mat.copyTo(pts);
如果我有这样的垫子
Mat mat = (Mat_<int>(1, 8) << 5, 6, 0, 4, 0, 1, 9, 9);
当然我可以通过
将mat
转换成向量vec
vector<int> vec(mat.begin<int>(), mat.end<int>());
但是当mat
有2个或更多通道时,如何将其转换为vector<vector<int>>
?我的意思是如果我有这样的 mat
int vec[4][2] = { {5, 6}, {0, 4}, {0,1}, {9, 9} };
Mat mat(4,1,CV_32SC2,vec);
如何得到一个vector<vector<int>> vec2{ {5, 6}, {0, 4}, {0,1}, {9, 9} }
?当然我们可以这样遍历非常像素
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]);
}
但是有没有更好的方法可以做到这一点?
尝试使用此代码段输入 CV_32SC3 图片:
cv::Mat image = imread("picture.jpg");
cv::Scalar scalar[3] = { { 255, 0, 0}, { 0,255, 0 }, { 0, 0, 255 } };
Mat channel[3];
vector<vector<int>> splitted_images;
for (int i = 0; i < 3; i++) {
channel[i] = image & scalar[i];
vector<int> vec(channel[i].begin<int>(), channel[i].end<int>());
splitted_images.push_back(vec);
}
您可以尝试使用具有 2 个或更多通道的垫子
vector<int> vec1, vec2;
//we can convert the 2 channel image into a single channel image with 2 columns
mat.reshape(1).col(0).copyTo(vec1); //to access the first column
mat.reshape(1).col(1).copyTo(vec2); //to access the second column
//Or like this (if you know the number of channels
vector<vector<int>> vec(2);
mat.reshape(1).col(0).copyTo(vec[0]); //to access the first column
mat.reshape(1).col(1).copyTo(vec[1]); //to access the second column
//You can make them into a vector of points like this
vector<Point> pts;
mat.copyTo(pts);