C++ 如何将具有两个通道的Mat转换为矢量<;向量<;int>>;?

C++ 如何将具有两个通道的Mat转换为矢量<;向量<;int>>;?,c++,opencv,vector,vectorization,mat,C++,Opencv,Vector,Vectorization,Mat,如果我有一个像这样的垫子 Mat mat = (Mat_<int>(1, 8) << 5, 6, 0, 4, 0, 1, 9, 9); 但是当mat有2个或更多通道时,如何将其转换为向量?我的意思是如果我有这样的mat int vec[4][2] = { {5, 6}, {0, 4}, {0,1}, {9, 9} }; Mat mat(4,1,CV_32SC2,vec); 如何得到向量向量2{{5,6},{0,4},{0,1},{9,9}?当然我们可以像这样遍历像素

如果我有一个像这样的垫子

Mat mat = (Mat_<int>(1, 8) << 5, 6, 0, 4, 0, 1, 9, 9);
但是当
mat
有2个或更多通道时,如何将其转换为
向量
?我的意思是如果我有这样的
mat

int vec[4][2] = { {5, 6}, {0, 4}, {0,1}, {9, 9} };
Mat mat(4,1,CV_32SC2,vec);
如何得到向量向量2{{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]);
}
vec2;
对于(int i=0;i<4;i++){
Vec2i*p=材料ptr(i);
vec2.推回(vector());
vec2[vec2.size()-1]。向后推(p[0][0]);
vec2[vec2.size()-1]。向后推(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);
}
cv::Mat image=imread(“picture.jpg”);
标量[3]={{255,0,0},{0255,0},{0,0,255};
Mat通道[3];
矢量分割图像;
对于(int i=0;i<3;i++){
通道[i]=图像和标量[i];
向量向量(通道[i].begin(),通道[i].end());
分割的图像。推回(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);
vec1,vec2;
//我们可以将2通道图像转换为具有2列的单通道图像
材料重塑(1.col(0.copyTo)(vec1)//要访问第一列
材料重塑(1)、柱(1)、复制(2)//要访问第二列
//或者像这样(如果你知道频道的数量
载体vec(2);
mat.reformate(1.col(0.copyTo)(vec[0]);//访问第一列
mat.reformate(1.col(1.copyTo)(vec[1]);//访问第二列
//你可以把它们做成这样的点向量
向量pts;
材料副本(pts);

一般来说,我会将任何矩阵存储在一个
std::vector
(而不是嵌套向量)中。双个子脚本是IMHO“syntax sugar”可以简单地简化为类似于
iRow*nCols+iCol
。对于多通道矩阵,我们想到了两种解决方案:要么使用
struct
作为向量元素,要么使用与上述相同的寻址技巧制作“三维”矩阵作为向量:
(iRow*nCols+iCol)*nChans+iChan
。您是否有任何具体理由使用大小为[numPixels][numChannels]的向量而不是[numChannels][numPixels]?因为后者更容易通过拆分实现。
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);